What is a Java content repository (Java content repository) (end 4)

Source: Internet
Author: User
Get list
In the previous step, we have saved the data to the content warehouse. How can we ensure that the data is actually saved? The getbloglist () method returns all the child nodes named blogentry. Under the root node.

PublicArraylist getbloglist ()Throws Blogapplicationexception {
Session session= Jackrabbitplugin. getsession ();
Arraylist blogentrylist= New Arraylist ();
Node rootnode= Session. getrootnode ();
Nodeiterator blogentrynodeiterator= Rootnode. getnodes ();

While (Blogentrynodeiterator. hasnext ()){
Node blogentry= Blogentrynodeiterator. nextnode ();
If(Blogentry. getname (). Equals ("Blogentry")= False )
Continue ;
String title=Blogentry. getproperty ("Title" ). Getstring ();
String blogcontent=Blogentry. getproperty ("Blogcontent" ). Getstring ();
Value creationtimevalue= (Value) blogentry. getproperty (
"Creationtime" ). Getvalue ();
String Username=Blogentry. getproperty ("Username" ). Getstring ();
Blogentrydto= New Blogentrydto (username, title,
Blogcontent, creationtimevalue. getdate ());
Blogentrylist. Add (blogentrydto );
}
Return Blogentrylist;
}

Once you obtain the Root Node object, you can call the getnodes () method to obtain all its subnodes. If the node does not have a subnode, an empty nodeiterator object is returned. We can traverse this nodeiterator object to obtain the node set named blogentry, and then use the getproperty () method to obtain the attributes on the node, that is, the actual data we save. The getproperty () method returns an instance of the value object. Because different data types are stored, the returned value object instances are different. Depending on different data types, you should call a specific method to obtain data, such as getstring () to obtain the string, while getdate () to obtain a date.

Search for content (using XPath)
The JSR-170 defines two ways to find content (which can also be understood as finding nodes ). One uses the XPath syntax and the other uses the SQL syntax. JSR-170 requires that level 1 must implement the XPath approach, while the SQL approach serves as an optional feature.

XPath was originally designed to look for XML elements. Because our workspace is a tree structure, which is similar to XML. Therefore, the XPath syntax is very suitable for finding content here. The followingCodeThis example shows how to use the author name to find a node.

Session session= Jackrabbitplugin. getsession ();
Workspace= Session. getworkspace ();
Querymanager= Workspace. getquerymanager ();

Stringbuffer querystr =   New Stringbuffer (
"// Blogentry [@"+Prop_blogauthor+"='" );
Querystr. append (username );
Querystr. append ("']" );
Query= Querymanager. createquery (querystr. tostring (),
Query. XPath );

Queryresult=Query.exe cute ();

Nodeiterator queryresultnodeiterator=Queryresult. getnodes ();
While(Queryresultnodeiterator. hasnext ()){

Node blogentry = Queryresultnodeiterator. nextnode ();
String title= Blogentry. getproperty (prop_title). getstring ();
String blogcontent= Blogentry. getproperty (prop_blogcontent). getstring ();
Value creationtimevalue= (Value) blogentry. getproperty (
Prop_creationtime). getvalue ();
Blogentrydto= New Blogentrydto (username, title,
Blogcontent, creationtimevalue. getdate ());
Blogentrylist. Add (blogentrydto );
}

First obtain the session object, get the workspace connected to it, and then obtain the querymanager of the workspace through the workspace. The querymanager interface defines many query methods. Next, we need to create a query statement. Here we write "// blogentry [@ blogauthor = '<bloggername> '". This statement searches for all nodes with the blogentry attribute blogauthor and the attribute value is <bloggername>. Specific can look at the JSR-170 specification.

The querymanager's createquery () method is used to create a query object. This method requires two parameters: one is our query statement and the other is the query method. Here we use XPath. After obtaining the query object, call its execute () method to start executing the query and return a queryresult object. Note that the query results are restricted by the current session. In other words, if the session has no permission to view a specific node, even if the node meets the query conditions, we cannot see this node in our query results. All query data comes from the persistent data of the workspace, which has changed but has not passed the session. save () (item. save () The data persisted to the workspace is not in the query column. After obtaining the queryresult object, we can call the getnodes () method to obtain a traversal of a node that meets the query conditions.

The remaining two unimplemented methods are updateblogentry () and removeblogentry (), which are easy to implement. We use the Bolg title as the primary key to obtain relevant nodes through the title. In the updateblogentry () method, we directly set the attributes to be changed. In the removeblogentry () method, we obtain the target node and directly call the remove () method on the node. Do not forget to call session. Save () to make the changed data persistent.

Processing binary content
For content warehouses, processing binary content is a basic requirement, such as an image. Our exampleProgramAttach an image to each blog. The following are the methods for attaching images and obtaining images respectively.

Public Void Attachfiletoblogentry (string blogtitle,
Inputstream uploadinputstream)Throws Blogapplicationexception {
Session session= Jackrabbitplugin. getsession ();
Node blogentrynode= Getblogentrynode (blogtitle, session );
Blogentrynode. setproperty (prop_attachment, uploadinputstream );
Session. Save ();

}
Public Inputstream getattachedfile (string blogtitle) Throws Blogapplicationexception {
Inputstream attachfileis= Null ;
Node blogentrynode= Getblogentrynode (blogtitle );
Value attachfilevalue= (Value) blogentrynode. getproperty (prop_attachment). getvalue ();
Attachfileis= Attachfilevalue. getstream ();
Return Attachfileis;
}

As you can see, our code is no big difference between processing binary content and general content. The difference is that you need to store and obtain binary data through the inputstream object. In our configuration file, persistent manager has an externalblobs attribute. Set this attribute to true, and the image will be saved in the file. On the contrary, the image will be saved in the Blob field of the database.

Summary
Here we have a rough understanding of JSR-170, Jackrabbit, and how to use JSR-170 APIs to develop a simple application. Our discussion is more about the basics. I believe everyone will have a preliminary understanding of the content warehouse.

Http://www.blogjava.net/ronghao Rong Hao original, thanks!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.