/*
 *  Copyright
 *      Copyright (c) Exadel,Inc. 2006
 *      All rights reserved.
 *  
 *  History
 *      $Source: /cvs-master/intralinks-jsf-comps/web-projects/data-view-grid-demo/src/com/exadel/jsf/demo/datagrid/service/JiraService.java,v $
 *      $Revision: 1.5 $ 
 */

package org.richfaces.demo.datagrid.service;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.ExtendedBaseRules;
import org.apache.commons.digester.Rule;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.richfaces.demo.datagrid.model.Channel;
import org.richfaces.demo.datagrid.model.Issue;
import org.richfaces.demo.datagrid.model.JiraUser;
import org.richfaces.demo.datagrid.model.Key;
import org.richfaces.demo.datagrid.model.Priority;
import org.richfaces.demo.datagrid.model.Status;
import org.richfaces.demo.datagrid.model.Type;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * @author Maksim Kaszynski
 *
 */
public class JiraService {

	private static final String KEY = "jiraService";
	
	private static final Log log = LogFactory.getLog(JiraService.class);
	
	private static final JiraService instance = new JiraService();
	
	/**
	 * 
	 */
	public JiraService() {
		super();
		log.trace("---> JiraService.JiraService()");
		init();
	}

	private Channel channel = null;
	public Channel getChannel() {
		
		if (channel == null) {
			init();
		}
		
		return channel;
	}
	public void setChannel(Channel channel) {
		this.channel = channel;
	}


	static class JiraCursedRule extends Rule {

		private String propertyName;

		public JiraCursedRule(String propertyName) {
			super();
			this.propertyName = propertyName;
		}

		@Override
		public void body(String namespace, String name, String text) throws Exception {
			Object bean = digester.peek();
			PropertyUtils.setSimpleProperty(bean, propertyName, text.trim());
			
		}

		@Override
		public void finish() throws Exception {
			// TODO Auto-generated method stub
			super.finish();
		}
	}

	
	private void init() {
		System.out.println("JiraService.init()");
		channel = new Channel();
		Digester digester = new Digester();
		digester.setValidating(false);
		digester.setNamespaceAware(false);
		digester.setEntityResolver(new EntityResolver() {
			public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
				// TODO Auto-generated method stub
				return null;
			}
		});
		digester.setRules(new ExtendedBaseRules());
		
		digester.push(channel);
		
		String path = "rss/channel";
		digester.addBeanPropertySetter(path + "/?");
		
		path = path + "/item";
		String issuePath = path;
		digester.addObjectCreate(path, Issue.class);
		digester.addSetProperties(path);
		digester.addBeanPropertySetter(path + "/?");
		digester.addSetNext(path, "addIssue");
		
		path = issuePath + "/key";
		digester.addObjectCreate(path, Key.class);
		digester.addSetProperties(path);
		digester.addRule(path, new JiraCursedRule("value"));
		digester.addBeanPropertySetter(path + "/?");
		digester.addSetNext(path, "setKey");
		
		path = issuePath + "/priority";
		digester.addObjectCreate(path, Priority.class);
		digester.addSetProperties(path);
		digester.addBeanPropertySetter(path + "/?");
		digester.addRule(path, new JiraCursedRule("name"));
		digester.addSetNext(path, "setPriority");
		
		path = issuePath + "/status";
		digester.addObjectCreate(path, Status.class);
		digester.addSetProperties(path);
		digester.addBeanPropertySetter(path + "/?");
		digester.addRule(path, new JiraCursedRule("name"));
		digester.addSetNext(path, "setStatus");

		path = issuePath + "/assignee";
		digester.addObjectCreate(path, JiraUser.class);
		digester.addSetProperties(path);
		digester.addBeanPropertySetter(path + "/?");
		digester.addRule(path, new JiraCursedRule("name"));
		digester.addSetNext(path, "setAssignee");
		
		path = issuePath + "/reporter";
		digester.addObjectCreate(path, JiraUser.class);
		digester.addSetProperties(path);
		digester.addBeanPropertySetter(path + "/?");
		digester.addRule(path, new JiraCursedRule("name"));
		digester.addSetNext(path, "setReporter");
		
		path = issuePath + "/type";
		digester.addObjectCreate(path, Type.class);
		digester.addSetProperties(path);
		digester.addBeanPropertySetter(path + "/?");
		digester.addRule(path, new JiraCursedRule("name"));
		digester.addSetNext(path, "setType");
		try {
			//InputStream stream; //= Thread.currentThread().getContextClassLoader().getResourceAsStream();
			//stream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/WEB-INF/xmldata/jira.xml");
			//JarInputStream stream = getXMLInputStream();
			InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("jira.xml");
			digester.parse(stream);
			stream.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	public static final JiraService getInstance() {
		return instance;
	}
}
