/* * Copyright (C) 2005-2010 Alfresco Software Limited. * * This file is part of Alfresco * * Alfresco is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Alfresco is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Alfresco. If not, see . */ package org.alfresco.wcm.sandbox.script; import java.io.Serializable; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.alfresco.service.cmr.repository.ContentWriter; import org.alfresco.service.namespace.NamespaceException; import org.alfresco.service.namespace.NamespaceService; import org.alfresco.service.namespace.QName; import org.springframework.extensions.surf.util.ISO8601DateFormat; import org.alfresco.wcm.asset.AssetInfo; import org.alfresco.wcm.asset.AssetService; import org.alfresco.wcm.sandbox.SandboxService; /** * WCM Asset in a sandbox exposed over Java Script API. * @author mrogers * */ public class Asset implements Serializable { /** * */ private static final long serialVersionUID = -5759260478423750966L; private AssetInfo asset; private Sandbox sandbox; private Map props; public Asset(Sandbox sandbox, AssetInfo asset) { this.sandbox = sandbox; this.asset = asset; } /** * The creator of this asset * @return the creator */ public String getCreator() { return asset.getCreator(); } public Date getCreatedDate() { return asset.getCreatedDate(); } public long getFileSize() { return asset.getFileSize(); } public String getCreatedDateAsISO8601() { return ISO8601DateFormat.format(getCreatedDate()); } public String getModifier() { return asset.getModifier(); } public Date getModifiedDate() { return asset.getModifiedDate(); } public String getModifiedDateAsISO8601() { return ISO8601DateFormat.format(getModifiedDate()); } /** * rename this asset * @param newName */ public Asset rename(String newName) { if(!newName.equals(asset.getName())) { AssetInfo newAsset = getAssetService().renameAsset(asset, newName); this.asset = newAsset; } return this; } /** * move this asset * @param newPath */ public Asset move(String newPath) { if(!newPath.equals(asset.getPath())) { AssetInfo newAsset = getAssetService().moveAsset(asset, newPath); this.asset = newAsset; } return this; } public String getName() { return asset.getName(); } /** * Get the full path of this asset eg. /www/avm_webapps/ROOT/myFile.jpg * @return the path of this asset. */ public String getPath() { return asset.getPath(); } public boolean isFile() { return asset.isFile(); } public boolean isFolder() { return asset.isFolder(); } public boolean isDeleted() { return asset.isDeleted(); } public boolean isLocked() { return asset.isLocked(); } public String lockOwner() { return asset.getLockOwner(); } public int getVersion() { return asset.getSandboxVersion(); } /** * Get the properties as a key value pair. The key will be either a local qname e.g. "cm:content" or * a global qname e.g. "{http://www.alfresco.com/content/1.0}content". * * Some properties will be updatable, protected properties are not. * * @return the properties in a key, value pair */ public Map getProperties() { if(props == null) { // Note there is something very strange going on with scope which is why there's this guff with propsX Map propsX = new HashMap(); props = propsX; NamespaceService ns = getNamespaceService(); if(!asset.isDeleted()) { Map intprops = getAssetService().getAssetProperties(asset); for (QName qname : intprops.keySet()) { QName prefixQname = qname.getPrefixedQName(ns); Serializable propValue = intprops.get(qname); try { propsX.put(prefixQname.toPrefixString(), propValue.toString()); } catch (NamespaceException ne) { // No local name, only thing I can do is use the full namke propsX.put(qname.toString(), propValue.toString()); } } } } return props; } // /** // * Save the properties please note some system properties are protected and cannot be updated. If you attempt to // * update a protected property your request will be ignored. // * @param properties // */ // public void save() // { // if(props != null) // { // /** // * Need to map the to a // */ // NamespaceService ns = getNamespaceService(); // Map newProps = new HashMap(props.size()); // for (String key : props.keySet()) // { // String value = props.get(key); // QName q = QName.resolveToQName(ns, key); // newProps.put(q, value); // } // getAssetService().setAssetProperties(asset, newProps); // } // } /** * Submit this asset to staging * @param submitLabel * @param submitComment */ public void submit(String submitLabel, String submitComment) { List items = new ArrayList(1); items.add(this.getPath()); getSandboxService().submitList(getSandbox().getSandboxRef(), items, submitLabel, submitComment); } /** * Delete this asset, after it has been deleted do not use this asset. */ public void deleteAsset() { getAssetService().deleteAsset(this.asset); } /** * revert this asset */ public void revert() { List items = new ArrayList(1); items.add(this.getPath()); getSandboxService().revertList(getSandbox().getSandboxRef(), items); } /** * Get children of this asset, returns an empty array if there are no children. * Only folders have children. */ public Asset[] getChildren() { Asset[] ret = new Asset[0]; if(asset.isFolder()) { int i = 0; List assets = getAssetService().listAssets(getSandbox().getSandboxRef(), asset.getPath(), true); ret = new Asset[assets.size()]; for(AssetInfo asset : assets) { ret[i++]=new Asset(sandbox, asset); } } return ret; } /** * create a new file with the specified properties and content. * @param name the name of the file * @param stringContent the content of the file. Can be null. */ public void createFile(String name, String stringContent) { ContentWriter writer = getAssetService().createFile(getSandbox().getSandboxRef(), asset.getPath(), name, null); if(stringContent != null) { writer.putContent(stringContent); } } /** * create a new folder * @param name the name of the new folder */ public void createFolder(String name) { getAssetService().createFolder(getSandbox().getSandboxRef(), asset.getPath(), name, null); } /** * Get the parent sandbox which contains this asset * @return the parent sandbox which contains this asset */ public Sandbox getSandbox() { return this.sandbox; } /** * @return */ private SandboxService getSandboxService() { return getSandbox().getWebproject().getWebProjects().getSandboxService(); } /** * Get the asset service * @return the asset service */ private AssetService getAssetService() { return getSandbox().getWebproject().getWebProjects().getAssetService(); } /** * Get the asset service * @return the asset service */ private NamespaceService getNamespaceService() { return getSandbox().getWebproject().getWebProjects().getNamespaceService(); } }