001/* 002 * (C) Copyright 2016 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 * bstefanescu 018 */ 019package org.nuxeo.automation.scripting.internals; 020 021import java.io.Serializable; 022import java.util.Collection; 023import java.util.HashMap; 024import java.util.Map; 025import java.util.Set; 026 027import org.nuxeo.ecm.core.api.CoreSession; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.core.api.DocumentRef; 030import org.nuxeo.ecm.core.api.PathRef; 031import org.nuxeo.ecm.core.api.PropertyException; 032import org.nuxeo.ecm.core.api.model.Property; 033import org.nuxeo.ecm.core.schema.DocumentType; 034 035import jdk.nashorn.api.scripting.ScriptObjectMirror; 036 037/** 038 * Wrap a {@link DocumentModel} to expose in a pretty way more information to automation scripts. 039 * 040 * @since 8.4 041 */ 042public class DocumentScriptingWrapper extends HashMap<String, Object> { 043 044 private static final long serialVersionUID = 1L; 045 046 protected final CoreSession session; 047 048 protected final DocumentModel doc; 049 050 public DocumentScriptingWrapper(CoreSession session, DocumentModel doc) { 051 this.session = session; 052 this.doc = doc; 053 } 054 055 public DocumentModel getDoc() { 056 return doc; 057 } 058 059 public CoreSession getSession() { 060 return session; 061 } 062 063 public DocumentScriptingWrapper getParent() { 064 DocumentModel parent = session.getParentDocument(doc.getRef()); 065 return parent != null ? new DocumentScriptingWrapper(session, parent) : null; 066 } 067 068 public DocumentScriptingWrapper getParent(String type) { 069 DocumentModel parent = session.getParentDocument(doc.getRef()); 070 while (parent != null && !type.equals(parent.getType())) { 071 parent = session.getParentDocument(parent.getRef()); 072 } 073 if (parent == null) { 074 return null; 075 } 076 return new DocumentScriptingWrapper(session, parent); 077 } 078 079 public DocumentScriptingWrapper getWorkspace() { 080 return getParent("Workspace"); 081 } 082 083 public DocumentScriptingWrapper getDomain() { 084 return getParent("Domain"); 085 } 086 087 public String getTitle() { 088 return doc.getTitle(); 089 } 090 091 public String getPath() { 092 return doc.getPathAsString(); 093 } 094 095 public String resolvePath(String relative) { 096 return doc.getPath().append(relative).toString(); 097 } 098 099 /** 100 * @return the document ref 101 */ 102 public DocumentRef getRef() { 103 return doc.getRef(); 104 } 105 106 public DocumentRef resolvePathAsRef(String relative) { 107 return new PathRef(doc.getPath().append(relative).toString()); 108 } 109 110 public String getDescription() { 111 return (String) doc.getPropertyValue("dc:description"); 112 } 113 114 public boolean hasFacet(String facet) { 115 return doc.hasFacet(facet); 116 } 117 118 public boolean hasSchema(String schema) { 119 return doc.hasSchema(schema); 120 } 121 122 public boolean addFacet(String facet) { 123 return doc.addFacet(facet); 124 } 125 126 public boolean removeFacet(String facet) { 127 return doc.removeFacet(facet); 128 } 129 130 public String getType() { 131 return doc.getType(); 132 } 133 134 public DocumentType getDocumentType() { 135 return doc.getDocumentType(); 136 } 137 138 public String getLifeCycle() { 139 return doc.getCurrentLifeCycleState(); 140 } 141 142 public boolean isLocked() { 143 return doc.isLocked(); 144 } 145 146 public boolean isFolder() { 147 return doc.isFolder(); 148 } 149 150 public boolean isImmutable() { 151 return doc.isImmutable(); 152 } 153 154 public boolean isProxy() { 155 return doc.isProxy(); 156 } 157 158 public boolean isVersion() { 159 return doc.isVersion(); 160 } 161 162 public boolean isDownloadable() { 163 return doc.isDownloadable(); 164 } 165 166 public boolean isVersionable() { 167 return doc.isVersionable(); 168 } 169 170 public String getId() { 171 return doc.getId(); 172 } 173 174 public String getName() { 175 return doc.getName(); 176 } 177 178 public String[] getSchemas() { 179 return doc.getSchemas(); 180 } 181 182 public Set<String> getFacets() { 183 return doc.getFacets(); 184 } 185 186 public Serializable getProperty(String key) { 187 return doc.getPropertyValue(key); 188 } 189 190 /** 191 * Alias for #getProperty. 192 */ 193 public Serializable getPropertyValue(String key) { 194 return doc.getPropertyValue(key); 195 } 196 197 public void setProperty(String key, Serializable value) { 198 doc.setPropertyValue(key, value); 199 } 200 201 /** 202 * Alias for #setProperty. 203 */ 204 public void setPropertyValue(String key, Serializable value) { 205 doc.setPropertyValue(key, value); 206 } 207 208 /** 209 * Used by nashorn for native javascript array/date. 210 */ 211 public void setPropertyValue(String key, ScriptObjectMirror value) { 212 doc.setPropertyValue(key, (Serializable) ScriptObjectMirrors.unwrap(value)); 213 } 214 215 public String getVersionLabel() { 216 return doc.getVersionLabel(); 217 } 218 219 /** property map implementation */ 220 221 @Override 222 public boolean containsKey(Object key) { 223 try { 224 doc.getProperty(key.toString()); 225 return true; 226 } catch (PropertyException e) { 227 return false; 228 } 229 } 230 231 /** 232 * The behavior of this method was changed -> it is checking if an xpath has a value attached. 233 */ 234 @Override 235 public boolean containsValue(Object value) { 236 try { 237 return doc.getProperty(value.toString()).getValue() != null; 238 } catch (PropertyException e) { 239 return false; 240 } 241 } 242 243 @Override 244 public Serializable get(Object key) { 245 try { 246 return doc.getProperty(key.toString()).getValue(); 247 } catch (PropertyException e) { 248 return null; 249 } 250 } 251 252 @Override 253 public boolean isEmpty() { 254 return false; 255 } 256 257 @Override 258 public int size() { 259 throw new UnsupportedOperationException("Operation not supported."); 260 } 261 262 @Override 263 public Set<String> keySet() { 264 throw new UnsupportedOperationException("Operation not supported."); 265 } 266 267 @Override 268 public Collection<Object> values() { 269 throw new UnsupportedOperationException("Operation not supported."); 270 } 271 272 @Override 273 public Set<Entry<String, Object>> entrySet() { 274 throw new UnsupportedOperationException("Operation not supported."); 275 } 276 277 /** 278 * As we need to handle {@link ScriptObjectMirror} for array type from nashorn. 279 */ 280 @Override 281 public Object put(String key, Object value) { 282 if (value instanceof ScriptObjectMirror) { 283 return put(key, (Serializable) ScriptObjectMirrors.unwrap((ScriptObjectMirror) value)); 284 } 285 return put(key, (Serializable) value); 286 } 287 288 public Serializable put(String key, Serializable value) { 289 Property p = doc.getProperty(key); 290 Serializable v = p.getValue(); 291 p.setValue(value); 292 return v; 293 } 294 295 @Override 296 public void putAll(Map<? extends String, ?> m) { 297 throw new UnsupportedOperationException("Read Only Map."); 298 } 299 300 @Override 301 public Serializable remove(Object key) { 302 throw new UnsupportedOperationException("Read Only Map."); 303 } 304 305 @Override 306 public void clear() { 307 throw new UnsupportedOperationException("Read Only Map."); 308 } 309 310 @Override 311 public String toString() { 312 return doc.toString(); 313 } 314 315}