001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * bstefanescu 011 */ 012package org.nuxeo.ecm.automation.core.scripting; 013 014import java.io.Serializable; 015import java.util.Collection; 016import java.util.HashMap; 017import java.util.Map; 018import java.util.Set; 019 020import org.nuxeo.ecm.core.api.CoreSession; 021import org.nuxeo.ecm.core.api.DocumentModel; 022import org.nuxeo.ecm.core.api.DocumentRef; 023import org.nuxeo.ecm.core.api.PathRef; 024import org.nuxeo.ecm.core.api.PropertyException; 025import org.nuxeo.ecm.core.api.model.Property; 026import org.nuxeo.ecm.core.schema.DocumentType; 027 028/** 029 * Wrap a {@link DocumentModel} to expose in a pretty way more information to mvel scripts. 030 * 031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 032 */ 033public class DocumentWrapper extends HashMap<String, Serializable> { 034 035 private static final long serialVersionUID = 1L; 036 037 protected final CoreSession session; 038 039 protected final DocumentModel doc; 040 041 public DocumentWrapper(CoreSession session, DocumentModel doc) { 042 this.session = session; 043 this.doc = doc; 044 } 045 046 public DocumentModel getDoc() { 047 return doc; 048 } 049 050 public CoreSession getSession() { 051 return session; 052 } 053 054 public DocumentWrapper getParent() { 055 DocumentModel parent = session.getParentDocument(doc.getRef()); 056 return parent != null ? new DocumentWrapper(session, parent) : null; 057 } 058 059 public DocumentWrapper getParent(String type) { 060 DocumentModel parent = session.getParentDocument(doc.getRef()); 061 while (parent != null && !type.equals(parent.getType())) { 062 parent = session.getParentDocument(parent.getRef()); 063 } 064 if (parent == null) { 065 return null; 066 } 067 return new DocumentWrapper(session, parent); 068 } 069 070 public DocumentWrapper getWorkspace() { 071 return getParent("Workspace"); 072 } 073 074 public DocumentWrapper getDomain() { 075 return getParent("Domain"); 076 } 077 078 public String getTitle() { 079 return doc.getTitle(); 080 } 081 082 public String getPath() { 083 return doc.getPathAsString(); 084 } 085 086 public String resolvePath(String relative) { 087 return doc.getPath().append(relative).toString(); 088 } 089 090 /** 091 * @return the document ref 092 * @since 5.6 093 */ 094 public DocumentRef getRef() { 095 return doc.getRef(); 096 } 097 098 public DocumentRef resolvePathAsRef(String relative) { 099 return new PathRef(doc.getPath().append(relative).toString()); 100 } 101 102 public String getDescription() { 103 return (String) doc.getPropertyValue("dc:description"); 104 } 105 106 public boolean hasFacet(String facet) { 107 return doc.hasFacet(facet); 108 } 109 110 public boolean hasSchema(String schema) { 111 return doc.hasSchema(schema); 112 } 113 114 public boolean addFacet(String facet) { 115 return doc.addFacet(facet); 116 } 117 118 public boolean removeFacet(String facet) { 119 return doc.removeFacet(facet); 120 } 121 122 public String getType() { 123 return doc.getType(); 124 } 125 126 public DocumentType getDocumentType() { 127 return doc.getDocumentType(); 128 } 129 130 public String getLifeCycle() { 131 return doc.getCurrentLifeCycleState(); 132 } 133 134 public boolean isLocked() { 135 return doc.isLocked(); 136 } 137 138 public boolean isFolder() { 139 return doc.isFolder(); 140 } 141 142 public boolean isImmutable() { 143 return doc.isImmutable(); 144 } 145 146 public boolean isProxy() { 147 return doc.isProxy(); 148 } 149 150 public boolean isVersion() { 151 return doc.isVersion(); 152 } 153 154 public boolean isDownloadable() { 155 return doc.isDownloadable(); 156 } 157 158 public boolean isVersionable() { 159 return doc.isVersionable(); 160 } 161 162 public String getId() { 163 return doc.getId(); 164 } 165 166 public String getName() { 167 return doc.getName(); 168 } 169 170 public String[] getSchemas() { 171 return doc.getSchemas(); 172 } 173 174 public Set<String> getFacets() { 175 return doc.getFacets(); 176 } 177 178 public Serializable getProperty(String key) { 179 return doc.getPropertyValue(key); 180 } 181 182 /** 183 * @since 5.7.3 Alias for #getProperty. 184 */ 185 public Serializable getPropertyValue(String key) { 186 return doc.getPropertyValue(key); 187 } 188 189 public void setProperty(String key, Serializable value) { 190 doc.setPropertyValue(key, value); 191 } 192 193 /** 194 * @since 5.7.3 Alias for #setProperty. 195 */ 196 public void setPropertyValue(String key, Serializable value) { 197 doc.setPropertyValue(key, value); 198 } 199 200 public String getVersionLabel() { 201 return doc.getVersionLabel(); 202 } 203 204 /** property map implementation */ 205 206 @Override 207 public boolean containsKey(Object key) { 208 try { 209 doc.getProperty(key.toString()); 210 return true; 211 } catch (PropertyException e) { 212 return false; 213 } 214 } 215 216 /** 217 * The behavior of this method was changed -> it is checking if an xpath has a value attached. 218 */ 219 @Override 220 public boolean containsValue(Object value) { 221 try { 222 return doc.getProperty(value.toString()).getValue() != null; 223 } catch (PropertyException e) { 224 return false; 225 } 226 } 227 228 @Override 229 public Serializable get(Object key) { 230 try { 231 return doc.getProperty(key.toString()).getValue(); 232 } catch (PropertyException e) { 233 return null; 234 } 235 } 236 237 @Override 238 public boolean isEmpty() { 239 return false; 240 } 241 242 @Override 243 public int size() { 244 throw new UnsupportedOperationException("Operation not supported."); 245 } 246 247 @Override 248 public Set<String> keySet() { 249 throw new UnsupportedOperationException("Operation not supported."); 250 } 251 252 @Override 253 public Collection<Serializable> values() { 254 throw new UnsupportedOperationException("Operation not supported."); 255 } 256 257 @Override 258 public Set<Map.Entry<String, Serializable>> entrySet() { 259 throw new UnsupportedOperationException("Operation not supported."); 260 } 261 262 @Override 263 public Serializable put(String key, Serializable value) { 264 Property p = doc.getProperty(key); 265 Serializable v = p.getValue(); 266 p.setValue(value); 267 return v; 268 } 269 270 @Override 271 public void putAll(Map<? extends String, ? extends Serializable> m) { 272 throw new UnsupportedOperationException("Read Only Map."); 273 } 274 275 @Override 276 public Serializable remove(Object key) { 277 throw new UnsupportedOperationException("Read Only Map."); 278 } 279 280 @Override 281 public void clear() { 282 throw new UnsupportedOperationException("Read Only Map."); 283 } 284 285}