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