001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thierry Delprat
016 */
017package org.nuxeo.apidoc.adapters;
018
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021import org.nuxeo.apidoc.api.BaseNuxeoArtifact;
022import org.nuxeo.apidoc.api.NuxeoArtifact;
023import org.nuxeo.apidoc.snapshot.DistributionSnapshot;
024import org.nuxeo.common.utils.IdUtils;
025import org.nuxeo.common.utils.Path;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.PathRef;
029import org.nuxeo.ecm.core.api.PropertyException;
030
031import java.util.Collections;
032import java.util.List;
033
034public abstract class BaseNuxeoArtifactDocAdapter extends BaseNuxeoArtifact {
035
036    protected static final Log log = LogFactory.getLog(BaseNuxeoArtifactDocAdapter.class);
037
038    protected final DocumentModel doc;
039
040    protected static final ThreadLocal<CoreSession> localCoreSession = new ThreadLocal<CoreSession>();
041
042    public static void setLocalCoreSession(CoreSession session) {
043        localCoreSession.set(session);
044    }
045
046    public static void releaseLocalCoreSession() {
047        localCoreSession.remove();
048    }
049
050    protected BaseNuxeoArtifactDocAdapter(DocumentModel doc) {
051        this.doc = doc;
052    }
053
054    protected static String computeDocumentName(String name) {
055        return IdUtils.generateId(name, "-", true, 500);
056    }
057
058    protected static String getRootPath(CoreSession session, String basePath, String suffix) {
059        PathRef rootRef = new PathRef(basePath);
060        if (session.exists(rootRef)) {
061            Path path = new Path(basePath).append(suffix);
062            rootRef = new PathRef(path.toString());
063            if (session.exists(rootRef)) {
064                return path.toString();
065            } else {
066                DocumentModel root = session.createDocumentModel("Folder");
067                root.setPathInfo(basePath, suffix);
068                root = session.createDocument(root);
069                return root.getPathAsString();
070            }
071        }
072        return null;
073    }
074
075    @Override
076    public int hashCode() {
077        return doc.getId().hashCode();
078    }
079
080    public DocumentModel getDoc() {
081        return doc;
082    }
083
084    protected CoreSession getCoreSession() {
085        CoreSession session = null;
086        if (doc != null) {
087            session = doc.getCoreSession();
088        }
089        if (session == null) {
090            session = localCoreSession.get();
091        }
092        return session;
093    }
094
095    protected <T> T getParentNuxeoArtifact(Class<T> artifactClass) {
096        List<DocumentModel> parents = getCoreSession().getParentDocuments(doc.getRef());
097        for (DocumentModel parent : parents) {
098            T result = parent.getAdapter(artifactClass);
099            if (result != null) {
100                return result;
101            }
102        }
103        log.error("Parent artifact not found ");
104        return null;
105    }
106
107    protected String safeGet(String xPath) {
108        return safeGet(String.class, xPath, null);
109    }
110
111    protected String safeGet(String xPath, String defaultValue) {
112        return safeGet(String.class, xPath, defaultValue);
113    }
114
115    @SuppressWarnings("unchecked")
116    protected <T> T safeGet(Class<T> typ, String xPath, Object defaultValue) {
117        try {
118            T value = (T) doc.getPropertyValue(xPath);
119            return value;
120        } catch (PropertyException e) {
121            log.error("Error while getting property " + xPath, e);
122            if (defaultValue == null) {
123                return null;
124            }
125            return (T) defaultValue;
126        }
127    }
128
129    @Override
130    public String getHierarchyPath() {
131        List<DocumentModel> parents = getCoreSession().getParentDocuments(doc.getRef());
132        Collections.reverse(parents);
133
134        String path = "";
135        for (DocumentModel doc : parents) {
136            if (doc.getType().equals(DistributionSnapshot.TYPE_NAME)) {
137                break;
138            }
139            if (doc.getType().equals(DistributionSnapshot.CONTAINER_TYPE_NAME)) {
140                // skip containers
141                continue;
142            }
143            NuxeoArtifact item = doc.getAdapter(NuxeoArtifact.class);
144
145            path = "/" + item.getId() + path;
146        }
147        return path;
148    }
149
150}