package my.company.client.controllers; import org.jboss.solder.logging.internal.Logger; import org.modeshape.web.jcr.rest.client.IRestClient; import org.modeshape.web.jcr.rest.client.Status; import org.modeshape.web.jcr.rest.client.domain.QueryRow; import org.modeshape.web.jcr.rest.client.domain.Repository; import org.modeshape.web.jcr.rest.client.domain.Server; import org.modeshape.web.jcr.rest.client.domain.Workspace; import org.modeshape.web.jcr.rest.client.json.FileNode; import org.modeshape.web.jcr.rest.client.json.JsonRestClient; import javax.enterprise.context.RequestScoped; import javax.inject.Named; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeModel; import java.io.File; import java.io.IOException; import java.util.List; @Named("researcher") @RequestScoped public class ResearchController { public static final String PATH_SEPARATOR = "/"; Logger logger = Logger.getLogger(ResearchController.class); private final Workspace workspace; private final IRestClient restClient; public ResearchController() { Server server = new Server("http://localhost:8080/rest-server", "user", "password"); Repository repository = new Repository("MyRepo", server); workspace = new Workspace("workspace1", repository); restClient = new JsonRestClient(); } public TreeModel getNodesTreeModel() { return new DefaultTreeModel(getTreeNode("", 1)); } public void addNode(String nodeName) { File file = new File(nodeName); try { file.createNewFile(); } catch (IOException e) { logger.error(e); } Status status = restClient.publish(workspace, "/uploads/", file); file.delete(); if (status.isError()) { logger.error(status.getMessage(), status.getException()); } } private DefaultMutableTreeNode getTreeNode(String path, int depth) { DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(getLastPathSegment(path)); List query = null; try { query = restClient.query(workspace, "JCR-SQL2", "SELECT [jcr:path] FROM [nt:base] " + "WHERE PATH([nt:base]) LIKE '" + path + "/%' " + "AND DEPTH([nt:base]) = CAST(" + depth + " AS LONG)"); } catch (Exception e) { logger.error(e); } if (query != null) { for (QueryRow row : query) { treeNode.add(getTreeNode(row.getValue("jcr:path").toString(), depth + 1)); } } return treeNode; } /** * /a/b/c => c */ private String getLastPathSegment(String path) { if (path == null) { return null; } if (!path.startsWith(PATH_SEPARATOR)) { path = PATH_SEPARATOR + path; } while (path.endsWith(PATH_SEPARATOR)) { path = path.substring(0, path.length() - PATH_SEPARATOR.length()); } if (!path.contains(PATH_SEPARATOR)) { return path; } else { int idx = path.lastIndexOf(PATH_SEPARATOR); return path.substring(idx + PATH_SEPARATOR.length()); } } /** * /a/b/c => /a/b */ private String getPathExcludeLastSegment(String path) { if (path == null) { return null; } if (!path.startsWith(PATH_SEPARATOR)) { path = PATH_SEPARATOR + path; } while (path.endsWith(PATH_SEPARATOR)) { path = path.substring(0, path.length() - PATH_SEPARATOR.length()); } if (!path.contains(PATH_SEPARATOR)) { return path; } else { int idx = path.lastIndexOf(PATH_SEPARATOR); if (idx == 0) { return PATH_SEPARATOR; } return path.substring(0, idx); } } public void removeNode(String pathToNode) { File file = new File("my_file"); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } try { logger.info("\nURL: "+(new FileNode(workspace,"/uploads",file).getUrl())); } catch (Exception e) { e.printStackTrace(); } Status status = restClient.unpublish(workspace, "/uploads", file); if (status.isError()) { logger.error("removeNode", status.getException()); } if (status.isInfo()) { logger.info("not found!"); } } public TreeModel executeQuery(String queryText) { if (queryText == null || queryText.isEmpty()) { return new DefaultTreeModel(new DefaultMutableTreeNode("no query - no data")); } DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode("result of " + queryText); List query = null; try { query = restClient.query(workspace, "JCR-SQL2", queryText); } catch (Exception e) { logger.error(e); } if (query != null) { for (QueryRow row : query) { if (row.getColumnNames().contains("jcr:name")) { treeNode.add(new DefaultMutableTreeNode(row.getValue("jcr:name"))); } else { treeNode.add(new DefaultMutableTreeNode("no jcr:name column in a row")); } } } return new DefaultTreeModel(treeNode); } }