// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package cmisdemo;

import java.util.HashMap;
import java.util.Map;

import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.Property;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.enums.BindingType;

public class Explore {
    private static void list(String indent, Folder dir, boolean recurse, boolean showprops) {
        for(CmisObject item : dir.getChildren()) {
            System.out.printf("%s%s (%s)\n", indent, item.getName(), item.getType().getLocalName());
            if(showprops) {
                for(Property<?> p : item.getProperties()) {
                    if(p.getValue() != null) {
                        System.out.printf("%s- %s = %s\n", indent, p.getLocalName(), p.getValue());
                    }
                }
            }
            if(recurse && item instanceof Folder) {
                list(indent + "    ", (Folder)item, recurse, showprops);
            }
        }
    }
    public static void test(Map<String, String> param,  boolean recurse, boolean showprops) {
        System.out.println(param);
        Session ses = SessionFactoryImpl.newInstance().createSession(param);
        // server info
        System.out.println(ses.getRepositoryInfo().getName());
        System.out.println(ses.getRepositoryInfo().getDescription());
        System.out.println(ses.getRepositoryInfo().getProductName());
        System.out.println(ses.getRepositoryInfo().getProductVersion());
        // list items
        list("", ses.getRootFolder(), recurse, showprops);
    }
    public static void main(String[] args) {
        // Chemistry - AtomPub
        Map<String, String> chemistry_atompub = new HashMap<String, String>();
        chemistry_atompub.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
        chemistry_atompub.put(SessionParameter.ATOMPUB_URL, "http://localhost:8080/chemistry/atom");
        chemistry_atompub.put(SessionParameter.REPOSITORY_ID, "test");
        chemistry_atompub.put(SessionParameter.USER, "test");
        chemistry_atompub.put(SessionParameter.PASSWORD, "test");
        test(chemistry_atompub, true, true);
        // Chemistry - SOAP
        Map<String, String> chemistry_soap = new HashMap<String, String>();
        chemistry_soap.put(SessionParameter.BINDING_TYPE, BindingType.WEBSERVICES.value());
        chemistry_soap.put(SessionParameter.WEBSERVICES_ACL_SERVICE, "http://localhost:8080/chemistry/services/cmis?wsdl");
        chemistry_soap.put(SessionParameter.WEBSERVICES_DISCOVERY_SERVICE, "http://localhost:8080/chemistry/services/cmis?wsdl");
        chemistry_soap.put(SessionParameter.WEBSERVICES_MULTIFILING_SERVICE, "http://localhost:8080/chemistry/services/cmis?wsdl");
        chemistry_soap.put(SessionParameter.WEBSERVICES_NAVIGATION_SERVICE, "http://localhost:8080/chemistry/services/cmis?wsdl");
        chemistry_soap.put(SessionParameter.WEBSERVICES_OBJECT_SERVICE, "http://localhost:8080/chemistry/services/cmis?wsdl");
        chemistry_soap.put(SessionParameter.WEBSERVICES_POLICY_SERVICE, "http://localhost:8080/chemistry/services/cmis?wsdl");
        chemistry_soap.put(SessionParameter.WEBSERVICES_RELATIONSHIP_SERVICE, "http://localhost:8080/chemistry/services/cmis?wsdl");
        chemistry_soap.put(SessionParameter.WEBSERVICES_REPOSITORY_SERVICE, "http://localhost:8080/chemistry/services/cmis?wsdl");
        chemistry_soap.put(SessionParameter.WEBSERVICES_VERSIONING_SERVICE, "http://localhost:8080/chemistry/services/cmis?wsdl");
        chemistry_soap.put(SessionParameter.REPOSITORY_ID, "test");
        chemistry_soap.put(SessionParameter.USER, "test");
        chemistry_soap.put(SessionParameter.PASSWORD, "test");
        test(chemistry_soap, true, true);
        // OpenCMS - AtomPub
        Map<String, String> opencms_atompub = new HashMap<String, String>();
        opencms_atompub.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
        opencms_atompub.put(SessionParameter.ATOMPUB_URL, "http://localhost:8080/opencms/cmisatom");
        opencms_atompub.put(SessionParameter.REPOSITORY_ID, "cmis-online");
        opencms_atompub.put(SessionParameter.USER, "Admin");
        opencms_atompub.put(SessionParameter.PASSWORD, "admin");
        test(opencms_atompub, false, false);
        // OpenCMS - SOAP
        Map<String, String> opencms_soap = new HashMap<String, String>();
        opencms_soap.put(SessionParameter.BINDING_TYPE, BindingType.WEBSERVICES.value());
        opencms_soap.put(SessionParameter.WEBSERVICES_ACL_SERVICE, "http://localhost:8080/opencms/services/cmis?wsdl");
        opencms_soap.put(SessionParameter.WEBSERVICES_DISCOVERY_SERVICE, "http://localhost:8080/opencms/services/cmis?wsdl");
        opencms_soap.put(SessionParameter.WEBSERVICES_MULTIFILING_SERVICE, "http://localhost:8080/opencms/services/cmis?wsdl");
        opencms_soap.put(SessionParameter.WEBSERVICES_NAVIGATION_SERVICE, "http://localhost:8080/opencms/services/cmis?wsdl");
        opencms_soap.put(SessionParameter.WEBSERVICES_OBJECT_SERVICE, "http://localhost:8080/opencms/services/cmis?wsdl");
        opencms_soap.put(SessionParameter.WEBSERVICES_POLICY_SERVICE, "http://localhost:8080/opencms/services/cmis?wsdl");
        opencms_soap.put(SessionParameter.WEBSERVICES_RELATIONSHIP_SERVICE, "http://localhost:8080/opencms/services/cmis?wsdl");
        opencms_soap.put(SessionParameter.WEBSERVICES_REPOSITORY_SERVICE, "http://localhost:8080/opencms/services/cmis?wsdl");
        opencms_soap.put(SessionParameter.WEBSERVICES_VERSIONING_SERVICE, "http://localhost:8080/opencms/services/cmis?wsdl");
        opencms_soap.put(SessionParameter.REPOSITORY_ID, "cmis-online");
        opencms_soap.put(SessionParameter.USER, "Admin");
        opencms_soap.put(SessionParameter.PASSWORD, "admin");
        test(opencms_soap, false, false);
    }
}
