001/*
002 * (C) Copyright 2017 Nuxeo (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 *     Funsho David
018 *
019 */
020
021package org.nuxeo.ecm.automation.core.impl.adapters.helper;
022
023import org.nuxeo.ecm.automation.OperationContext;
024import org.nuxeo.ecm.automation.TypeAdaptException;
025import org.nuxeo.ecm.automation.core.scripting.Expression;
026import org.nuxeo.ecm.automation.core.scripting.Scripting;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentRef;
029import org.nuxeo.ecm.core.api.IdRef;
030import org.nuxeo.ecm.core.api.PathRef;
031
032/**
033 * Helper for type adapters
034 *
035 * @since 9.1
036 */
037public class TypeAdapterHelper {
038
039    /**
040     * Create document reference from an id or a path.
041     *
042     * @return the document reference
043     */
044    public static DocumentRef createDocumentRef(String value) {
045        if (value.startsWith("/")) {
046            return new PathRef(value);
047        }
048        // value could be of the form "repositoryName:docId|docPath" if so, remove the repositoryName
049        int index = value.indexOf(":");
050        return index != -1 ? createDocumentRef(value.substring(index + 1)) : new IdRef(value);
051    }
052
053    /**
054     * Create a document reference from an expression, an id or a path.
055     *
056     * @return the document reference
057     */
058    public static Object createDocumentRefOrExpression(String value) {
059        if (value.startsWith(".")) {
060            return Scripting.newExpression("Document.resolvePathAsRef(\"" + value + "\")");
061        } else {
062            return createDocumentRef(value);
063        }
064    }
065
066    /**
067     * Create a document reference from its path
068     *
069     * @param ctx the operation context
070     * @param value the document path
071     * @return the document reference
072     */
073    public static DocumentRef createDocumentRef(OperationContext ctx, String value) throws TypeAdaptException {
074        Object obj = createDocumentRefOrExpression(value);
075        if (obj instanceof DocumentRef) {
076            return (DocumentRef) obj;
077        } else if (obj instanceof Expression) {
078            obj = ((Expression) obj).eval(ctx);
079            if (obj instanceof DocumentModel) {
080                return ((DocumentModel) obj).getRef();
081            } else if (obj instanceof DocumentRef) {
082                return (DocumentRef) obj;
083            }
084            throw new TypeAdaptException(String.format("Cannot adapt value '%s' to a DocumentRef instance", value));
085        } else {
086            throw new RuntimeException(String.format("Unhandled value: %s", value));
087        }
088    }
089
090    /**
091     * Create a document model from its path
092     *
093     * @param ctx the operation context
094     * @param value the document path
095     * @return the document model
096     */
097    public static DocumentModel createDocumentModel(OperationContext ctx, String value) throws TypeAdaptException {
098        DocumentRef docRef = createDocumentRef(ctx, value);
099        return createDocumentModel(ctx, docRef);
100    }
101
102    /**
103     * Create a document model from its reference
104     *
105     * @param ctx the operation context
106     * @param docRef the document reference
107     * @return the document model
108     */
109    public static DocumentModel createDocumentModel(OperationContext ctx, DocumentRef docRef) throws TypeAdaptException {
110        return ctx.getCoreSession().getDocument(docRef);
111    }
112}