package com;

import javax.jcr.*;

import org.apache.jackrabbit.core.TransientRepository;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Third Jackrabbit example application. Imports an example XML file
 * and outputs the contents of the entire workspace.
 */
public class FifthHop {

	public static Session session = null;
	
    /** Runs the FifthHop example. */
	public static void main(String[] args) throws Exception {
		Repository repository = new TransientRepository();
		session = repository.login(new SimpleCredentials("username", "password"
				.toCharArray()));

		try {
			Node root = session.getRootNode();
			root.addMixin("mix:referenceable");

			Node node = root.addNode("myRoot");
			node.addMixin("mix:referenceable");
			
			FileInputStream xml = new FileInputStream(
						"D://workspace//sample//src//com//configure.xml");
			session.importXML("/myRoot" , xml,
						ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);

			xml.close();
			session.save();
			dump (session.getNode("/myRoot"));
			updateConfig("<configuration><parameter1>50</parameter1><parameter2>100</parameter2></configuration>");
			dump (session.getNode("/myRoot"));

		} finally {
			session.logout();
		}

	}
    
    
	public static void updateConfig(String xml)
			throws PathNotFoundException, RepositoryException, IOException {
		System.out.println("Updating xml:" + xml);
		updateConfig(new ByteArrayInputStream(xml.getBytes()));
	}

	public static void updateConfig(InputStream xml)
			throws PathNotFoundException, RepositoryException, IOException {

		Node node = session.getRootNode();

		if (node == null) {
			throw new PathNotFoundException("Node not found");
		} else {
			session.importXML("/myRoot" , xml,
					ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
			System.out.println("==========================");
			session.save();
		}
	}

    /** Recursively outputs the contents of the given node. */
    private static void dump(Node node) throws RepositoryException {
        // Skip the virtual (and large!) jcr:system subtree
        if (node.getName().equals("jcr:system")) {
            return;
        }        
        node.addMixin("mix:referenceable");
        // Then output the properties
        PropertyIterator properties = node.getProperties();
        while (properties.hasNext()) {
            Property property = properties.nextProperty();
            if (property.getDefinition().isMultiple()) {
                // A multi-valued property, print all values
                Value[] values = property.getValues();
                for (int i = 0; i < values.length; i++) {
                	//if (property.getType().equals("jcr:xmlcharacters"))
                    System.out.println(
                        property.getPath() + " = " + values[i].getString());
                }
            } else {
                // A single-valued property
                 System.out.println(
                    property.getPath() + " = " + property.getString());
            }
        }

        // Finally output all the child nodes recursively
        NodeIterator nodes = node.getNodes();
        while (nodes.hasNext()) {
             dump(nodes.nextNode());
        }
    }

}


