jcrserviceimpl.java

来自「GridSphere 门户 提供一个基于 portlet 的高级开放源代码门户。」· Java 代码 · 共 468 行 · 第 1/2 页

JAVA
468
字号
            }        } catch (ContentException e) {            output = "Content " + nodename + "not found!";        }        return output;    }    /**     * Checks the length of the String     *     * @param s       String to be checked     * @param length  length which the result should have     * @param filler  String to be appended/prepended     * @param prepend should the filler prependend (otherwise append)     * @return changed string     */    private String checkLength(String s, int length, String filler, boolean prepend) {        if (s.length() != length) {            for (int i = 1; i < length; i++) {                if (prepend) {                    s = filler + s;                } else {                    s = s + filler;                }            }        }        return s;    }    public void backupContent(String fullPathBackupDir) throws NamingException, RepositoryException, IOException {        File bDir = new File(fullPathBackupDir);        if (!bDir.exists()) {            bDir.mkdirs();        }        if (bDir.exists() && bDir.isDirectory()) {            Calendar c = new GregorianCalendar();            int m = c.get(Calendar.MONTH) + 1;            String month = checkLength(m + "", 2, "0", true);            String backupName = "PortalContentBackup-" + c.get(Calendar.YEAR) + "." + month + "." +                    checkLength(c.get(Calendar.DAY_OF_MONTH) + "", 2, "0", true) + "-" +                    checkLength(c.get(Calendar.HOUR_OF_DAY) + "", 2, "0", true) + ":" +                    checkLength(c.get(Calendar.MINUTE) + "", 2, "0", true) + ".xml";            String filename = fullPathBackupDir + File.separator + backupName;            Session s = null;            try {                s = getSession();                FileOutputStream x = new FileOutputStream(new File(filename));                s.exportSystemView("/" + JCRNode.GS_ROOT_NODE_NAME, x, false, false);            } catch (RepositoryException e) {                log.error("Repository Exception.");                throw new RepositoryException(e);            } catch (NamingException e) {                log.error("Naming Excpetion.");                throw new NamingException();            } catch (FileNotFoundException e) {                log.error("FileNotFoundException. " + filename);                throw new FileNotFoundException();            } catch (IOException e) {                log.error("IOException.");                throw new IOException("Backup failed. IOException.");            } finally {                if (s != null) s.logout();            }        } else {            log.error("ContentBackup failed '" + fullPathBackupDir + "' is NOT a directory (most likely a file)");            throw new IOException("Backup failed. Path " + fullPathBackupDir + " exists and is not a directoy.");        }    }    public void importContent(String fullPathFileName) throws NamingException, RepositoryException, IOException {        Session s = null;        try {            s = getSession();            FileInputStream x = new FileInputStream(new File(fullPathFileName));            s.importXML("/", x, javax.jcr.ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);            x.close();            s.save();        } catch (RepositoryException e) {            log.error("Repository Exception.");            throw new RepositoryException(e);        } catch (NamingException e) {            log.error("Naming Excpetion.");            throw new NamingException();        } catch (FileNotFoundException e) {            log.error("FileNotFoundException. " + fullPathFileName);            throw new FileNotFoundException();        } catch (IOException e) {            log.error("IOException.");            throw new IOException("Backup failed. IOException.");        } finally {            if (s != null) s.logout();        }    }    public Node getGridSphereRootNode(Session s) throws RepositoryException {        Node n = s.getRootNode();        Node gsRoot = n.getNode(JCRNode.GS_ROOT_NODE_NAME);        return gsRoot;    }    public ContentDocument getDocument(String path) throws ContentException {        Session session = null;        ContentDocument result = null;        try {            session = getSession();            Node node = (Node) session.getItem(JCRNode.GS_ROOT_CONTENTDOCUMENT_PATH + "/" + path);            result = createDocument(node);        } catch (RepositoryException e) {            throw new ContentException("JCR Repository Error or Node does not exsit.");        } catch (NamingException e) {            throw new ContentException("JCR Naming Error or Node does not exist.");        } finally {            if (session != null) session.logout();        }        return result;    }    public void saveDocument(ContentDocument document) throws ContentException {        Session session = null;        Node node = null;        try {            session = getSession();            if (document.getUuid() == null) {                Node parentNode = (Node) session.getItem(document.getParentPath());                node = parentNode.addNode(document.getTitle());                node.addMixin("mix:referenceable"); // make it to have uuid's            } else {                node = session.getNodeByUUID(document.getUuid());            }            node.setProperty(JCRNode.CONTENT, document.getContent());            node.setProperty(JCRNode.TITLE, document.getTitle());            session.save();        } catch (RepositoryException e) {            throw new ContentException("JCR Repository Error.");        } catch (NamingException e) {            throw new ContentException("JCR Naming Error.");        } finally {            if (session != null) session.logout();        }    }    protected Node getGsDocRootNode(Session s) throws RepositoryException {        Node rootNode = s.getRootNode();        Node result = rootNode.getNode(JCRNode.GS_ROOT_NODE_NAME + "/" + JCRNode.GS_ROOT_CONTENTDOCUMENT_NAME);        return result;    }    protected ContentDocument createDocument(Node n) throws ContentException {        ContentDocument doc = new ContentDocument();        try {            doc.setContent(n.getProperty(JCRNode.CONTENT).getString());            doc.setTitle(n.getProperty(JCRNode.TITLE).getString());            doc.setUuid(n.getUUID());        } catch (RepositoryException e) {            throw new ContentException("Could not create document from node.");        }        return doc;    }    public List<ContentDocument> listChildContentDocuments(String path) throws ContentException {        Session session = null;        String childPath = JCRNode.GS_ROOT_CONTENTDOCUMENT_PATH + path;        SortedSet<ContentDocument> sorted = new TreeSet<ContentDocument>();        List<ContentDocument> result = new ArrayList<ContentDocument>();        try {            session = getSession();            Node node = (Node) session.getItem(childPath);            NodeIterator ni = node.getNodes();            while (ni.hasNext()) {                Node n = ni.nextNode();                sorted.add(createDocument(n));            }        } catch (RepositoryException e) {            throw new ContentException("JCR Repository Error.");        } catch (NamingException e) {            throw new ContentException("JCR Naming Error.");        } finally {            if (session != null) session.logout();        }        for (ContentDocument aSorted : sorted) {            result.add(aSorted);        }        return result;    }    public ContentDocument getDocumentByUUID(String uuid) throws ContentException {        Session session = null;        ContentDocument doc = null;        try {            session = getSession();            Node n = session.getNodeByUUID(uuid);            doc = createDocument(n);        } catch (RepositoryException e) {            throw new ContentException("JCR Repository Error.");        } catch (NamingException e) {            throw new ContentException("JCR Naming Error.");        } finally {            if (session != null) session.logout();        }        return doc;    }    public void removeDocument(ContentDocument doc) throws ContentException {        removeDocumentByUuid(doc.getUuid());    }    public void removeDocumentByUuid(String uuid) throws ContentException {        Session session = null;        try {            session = getSession();            Node n = session.getNodeByUUID(uuid);            n.remove();            session.save();        } catch (RepositoryException e) {            throw new ContentException("JCR Repository Error.");        } catch (NamingException e) {            throw new ContentException("JCR Naming Error.");        } finally {            if (session != null) session.logout();        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?