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 if (ctx != null) { 079 obj = ((Expression) obj).eval(ctx); 080 } 081 if (obj instanceof DocumentModel) { 082 return ((DocumentModel) obj).getRef(); 083 } else if (obj instanceof DocumentRef) { 084 return (DocumentRef) obj; 085 } 086 throw new TypeAdaptException(String.format("Cannot adapt value '%s' to a DocumentRef instance", value)); 087 } else { 088 throw new RuntimeException(String.format("Unhandled value: %s", value)); 089 } 090 } 091 092 /** 093 * Create a document model from its path 094 * 095 * @param ctx the operation context 096 * @param value the document path 097 * @return the document model 098 */ 099 public static DocumentModel createDocumentModel(OperationContext ctx, String value) throws TypeAdaptException { 100 DocumentRef docRef = createDocumentRef(ctx, value); 101 return createDocumentModel(ctx, docRef); 102 } 103 104 /** 105 * Create a document model from its reference 106 * 107 * @param ctx the operation context 108 * @param docRef the document reference 109 * @return the document model 110 */ 111 public static DocumentModel createDocumentModel(OperationContext ctx, DocumentRef docRef) throws TypeAdaptException { 112 return ctx.getCoreSession().getDocument(docRef); 113 } 114}