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 its path
041     *
042     * @param value the document path
043     * @return the document reference
044     */
045    public static DocumentRef createDocumentRef(String value) {
046        return value.startsWith("/") ? new PathRef(value) : new IdRef(value);
047    }
048
049    /**
050     * Create a document reference from its path
051     * 
052     * @param value the document path
053     * @return the document reference
054     */
055    public static Object createDocumentRefOrExpression(String value) {
056        if (value.startsWith(".")) {
057            return Scripting.newExpression("Document.resolvePathAsRef(\"" + value + "\")");
058        } else {
059            return value.startsWith("/") ? new PathRef(value) : new IdRef(value);
060        }
061    }
062
063    /**
064     * Create a document reference from its path
065     * 
066     * @param ctx the operation context
067     * @param value the document path
068     * @return the document reference
069     */
070    public static DocumentRef createDocumentRef(OperationContext ctx, String value) throws TypeAdaptException {
071        Object obj = createDocumentRefOrExpression(value);
072        if (obj instanceof DocumentRef) {
073            return (DocumentRef) obj;
074        } else if (obj instanceof Expression) {
075            if (ctx != null) {
076                obj = ((Expression) obj).eval(ctx);
077            }
078            if (obj instanceof DocumentModel) {
079                return ((DocumentModel) obj).getRef();
080            } else if (obj instanceof DocumentRef) {
081                return (DocumentRef) obj;
082            }
083            throw new TypeAdaptException(String.format("Cannot adapt value '%s' to a DocumentRef instance", value));
084        } else {
085            throw new RuntimeException(String.format("Unhandled value: %s", value));
086        }
087    }
088
089    /**
090     * Create a document model from its path
091     *
092     * @param ctx the operation context
093     * @param value the document path
094     * @return the document model
095     */
096    public static DocumentModel createDocumentModel(OperationContext ctx, String value) throws TypeAdaptException {
097        DocumentRef docRef = createDocumentRef(ctx, value);
098        return createDocumentModel(ctx, docRef);
099    }
100
101    /**
102     * Create a document model from its reference
103     *
104     * @param ctx the operation context
105     * @param docRef the document reference
106     * @return the document model
107     */
108    public static DocumentModel createDocumentModel(OperationContext ctx, DocumentRef docRef) throws TypeAdaptException {
109        return ctx.getCoreSession().getDocument(docRef);
110    }
111}