Page tree
Skip to end of metadata
Go to start of metadata
	    /*0.) Setup Repository Instance: Many ways to do this */
            Repository repository = getRepositoryFromSomewhere();

            Session session =
        	repository.login(
        	    new SimpleCredentials(
        		    "username",
        		    "password".toCharArray()
        	    )
        	);

            /*1.) Editing: Create a node in the repository */
            {
                Node rootNode = session.getRootNode();
                Node item = rootNode.addNode("1721.1_33972");

                /* Set a simple property on the node */
                item.setProperty("title", "My Thesis");
                item.setProperty("creationTime", Calendar.getInstance());
                item.setProperty("userName", "username");

                {
                    /* Putting binary Content into a property */
                    Node bitstream = item.addNode("bitstream_1");
                    bitstream.setProperty("name", "file.txt");
                    bitstream.setProperty("description", "My File");
                    bitstream.setProperty("creationTime", Calendar.getInstance());
                    
                    InputStream in = getInputStreamFromSomewhere();
                    bitstream.setProperty("content", in);
                }


                session.save();
            }

            /*2a.) Accessing: Iterate over Nodes */
            {
                Node root = session.getRootNode();

                for (NodeIterator iter = root.getNodes(); iter.hasNext(); ) {
                    Node item = iter.nextNode();

                    if (!item.getName().equals("1721.1_33972"))
                        continue;
                    String title = item.getProperty("title").getString();
                    Value creationTimeValue = (Value) item.getProperty("creationTime").getValue();
                    String userName = item.getProperty("userName").getString();

                    {
                	/* get the bitstream node and retreive an InputStream */
                	Node bitstream = item.getNode("1");
          	      	Value value = (Value) bitstream.getProperty("content").getValue();
          	      	InputStream in = value.getStream();
          	      	
          	      	/*... Do somethinf fun here */
          	      	
                    }
                }
            }

            /*2b.) Accessing: Use XPath to Query Nodes */
            {
        	Workspace workSpace = session.getWorkspace();
        	QueryManager queryManager = workSpace.getQueryManager();

        	Query query = queryManager.createQuery(
        		"//1721.1_33972[@title='My Thesis']/bitstream_1",
        	        Query.XPATH);

        	QueryResult result = query.execute();
        	for (NodeIterator iter = result.getNodes(); iter.hasNext(); ) {
        	    Node bitstream = iter.nextNode();
        	    /* get the bitstream node and retreive an InputStream */
            	    Value value = (Value) bitstream.getProperty("content").getValue();
      	      	    InputStream in = value.getStream();
      	      	    /*... Do something fun here */ 	    
        	}

            }

            /* 3a.) Versioning: Checkout, Modify and Checkin */
            {
        	Node node = getNodeFromSomewhere();
        	node.checkout();
        	node.setProperty("wiki:content", "Updated content for the entry.");
        	node.save();
        	node.checkin();
            }
            
            /* 3b.) Versioning: Accessing Versions */
            {
        	Node node = getNodeFromSomewhere();
        	VersionHistory vh = node.getVersionHistory();
        	VersionIterator vi = vh.getAllVersions();
        	vi.skip(1);
		while (vi.hasNext()) {
		    Version v = vi.nextVersion();
		    for (NodeIterator ni = v.getNodes(); ni.hasNext(); ) {
			Node nv = ni.nextNode();
			System.out.println("Version: " + v.getCreated().getTime());
			System.out.println(nv.getProperty("wiki:title").getString());
			System.out.println(nv.getProperty("wiki:content").getString());
		    }
		}
            }
  • No labels