// 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.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.chemistry.opencmis.client.api.Document;
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.PropertyIds;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.chemistry.opencmis.commons.enums.BindingType;
import org.apache.chemistry.opencmis.commons.enums.VersioningState;
import org.apache.chemistry.opencmis.commons.exceptions.CmisNameConstraintViolationException;
import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;

// Note: paths are actually implementation specific but /cmis:name/cmis:name/cmis:name is common
public class CRUD {
    private static void dump(Session ses, String path) throws IOException {
        try {
            Document f = (Document) ses.getObjectByPath(path);
            for(Property<?> p : f.getProperties()) {
                if(p.getValue() != null) {
                    System.out.printf("%s = %s\n", p.getLocalName(), p.getValue());
                }
            }
            ContentStream stm = f.getContentStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(stm.getStream()));
            String line;
            while((line = br.readLine()) != null) {
                System.out.println(line);
            }
            br.close();
        } catch (CmisObjectNotFoundException ex) {
            System.out.println(path + " does not exist");
        }
    }
    public static void main(String[] args) throws Exception {
        Map<String, String> param = new HashMap<String, String>();
        param.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
        param.put(SessionParameter.ATOMPUB_URL, "http://localhost:8080/chemistry/atom");
        param.put(SessionParameter.REPOSITORY_ID, "test");
        param.put(SessionParameter.USER, "test");
        param.put(SessionParameter.PASSWORD, "test");
        Session ses = SessionFactoryImpl.newInstance().createSession(param);
        // read
        dump(ses, "/RW/demo.txt");
        // create
        String s = "A\r\nBB\r\nCCC\r\n";
        Folder dir = (Folder) ses.getObjectByPath("/RW");
        Map<String, Object> props = new HashMap<String, Object>();
        props.put(PropertyIds.NAME, "demo.txt");
        props.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
        ContentStream stm = ses.getObjectFactory().createContentStream("demo.txt", s.length(), "text/plain", new ByteArrayInputStream(s.getBytes()));
        try {
            dir.createDocument(props, stm, VersioningState.NONE);
        } catch(CmisNameConstraintViolationException ex) {
            System.out.println("demo.txt did already exist");
        }
        // read
        dump(ses, "/RW/demo.txt");
        // delete
        ses.getObjectByPath("/RW/demo.txt").delete(true);
        // read
        dump(ses, "/RW/demo.txt");
    }
}
