Search in sources :

Example 1 with ScriptException

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);
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ScriptException(org.alfresco.scripts.ScriptException) FileInfo(org.alfresco.service.cmr.model.FileInfo) FileExistsException(org.alfresco.service.cmr.model.FileExistsException) JSONException(org.json.JSONException) UnimportantTransformException(org.alfresco.repo.content.transform.UnimportantTransformException) IOException(java.io.IOException) UnsupportedTransformationException(org.alfresco.repo.content.transform.UnsupportedTransformationException) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) NamespaceException(org.alfresco.service.namespace.NamespaceException) NoTransformerException(org.alfresco.service.cmr.repository.NoTransformerException) InvalidTypeException(org.alfresco.repo.model.filefolder.FileFolderServiceImpl.InvalidTypeException) ScriptException(org.alfresco.scripts.ScriptException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException)

Example 2 with ScriptException

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;
}
Also used : ScriptException(org.alfresco.scripts.ScriptException) FileExistsException(org.alfresco.service.cmr.model.FileExistsException) JSONException(org.json.JSONException) UnimportantTransformException(org.alfresco.repo.content.transform.UnimportantTransformException) IOException(java.io.IOException) UnsupportedTransformationException(org.alfresco.repo.content.transform.UnsupportedTransformationException) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) NamespaceException(org.alfresco.service.namespace.NamespaceException) NoTransformerException(org.alfresco.service.cmr.repository.NoTransformerException) InvalidTypeException(org.alfresco.repo.model.filefolder.FileFolderServiceImpl.InvalidTypeException) ScriptException(org.alfresco.scripts.ScriptException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) FileExistsException(org.alfresco.service.cmr.model.FileExistsException)

Example 3 with ScriptException

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);
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) ScriptException(org.alfresco.scripts.ScriptException)

Example 4 with ScriptException

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;
}
Also used : ScriptException(org.alfresco.scripts.ScriptException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException)

Example 5 with ScriptException

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;
}
Also used : Serializable(java.io.Serializable) Action(org.alfresco.service.cmr.action.Action) ThumbnailService(org.alfresco.service.cmr.thumbnail.ThumbnailService) ThumbnailRegistry(org.alfresco.repo.thumbnail.ThumbnailRegistry) UnimportantTransformException(org.alfresco.repo.content.transform.UnimportantTransformException) ScriptException(org.alfresco.scripts.ScriptException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ThumbnailDefinition(org.alfresco.repo.thumbnail.ThumbnailDefinition) ContentData(org.alfresco.service.cmr.repository.ContentData) ScriptThumbnail(org.alfresco.repo.thumbnail.script.ScriptThumbnail) RenditionDefinition2(org.alfresco.repo.rendition2.RenditionDefinition2) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException)

Aggregations

ScriptException (org.alfresco.scripts.ScriptException)9 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)5 UnimportantTransformException (org.alfresco.repo.content.transform.UnimportantTransformException)3 NodeRef (org.alfresco.service.cmr.repository.NodeRef)3 Context (org.mozilla.javascript.Context)3 Script (org.mozilla.javascript.Script)3 IOException (java.io.IOException)2 UnsupportedTransformationException (org.alfresco.repo.content.transform.UnsupportedTransformationException)2 InvalidTypeException (org.alfresco.repo.model.filefolder.FileFolderServiceImpl.InvalidTypeException)2 FileExistsException (org.alfresco.service.cmr.model.FileExistsException)2 FileNotFoundException (org.alfresco.service.cmr.model.FileNotFoundException)2 ContentIOException (org.alfresco.service.cmr.repository.ContentIOException)2 NoTransformerException (org.alfresco.service.cmr.repository.NoTransformerException)2 NamespaceException (org.alfresco.service.namespace.NamespaceException)2 JSONException (org.json.JSONException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 Serializable (java.io.Serializable)1