考虑到webservice对某些对象解析冲突问题,暂且返回节点的UUID
INodeService.java
package com.jr.iservice; import java.io.InputStream; import java.util.List; import javax.jcr.Node; import com.jr.model.Document; /** * @author 李晗 */ public interface INodeService { //在指定节点下面添加节点 public String createNode(Node node,String nodeName) throws Exception; //在指定节点下面添加指定类型的节点 nt:file/nt:folder/nt:resource/nt:version public String createNode(Node node,String nodeName,String nodeType) throws Exception; //在指定节点下面添加文档 public void addDocument(Node node,Document doc)throws Exception; //获取指定文档的UUID(DOI主键) public String getUUID(Node node) throws Exception; //获取指定节点下面的所有子节点 public List<String> getNodes(Node node) throws Exception; //删除指定节点下面所有节点 public void removeNodes(Node node) throws Exception; //列出指定文档下面所有属性(元数据) public List<String> getProperties(Node node) throws Exception; //根据某一属性获取该文档属性值,返回字符串 public String getPropertyValue(Node node,String pName) throws Exception; //根据某一属性获取该文档属性值,返回流 public InputStream getPropertyValue(Node node,String pName,String fis) throws Exception; //修改指定的一个文档,不记录版本号 public void modifyDocumentWithNoVersion(Node node,Document doc) throws Exception; //修改指定的一个文档,记录版本号 public void modifyDocumentWithVersion(Node node,Document doc) throws Exception; //列出指定文档所有的版本号 public List<String> getVersions(Node node) throws Exception; //将指定文档/节点恢复到指定的版本号 public void restore(Node node,String vName)throws Exception; //将指定文档上锁 isDeep指定子节点是否有效,isSessionScope指定是否session范围 public void lock(Node node,boolean isDeep,boolean isSessionScope) throws Exception; //将指定文档解锁 public void unlock(Node node) throws Exception; //使用XPATH检索文档节点 public List<String> queryByXpath(String xPath) throws Exception; //使用SQL语句检索文档节点 public List<String> queryBySql(String sql) throws Exception; }
NodeService.java
package com.jr.service; import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.jcr.Node; import javax.jcr.NodeIterator; import javax.jcr.Property; import javax.jcr.PropertyIterator; import javax.jcr.query.Query; import javax.jcr.query.QueryManager; import javax.jcr.query.QueryResult; import javax.jcr.version.Version; import javax.jcr.version.VersionIterator; import com.jr.dao.SimpleSession; import com.jr.iservice.INodeService; import com.jr.model.Document; public class NodeService implements INodeService { /** * 在指定节点下面添加节点 * @param node * @param nodeName * @throws Exception */ public String createNode(Node node,String nodeName) throws Exception { node.addMixin("mix:versionable"); node.addMixin("mix:referenceable"); Node nodeSec = node.addNode(nodeName); nodeSec.addMixin("mix:referenceable"); SimpleSession.getSession().save(); return nodeSec.getUUID(); } /** * 在指定节点下面添加节点,指定类型 */ public String createNode(Node node,String nodeName,String nodeType) throws Exception{ node.addMixin("mix:versionable"); node.addMixin("mix:referenceable"); Node nodeSec = node.addNode(nodeName,nodeType); nodeSec.addMixin("mix:referenceable"); SimpleSession.getSession().save(); return nodeSec.getUUID(); } /** * 在指定节点下面添加文档 * @param node * @param doc * @throws Exception */ public void addDocument(Node node,Document doc) throws Exception{ node.setProperty("name", doc.getName()); node.setProperty("type", doc.getType()); node.setProperty("content", new FileInputStream(doc.getFile())); node.setProperty("size", doc.getSize()); SimpleSession.getSession().save(); } /** * 列出指定节点下面的所有节点 * @param node * @return list<Node> * @throws Exception */ public List<String> getNodes(Node node) throws Exception{ List<String> list = new ArrayList<String>(); NodeIterator ni = node.getNodes(); while(ni.hasNext()){ Node n = ni.nextNode(); if(!n.isCheckedOut()) n.checkout(); n.addMixin("mix:versionable");//加入版本控制 n.addMixin("mix:referenceable"); list.add(n.getUUID()); } return list; } /** * 删除指定节点下面所有节点 * @param node * @throws Exception */ public void removeNodes(Node node) throws Exception{ List<String> list = getNodes(node); for(String n:list){ if(SimpleSession.getSession().getNodeByUUID(n).isCheckedOut()){ if(!"jcr:system".equals(SimpleSession.getSession().getNodeByUUID(n).getName())){//系统节点不可删除 SimpleSession.getSession().getNodeByUUID(n).remove(); SimpleSession.getSession().save(); } } else{ SimpleSession.getSession().getNodeByUUID(n).checkout(); SimpleSession.getSession().getNodeByUUID(n).remove(); SimpleSession.getSession().save(); } } } /** * 获取指定节点下面所有属性名 * @param node * @return * @throws Exception */ public List<String> getProperties(Node node) throws Exception{ List<String> list = new ArrayList<String>(); PropertyIterator pi = node.getProperties(); while(pi.hasNext()){ Property p = pi.nextProperty(); list.add(p.getName()); } return list; } /** * 获取指定属性名的值 * @param pName * @return String * @throws Exception */ public String getPropertyValue(Node node,String pName) throws Exception{ return node.getProperty(pName).getString(); } public InputStream getPropertyValue(Node node,String pName,String fis) throws Exception{ return node.getProperty(pName).getStream(); } /** * 修改某一指定文档,不记录版本号 */ public void modifyDocumentWithNoVersion(Node node,Document doc) throws Exception{ node.setProperty("name", doc.getName()); node.setProperty("type", doc.getType()); node.setProperty("content", new FileInputStream(doc.getFile())); node.setProperty("size", doc.getSize()); SimpleSession.getSession().save(); } /** * 修改某一指定文档,记录版本号 */ public void modifyDocumentWithVersion(Node node,Document doc) throws Exception{ node.addMixin("mix:versionable"); node.setProperty("name", doc.getName()); node.setProperty("type", doc.getType()); node.setProperty("content", new FileInputStream(doc.getFile())); node.setProperty("size", doc.getSize()); SimpleSession.getSession().save(); node.checkin(); } /** * 获取当前节点下面的所有版本号 */ public List<String> getVersions(Node node) throws Exception{ List<String> list = new ArrayList<String>(); VersionIterator vi = node.getVersionHistory().getAllVersions(); while(vi.hasNext()){ Version v = vi.nextVersion(); list.add(v.getName()); } return list; } /** * 将指定文档/节点恢复到指定的版本号 */ public void restore(Node node,String vName)throws Exception{ node.restore(vName, true); } /** * 将指定文档上锁 */ public void lock(Node node,boolean isDeep,boolean isSessionScope) throws Exception{ node.lock(isDeep, isSessionScope); } /** * 将指定文档解锁 */ public void unlock(Node node) throws Exception{ node.unlock(); } /** * 使用XPATH检索文档节点 */ public List<String> queryByXpath(String xPath) throws Exception{ List<String> list = new ArrayList<String>(); QueryManager qm = SimpleSession.getSession().getWorkspace().getQueryManager(); Query query = qm.createQuery(xPath,Query.XPATH); QueryResult qr = query.execute(); NodeIterator ni = qr.getNodes(); while(ni.hasNext()){ Node n = ni.nextNode(); list.add(n.getUUID()); } return list; } /** * 使用SQL语句检索文档节点 */ public List<String> queryBySql(String sql) throws Exception{ List<String> list = new ArrayList<String>(); QueryManager qm = SimpleSession.getSession().getWorkspace().getQueryManager(); Query query = qm.createQuery(sql,Query.SQL); QueryResult qr = query.execute(); NodeIterator ni = qr.getNodes(); while(ni.hasNext()){ Node n = ni.nextNode(); list.add(n.getUUID()); } return list; } public String getUUID(Node node) throws Exception{ return node.getUUID(); } }