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; 029import org.nuxeo.ecm.core.api.trash.TrashService; 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 /** 043 * @deprecated since 10.3, use {@link TrashService} instead 044 */ 045 @Deprecated 046 public static final String DELETED_STATE = "deleted"; 047 048 /** 049 * Find or create a set of folders representing the date hierarchy 050 * 051 * @return the last child created (day) 052 */ 053 public static DocumentModel getOrCreateDateTreeFolder(CoreSession session, DocumentModel root, Date date, 054 String folderType) { 055 String subPath = new SimpleDateFormat("yyyy/MM/dd").format(date); 056 return getOrCreatePath(session, root, subPath, folderType); 057 } 058 059 public static DocumentModel getOrCreatePath(CoreSession session, DocumentModel root, String subPath, 060 String folderType) { 061 String[] pathSplit = subPath.split("/"); 062 String parentPath = root.getPathAsString(); 063 DocumentModel child = root; 064 for (String id : pathSplit) { 065 child = getOrCreate(session, parentPath, id, folderType); 066 parentPath = child.getPathAsString(); 067 } 068 return child; 069 } 070 071 public static synchronized DocumentModel getOrCreate(CoreSession session, String rootPath, String id, 072 String folderType) { 073 String path = String.format("%s/%s", rootPath, id); 074 DocumentRef pathRef = new PathRef(path); 075 boolean exists = session.exists(pathRef); 076 if (exists) { 077 DocumentModel existing = session.getDocument(pathRef); 078 if (!existing.isTrashed()) { 079 return existing; 080 } 081 } 082 // create it 083 DocumentModel newDocument = session.createDocumentModel(rootPath, IdUtils.generateId(id, "-", true, 24), 084 folderType); 085 newDocument.setPropertyValue(TITLE_PROPERTY_NAME, id); 086 newDocument = session.createDocument(newDocument); 087 return newDocument; 088 } 089 090}