001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
016 *
017 * $Id: MailTreeHelper.java 57899 2008-10-07 12:02:44Z atchertchian $
018 */
019
020package org.nuxeo.ecm.platform.routing.core.persistence;
021
022import java.text.SimpleDateFormat;
023import java.util.Date;
024
025import org.nuxeo.common.utils.IdUtils;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentRef;
029import org.nuxeo.ecm.core.api.PathRef;
030
031/**
032 * Helper to create tree structure based on date
033 * <p>
034 * Emails and Mail envelopes are created within trees of folder.
035 *
036 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
037 */
038public class TreeHelper {
039
040    public static final String TITLE_PROPERTY_NAME = "dc:title";
041
042    public static final String DELETED_STATE = "deleted";
043
044    /**
045     * Find or create a set of folders representing the date hierarchy
046     *
047     * @return the last child created (day)
048     */
049    public static DocumentModel getOrCreateDateTreeFolder(CoreSession session, DocumentModel root, Date date,
050            String folderType) {
051        String subPath = new SimpleDateFormat("yyyy/MM/dd").format(date);
052        return getOrCreatePath(session, root, subPath, folderType);
053    }
054
055    public static DocumentModel getOrCreatePath(CoreSession session, DocumentModel root, String subPath,
056            String folderType) {
057        String[] pathSplit = subPath.split("/");
058        String parentPath = root.getPathAsString();
059        DocumentModel child = root;
060        for (String id : pathSplit) {
061            child = getOrCreate(session, parentPath, id, folderType);
062            parentPath = child.getPathAsString();
063        }
064        return child;
065    }
066
067    public static synchronized DocumentModel getOrCreate(CoreSession session, String rootPath, String id,
068            String folderType) {
069        String path = String.format("%s/%s", rootPath, id);
070        DocumentRef pathRef = new PathRef(path);
071        boolean exists = session.exists(pathRef);
072        if (exists) {
073            DocumentModel existing = session.getDocument(pathRef);
074            if (!DELETED_STATE.equals(existing.getCurrentLifeCycleState())) {
075                return existing;
076            }
077        }
078        // create it
079        DocumentModel newDocument = session.createDocumentModel(rootPath, IdUtils.generateId(id), folderType);
080        newDocument.setPropertyValue(TITLE_PROPERTY_NAME, id);
081        newDocument = session.createDocument(newDocument);
082        return newDocument;
083    }
084
085}