001/* 002 * (C) Copyright 2006-2016 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 */ 019package org.nuxeo.ecm.platform.routing.core.persistence; 020 021import java.text.SimpleDateFormat; 022import java.util.Date; 023 024import org.nuxeo.common.utils.IdUtils; 025import org.nuxeo.ecm.core.api.CoreSession; 026import org.nuxeo.ecm.core.api.DocumentModel; 027import org.nuxeo.ecm.core.api.DocumentRef; 028import org.nuxeo.ecm.core.api.PathRef; 029 030/** 031 * Helper to create tree structure based on date 032 * <p> 033 * Emails and Mail envelopes are created within trees of folder. 034 * 035 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 036 */ 037public class TreeHelper { 038 039 public static final String TITLE_PROPERTY_NAME = "dc:title"; 040 041 public static final String DELETED_STATE = "deleted"; 042 043 /** 044 * Find or create a set of folders representing the date hierarchy 045 * 046 * @return the last child created (day) 047 */ 048 public static DocumentModel getOrCreateDateTreeFolder(CoreSession session, DocumentModel root, Date date, 049 String folderType) { 050 String subPath = new SimpleDateFormat("yyyy/MM/dd").format(date); 051 return getOrCreatePath(session, root, subPath, folderType); 052 } 053 054 public static DocumentModel getOrCreatePath(CoreSession session, DocumentModel root, String subPath, 055 String folderType) { 056 String[] pathSplit = subPath.split("/"); 057 String parentPath = root.getPathAsString(); 058 DocumentModel child = root; 059 for (String id : pathSplit) { 060 child = getOrCreate(session, parentPath, id, folderType); 061 parentPath = child.getPathAsString(); 062 } 063 return child; 064 } 065 066 public static synchronized DocumentModel getOrCreate(CoreSession session, String rootPath, String id, 067 String folderType) { 068 String path = String.format("%s/%s", rootPath, id); 069 DocumentRef pathRef = new PathRef(path); 070 boolean exists = session.exists(pathRef); 071 if (exists) { 072 DocumentModel existing = session.getDocument(pathRef); 073 if (!DELETED_STATE.equals(existing.getCurrentLifeCycleState())) { 074 return existing; 075 } 076 } 077 // create it 078 DocumentModel newDocument = session.createDocumentModel(rootPath, IdUtils.generateId(id, "-", true, 24), 079 folderType); 080 newDocument.setPropertyValue(TITLE_PROPERTY_NAME, id); 081 newDocument = session.createDocument(newDocument); 082 return newDocument; 083 } 084 085}