use of org.alfresco.scripts.ScriptException in project alfresco-repository by Alfresco.
the class ScriptNode method createFolderPath.
/**
* Create a path of folder (cm:folder) nodes as a child of this node.
* <p>
* This method operates like a unix 'mkdir -p' no error if existing, make parent directories as needed.
* <p>
* Beware: Any unsaved property changes will be lost when this is called. To preserve property changes call {@link #save()} first.
*
* @param path Folder path to create - of the form "One/Two/Three". Leading and trailing slashes are not expected
* to be present in the supplied path.
*
* @return reference to the last child of the newly created folder node(s) or null if failed to create.
*/
public ScriptNode createFolderPath(String path) {
ParameterCheck.mandatoryString("Folder path", path);
List<String> pathElements = Arrays.asList(path.split("/"));
NodeRef currentParentRef = this.nodeRef;
// just loop and create if necessary
for (final String element : pathElements) {
final NodeRef contextNodeRef = currentParentRef;
// does it exist?
// Navigation should not check permissions
NodeRef nodeRef = AuthenticationUtil.runAs(new RunAsWork<NodeRef>() {
@Override
public NodeRef doWork() throws Exception {
return nodeService.getChildByName(contextNodeRef, ContentModel.ASSOC_CONTAINS, element);
}
}, AuthenticationUtil.getSystemUserName());
if (nodeRef == null) {
// Checks for create permissions as the fileFolderService is a public service.
FileInfo createdFileInfo = services.getFileFolderService().create(currentParentRef, element, ContentModel.TYPE_FOLDER);
currentParentRef = createdFileInfo.getNodeRef();
} else if (!services.getDictionaryService().isSubClass(nodeService.getType(nodeRef), ContentModel.TYPE_FOLDER)) {
String parentName = (String) nodeService.getProperty(contextNodeRef, ContentModel.PROP_NAME);
throw new ScriptException("Name [" + element + "] already exists in the target parent: " + parentName);
} else {
// it exists
currentParentRef = nodeRef;
}
}
reset();
return newInstance(currentParentRef, this.services, this.scope);
}
use of org.alfresco.scripts.ScriptException in project alfresco-repository by Alfresco.
the class ScriptNode method move.
/**
* Move this Node from specified parent to a new parent destination.
*
* Beware: Any unsaved property changes will be lost when this is called. To preserve property changes call {@link #save()} first.
*
* @param source Node
* @param destination Node
* @return true on successful move, false on failure to move.
*/
public boolean move(ScriptNode source, ScriptNode destination) {
ParameterCheck.mandatory("Destination Node", destination);
if (source == null) {
return move(destination);
} else {
try {
this.services.getFileFolderService().moveFrom(this.nodeRef, source.getNodeRef(), destination.getNodeRef(), null);
}// MNT-7514 Uninformational error message on move when file name conflicts
catch (FileExistsException ex) {
throw ex;
} catch (Exception e) {
throw new ScriptException("Can't move node", e);
}
}
// reset cached values
reset();
return true;
}
use of org.alfresco.scripts.ScriptException in project alfresco-repository by Alfresco.
the class RhinoScriptProcessor method executeString.
/**
* @see org.alfresco.service.cmr.repository.ScriptProcessor#executeString(java.lang.String, java.util.Map)
*/
public Object executeString(String source, Map<String, Object> model) {
try {
// compile the script based on the node content
Script script;
Context cx = Context.enter();
try {
script = cx.compileString(resolveScriptImports(source), "AlfrescoJS", 1, null);
} finally {
Context.exit();
}
return executeScriptImpl(script, model, true, "string script");
} catch (Throwable err) {
throw new ScriptException("Failed to execute supplied script: " + err.getMessage(), err);
}
}
use of org.alfresco.scripts.ScriptException in project alfresco-repository by Alfresco.
the class ClasspathScriptLocation method getReader.
/**
* @see org.alfresco.service.cmr.repository.ScriptLocation#getReader()
*/
public Reader getReader() {
Reader reader = null;
try {
InputStream stream = getClass().getClassLoader().getResourceAsStream(location);
if (stream == null) {
throw new AlfrescoRuntimeException("Unable to load classpath resource: " + location);
}
reader = new InputStreamReader(stream);
} catch (Throwable err) {
throw new ScriptException("Failed to load classpath resource '" + location + "': " + err.getMessage(), err);
}
return reader;
}
use of org.alfresco.scripts.ScriptException in project alfresco-repository by Alfresco.
the class ScriptNode method createThumbnail.
/**
* Creates a thumbnail for the content property of the node.
*
* The thumbnail name corresponds to pre-set thumbnail details stored in the
* repository.
*
* If the thumbnail is created asynchronously then the result will be null and creation
* of the thumbnail will occure at some point in the background.
*
* If foce param specified system.thumbnail.generate is ignoring. Could be used for preview creation
*
* @param thumbnailName the name of the thumbnail
* @param async indicates whether the thumbnail is create asynchronously or not
* @param force ignore system.thumbnail.generate=false
* @return ScriptThumbnail the newly create thumbnail node or null if async creation occures
*
* @deprecated The async flag in the method signature will not be applicable as all of
* the future transformations will be asynchronous
*/
@Deprecated
public ScriptThumbnail createThumbnail(String thumbnailName, boolean async, boolean force) {
final ThumbnailService thumbnailService = services.getThumbnailService();
ScriptThumbnail result = null;
// We need to create preview for node even if system.thumbnail.generate=false
if (force || thumbnailService.getThumbnailsEnabled()) {
// Use the thumbnail registy to get the details of the thumbail
ThumbnailRegistry registry = thumbnailService.getThumbnailRegistry();
ThumbnailDefinition details = registry.getThumbnailDefinition(thumbnailName);
if (details == null) {
// Throw exception
throw new ScriptException("The thumbnail name '" + thumbnailName + "' is not registered");
}
// If there's nothing currently registered to generate thumbnails for the
// specified mimetype, then log a message and bail out
String nodeMimeType = getMimetype();
Serializable value = this.nodeService.getProperty(nodeRef, ContentModel.PROP_CONTENT);
ContentData contentData = DefaultTypeConverter.INSTANCE.convert(ContentData.class, value);
if (!ContentData.hasContent(contentData)) {
if (logger.isDebugEnabled())
logger.debug("Unable to create thumbnail '" + details.getName() + "' as there is no content");
return null;
}
if (!registry.isThumbnailDefinitionAvailable(contentData.getContentUrl(), nodeMimeType, getSize(), nodeRef, details)) {
logger.info("Unable to create thumbnail '" + details.getName() + "' for " + nodeMimeType + " as no transformer is currently available.");
return null;
}
// Have the thumbnail created
if (async == false) {
try {
// Create the thumbnail
NodeRef thumbnailNodeRef = thumbnailService.createThumbnail(this.nodeRef, ContentModel.PROP_CONTENT, details.getMimetype(), details.getTransformationOptions(), details.getName());
// Create the thumbnail script object
result = new ScriptThumbnail(thumbnailNodeRef, this.services, this.scope);
} catch (AlfrescoRuntimeException e) {
Throwable rootCause = e.getRootCause();
if (rootCause instanceof UnimportantTransformException) {
logger.debug("Unable to create thumbnail '" + details.getName() + "' as " + rootCause.getMessage());
return null;
}
throw e;
}
} else {
RenditionDefinition2 renditionDefinition = renditionDefinitionRegistry2.getRenditionDefinition(thumbnailName);
if (renditionDefinition != null) {
renditionService2.render(nodeRef, thumbnailName);
} else {
Action action = ThumbnailHelper.createCreateThumbnailAction(details, services);
// Queue async creation of thumbnail
this.services.getActionService().executeAction(action, this.nodeRef, true, true);
}
}
}
return result;
}
Aggregations