001/* 002 * (C) Copyright 2006-2010 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 * Thierry Delprat 018 */ 019package org.nuxeo.apidoc.adapters; 020 021import java.util.Collections; 022import java.util.List; 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026import org.nuxeo.apidoc.api.BaseNuxeoArtifact; 027import org.nuxeo.apidoc.api.NuxeoArtifact; 028import org.nuxeo.apidoc.snapshot.DistributionSnapshot; 029import org.nuxeo.common.utils.IdUtils; 030import org.nuxeo.common.utils.Path; 031import org.nuxeo.ecm.core.api.CoreSession; 032import org.nuxeo.ecm.core.api.DocumentModel; 033import org.nuxeo.ecm.core.api.PathRef; 034import org.nuxeo.ecm.core.api.PropertyException; 035 036public abstract class BaseNuxeoArtifactDocAdapter extends BaseNuxeoArtifact { 037 038 protected static final Log log = LogFactory.getLog(BaseNuxeoArtifactDocAdapter.class); 039 040 protected final DocumentModel doc; 041 042 protected static final ThreadLocal<CoreSession> localCoreSession = new ThreadLocal<>(); 043 044 public static void setLocalCoreSession(CoreSession session) { 045 localCoreSession.set(session); 046 } 047 048 public static void releaseLocalCoreSession() { 049 localCoreSession.remove(); 050 } 051 052 protected BaseNuxeoArtifactDocAdapter(DocumentModel doc) { 053 this.doc = doc; 054 } 055 056 protected static String computeDocumentName(String name) { 057 return IdUtils.generateId(name, "-", true, 500); 058 } 059 060 protected static String getRootPath(CoreSession session, String basePath, String suffix) { 061 PathRef rootRef = new PathRef(basePath); 062 if (session.exists(rootRef)) { 063 Path path = new Path(basePath).append(suffix); 064 rootRef = new PathRef(path.toString()); 065 if (session.exists(rootRef)) { 066 return path.toString(); 067 } else { 068 DocumentModel root = session.createDocumentModel("Folder"); 069 root.setPathInfo(basePath, suffix); 070 root = session.createDocument(root); 071 return root.getPathAsString(); 072 } 073 } 074 return null; 075 } 076 077 @Override 078 public int hashCode() { 079 return doc.getId().hashCode(); 080 } 081 082 public DocumentModel getDoc() { 083 return doc; 084 } 085 086 protected CoreSession getCoreSession() { 087 CoreSession session = null; 088 if (doc != null) { 089 session = doc.getCoreSession(); 090 } 091 if (session == null) { 092 session = localCoreSession.get(); 093 } 094 return session; 095 } 096 097 protected <T> T getParentNuxeoArtifact(Class<T> artifactClass) { 098 List<DocumentModel> parents = getCoreSession().getParentDocuments(doc.getRef()); 099 for (DocumentModel parent : parents) { 100 T result = parent.getAdapter(artifactClass); 101 if (result != null) { 102 return result; 103 } 104 } 105 log.error("Parent artifact not found "); 106 return null; 107 } 108 109 protected String safeGet(String xPath) { 110 return safeGet(String.class, xPath, null); 111 } 112 113 protected String safeGet(String xPath, String defaultValue) { 114 return safeGet(String.class, xPath, defaultValue); 115 } 116 117 @SuppressWarnings("unchecked") 118 protected <T> T safeGet(Class<T> typ, String xPath, Object defaultValue) { 119 try { 120 T value = (T) doc.getPropertyValue(xPath); 121 return value; 122 } catch (PropertyException e) { 123 log.error("Error while getting property " + xPath, e); 124 if (defaultValue == null) { 125 return null; 126 } 127 return (T) defaultValue; 128 } 129 } 130 131 @Override 132 public String getHierarchyPath() { 133 List<DocumentModel> parents = getCoreSession().getParentDocuments(doc.getRef()); 134 Collections.reverse(parents); 135 136 String path = ""; 137 for (DocumentModel doc : parents) { 138 if (doc.getType().equals(DistributionSnapshot.TYPE_NAME)) { 139 break; 140 } 141 if (doc.getType().equals(DistributionSnapshot.CONTAINER_TYPE_NAME)) { 142 // skip containers 143 continue; 144 } 145 NuxeoArtifact item = doc.getAdapter(NuxeoArtifact.class); 146 147 path = "/" + item.getId() + path; 148 } 149 return path; 150 } 151 152}