001package org.kuali.ole;
002
003import org.kuali.ole.docstore.model.xmlpojo.config.DocumentCategory;
004import org.kuali.ole.docstore.model.xmlpojo.config.DocumentConfig;
005import org.kuali.ole.docstore.model.xmlpojo.config.DocumentType;
006
007import javax.jcr.Node;
008import javax.jcr.Session;
009
010/**
011 * Created by IntelliJ IDEA.
012 * User: pvsubrah
013 * Date: 1/10/12
014 * Time: 11:02 AM
015 * To change this template use File | Settings | File Templates.
016 */
017public class DocumentStoreModelInitiallizer {
018
019        /*public void init() throws Exception {
020            RepositoryBrowser repositoryBrowser = new RepositoryBrowser();
021            List<OleDocStoreData> oleDocStoreDatas = repositoryBrowser.browseDataSetup();
022            RepositoryManager repositoryManager = RepositoryManager.getRepositoryManager();
023            Session session = repositoryManager.getSession("documentStoreModelInitiallizer","init");
024            Node rootNode = session.getRootNode();
025
026            for (Iterator<OleDocStoreData> iterator = oleDocStoreDatas.iterator(); iterator.hasNext(); ) {
027                OleDocStoreData oleDocStoreData = iterator.next();
028                String category = oleDocStoreData.getCategory();
029                Node categoryNode;
030                if (!rootNode.hasNode(category)) {
031                    categoryNode = rootNode.addNode(category, "nt:unstructured");
032                    categoryNode.setProperty("nodeType", "folder");
033                    categoryNode.addMixin("mix:referenceable");
034                } else {
035                    categoryNode = rootNode.getNode(category);
036                }
037                List<String> doctypes = oleDocStoreData.getDoctypes();
038                for (Iterator<String> stringIterator = doctypes.iterator(); stringIterator.hasNext(); ) {
039                    String docType = stringIterator.next();
040                    Node typeNode;
041                    if (!categoryNode.hasNode(docType)) {
042                        typeNode = categoryNode.addNode(docType, "nt:unstructured");
043                        typeNode.setProperty("nodeType", "folder");
044                        typeNode.addMixin("mix:referenceable");
045                    } else {
046                        typeNode = categoryNode.getNode(docType);
047                    }
048                }
049            }
050            session.save();
051            repositoryManager.logout(session);
052        }*/
053
054    public void init() throws Exception {
055        RepositoryManager repositoryManager = RepositoryManager.getRepositoryManager();
056        Session session = repositoryManager.getSession("documentStoreModelInitiallizer", "init");
057        Node rootNode = session.getRootNode();
058        DocumentConfig documentConfig = DocumentConfig.getInstance();
059        for (DocumentCategory documentCategory : documentConfig.getDocumentCategories()) {
060            String category = documentCategory.getId();
061            Node categoryNode;
062            if (!rootNode.hasNode(category)) {
063                categoryNode = rootNode.addNode(category, "nt:unstructured");
064                categoryNode.setProperty("nodeType", "folder");
065                categoryNode.addMixin("mix:referenceable");
066            } else {
067                categoryNode = rootNode.getNode(category);
068            }
069            for (DocumentType documentType : documentCategory.getDocumentTypes()) {
070                String docType = documentType.getId();
071                if (!((docType.equals("item") || docType.equals("holdings")))) {
072                    Node typeNode;
073                    if (!categoryNode.hasNode(docType)) {
074                        typeNode = categoryNode.addNode(docType, "nt:unstructured");
075                        typeNode.setProperty("nodeType", "folder");
076                        typeNode.addMixin("mix:referenceable");
077                    } else {
078                        typeNode = categoryNode.getNode(docType);
079                    }
080                }
081            }
082        }
083        session.save();
084        repositoryManager.logout(session);
085    }
086}