001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
018 *
019 * $Id: MailTreeHelper.java 57899 2008-10-07 12:02:44Z atchertchian $
020 */
021
022package org.nuxeo.ecm.platform.routing.core.persistence;
023
024import java.text.SimpleDateFormat;
025import java.util.Date;
026
027import org.nuxeo.common.utils.IdUtils;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.DocumentRef;
031import org.nuxeo.ecm.core.api.PathRef;
032
033/**
034 * Helper to create tree structure based on date
035 * <p>
036 * Emails and Mail envelopes are created within trees of folder.
037 *
038 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
039 */
040public class TreeHelper {
041
042    public static final String TITLE_PROPERTY_NAME = "dc:title";
043
044    public static final String DELETED_STATE = "deleted";
045
046    /**
047     * Find or create a set of folders representing the date hierarchy
048     *
049     * @return the last child created (day)
050     */
051    public static DocumentModel getOrCreateDateTreeFolder(CoreSession session, DocumentModel root, Date date,
052            String folderType) {
053        String subPath = new SimpleDateFormat("yyyy/MM/dd").format(date);
054        return getOrCreatePath(session, root, subPath, folderType);
055    }
056
057    public static DocumentModel getOrCreatePath(CoreSession session, DocumentModel root, String subPath,
058            String folderType) {
059        String[] pathSplit = subPath.split("/");
060        String parentPath = root.getPathAsString();
061        DocumentModel child = root;
062        for (String id : pathSplit) {
063            child = getOrCreate(session, parentPath, id, folderType);
064            parentPath = child.getPathAsString();
065        }
066        return child;
067    }
068
069    public static synchronized DocumentModel getOrCreate(CoreSession session, String rootPath, String id,
070            String folderType) {
071        String path = String.format("%s/%s", rootPath, id);
072        DocumentRef pathRef = new PathRef(path);
073        boolean exists = session.exists(pathRef);
074        if (exists) {
075            DocumentModel existing = session.getDocument(pathRef);
076            if (!DELETED_STATE.equals(existing.getCurrentLifeCycleState())) {
077                return existing;
078            }
079        }
080        // create it
081        DocumentModel newDocument = session.createDocumentModel(rootPath, IdUtils.generateId(id), folderType);
082        newDocument.setPropertyValue(TITLE_PROPERTY_NAME, id);
083        newDocument = session.createDocument(newDocument);
084        return newDocument;
085    }
086
087}