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!