Package org.apache.chemistry.opencmis.client.api

Examples of org.apache.chemistry.opencmis.client.api.Session


        }

        // create session with the first (and only) repository
        Repository repository = repositories.get(0);
        parameter.put(SessionParameter.REPOSITORY_ID, repository.getId());
        Session session = sessionFactory.createSession(parameter);

        System.out.println("Got a connection to repository: " + repository.getName()
                + ", with id: " + repository.getId() + EOL);


        // Get everything in the root folder and print the names of the objects
        Folder root = session.getRootFolder();
        ItemIterable<CmisObject> children = root.getChildren();
        System.out.println("Found the following objects in the root folder:-");
        for (CmisObject o : children) {
            System.out.println(o.getName() + " which is of type " + o.getType().getDisplayName());
        }

        System.out.println(EOL + "File and Folders...");
        System.out.println("-------------------");

        Folder defaultHost = null;
        for (CmisObject o : root.getChildren()) {
          defaultHost = (Folder) o;
            break;
        }

        // Add a new folder to the default host
        System.out.println("Creating 'CMISTest" + new java.util.Date().getTime() +"' to the default host : " + defaultHost.getName() + EOL);

        Map<String, String> newFolderProps = new HashMap<String, String>();
        newFolderProps.put(PropertyIds.OBJECT_TYPE_ID, BaseTypeId.CMIS_FOLDER.value());
        newFolderProps.put(PropertyIds.NAME, "CMISTest" + new java.util.Date().getTime());
        Folder newFolder = defaultHost.createFolder(newFolderProps);

        // Did it work?
        children = root.getChildren();
        System.out.println("Now finding the following objects in the default host : " + defaultHost.getName());
        for (CmisObject o : children) {
          defaultHost = (Folder) o;
          ItemIterable<CmisObject> hostChildren = defaultHost.getChildren();
          for (CmisObject obj : hostChildren) {
            System.out.println(obj.getName());
          }
          break;
        }

       
        // Create a simple text document in the new folder under default host       
        // First, create the content stream
        final String textFileName = "test.txt";
        System.out.println(EOL + "Creating a simple text document : " + textFileName + " inside the above created new folder" + EOL);
        String mimetype = "text/plain; charset=UTF-8";
        String content = "This is some test content.";
        String filename = textFileName;

        byte[] buf = null;
        try {
            buf = content.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        ByteArrayInputStream input = new ByteArrayInputStream(buf);
        ContentStream contentStream = session.getObjectFactory().createContentStream(filename,
                buf.length, mimetype, input);

        // Create the Document Object
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(PropertyIds.OBJECT_TYPE_ID, BaseTypeId.CMIS_DOCUMENT.value());
        properties.put(PropertyIds.NAME, filename);
        ObjectId id = newFolder.createDocument(properties, contentStream, VersioningState.NONE);

        // Did it work?
        // Get the contents of the document by id
        System.out.println("Getting object by id : " + id.getId());
        Document doc = (Document) session.getObject(id);
        try {
            content = getContentAsString(doc.getContentStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Contents of " + doc.getName() + " are : " + content + EOL);

        // Get the contents of the document by path
        String path = newFolder.getPath();
        if(!path.endsWith("/"))
          path = path + "/";
        path = path + textFileName;
        System.out.println("Getting object by path : " + path);
        doc = (Document) session.getObjectByPath(path);
        try {
            content = getContentAsString(doc.getContentStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Contents of " + doc.getName() + " are : " + content + EOL);

        // Types
        System.out.println("\nTypes...");
        System.out.println("--------");
        // Look at the type definition
        System.out.println("Getting type definition for doc");
        ObjectType objectType = session.getTypeDefinition(doc.getType().getId());
        System.out.println("doc is of type " + objectType.getDisplayName());
        System.out.println("isBaseType() returns " + (objectType.isBaseType() ? "true" : "false"));
        ObjectType baseType = objectType.getBaseType();
        if (baseType == null) {
            System.out.println("getBaseType() returns null");
        } else {
            System.out.println("getBaseType() returns " + baseType.getDisplayName());
        }
        ObjectType parentType = objectType.getParentType();
        if (parentType == null) {
            System.out.println("getParentType() returns null");
        } else {
            System.out.println("getParentType() returns " + parentType.getDisplayName());
        }
        System.out.println("Listing child types of " + objectType.getDisplayName());
        for (ObjectType o : objectType.getChildren()) {
            System.out.println("\t" + o.getDisplayName());
        }
        System.out.println("Getting immediate descendant types of " + objectType.getDisplayName());
        for (Tree<ObjectType> o : objectType.getDescendants(1)) {
            System.out.println("\t" + o.getItem().getDisplayName());
        }

        System.out.println("\nProperties...");
        System.out.println("-------------");

        // Look at all the properties of the document
        System.out.println(doc.getName() + " properties start");
        List<Property<?>> props = doc.getProperties();
        for (Property<?> p : props) {
            System.out.println(p.getDefinition().getDisplayName() + "=" + p.getValuesAsString());
        }
        System.out.println(doc.getName() + " properties end" + EOL);

        // get a property by id
        System.out.println("get property by property id");
        Property<?> someProperty = props.get(0);
        System.out.println(someProperty.getDisplayName() + " property on " + doc.getName()
                + " (by getPropertValue()) is " + doc.getPropertyValue(someProperty.getId()));

        // get a property by query name
        System.out.println("get property by query name");
        if (session.getRepositoryInfo().getCapabilities().getQueryCapability()
                .equals(CapabilityQuery.METADATAONLY)) {
            System.out.println("Full search not supported");
        } else {
            String query = "SELECT * FROM cmis:document WHERE cmis:name = 'test.txt'";
            ItemIterable<QueryResult> queryResult = session.query(query, false);
            for (QueryResult item : queryResult) {
                System.out.println("property cmis:name on test.txt is "
                        + item.getPropertyByQueryName("cmis:name").getFirstValue());
                break;
            }
        }

        GregorianCalendar calendar = doc.getLastModificationDate();
        String DATE_FORMAT = "yyyyMMdd";
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        System.out.println("Last modification for  " + doc.getName() + " is  on "
                + sdf.format(calendar.getTime()));

        System.out.println("\nQuery...");
        System.out.println("--------");
        // Query 1 - need full query capability for this
        if (session.getRepositoryInfo().getCapabilities().getQueryCapability()
                .equals(CapabilityQuery.METADATAONLY)) {
            System.out.println("Full search not supported");
        } else {
            String query = "SELECT * FROM cmis:document WHERE cmis:name = '%a%'";// News content type
            ItemIterable<QueryResult> q = session.query(query, false);

            // Did it work?
            System.out.println("***results from query " + query);

            int i = 1;
            for (QueryResult qr : q) {
                System.out.println("--------------------------------------------\n" + i + " , "
                        + qr.getPropertyByQueryName("cmis:objectTypeId").getFirstValue() + " , "
                        + qr.getPropertyByQueryName("cmis:name").getFirstValue() + " , "
                        + qr.getPropertyByQueryName("cmis:objectId").getFirstValue() + " , ");
                i++;
            }

        }

        // Capabilities
        System.out.println("\nCapabilities...");
        System.out.println("---------------");
        // Check what capabilities our repository supports
        System.out.println("Printing repository capabilities...");
        final RepositoryInfo repInfo = session.getRepositoryInfo();
        RepositoryCapabilities cap = repInfo.getCapabilities();
        System.out.println("\nNavigation Capabilities");
        System.out.println("-----------------------");
        System.out.println("Get descendants supported: "
                + (cap.isGetDescendantsSupported() ? "true" : "false"));
View Full Code Here

TOP

Related Classes of org.apache.chemistry.opencmis.client.api.Session

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.