Search in sources :

Example 1 with DocumentTarget

use of org.alfresco.rest.api.model.DocumentTarget in project alfresco-remote-api by Alfresco.

the class FavouritesImpl method addFavourite.

@Override
public Favourite addFavourite(String personId, Favourite favourite, Parameters parameters) {
    Favourite ret = null;
    personId = people.validatePerson(personId, true);
    Target target = favourite.getTarget();
    if (target == null) {
        throw new InvalidArgumentException("target is missing");
    } else if (target instanceof SiteTarget) {
        SiteTarget siteTarget = (SiteTarget) target;
        String guid = siteTarget.getSite().getGuid();
        SiteInfo siteInfo = sites.validateSite(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, guid));
        NodeRef siteNodeRef = siteInfo.getNodeRef();
        String siteId = siteInfo.getShortName();
        try {
            PersonFavourite personFavourite = favouritesService.addFavourite(personId, siteNodeRef);
            ret = getFavourite(personFavourite, parameters);
        } catch (SiteDoesNotExistException e) {
            throw new RelationshipResourceNotFoundException(personId, siteId);
        }
    } else if (target instanceof DocumentTarget) {
        DocumentTarget documentTarget = (DocumentTarget) target;
        NodeRef nodeRef = documentTarget.getFile().getGuid();
        if (!nodes.nodeMatches(nodeRef, Collections.singleton(ContentModel.TYPE_CONTENT), null)) {
            throw new RelationshipResourceNotFoundException(personId, nodeRef.getId());
        }
        PersonFavourite personFavourite = favouritesService.addFavourite(personId, nodeRef);
        ret = getFavourite(personFavourite, parameters);
    } else if (target instanceof FolderTarget) {
        FolderTarget folderTarget = (FolderTarget) target;
        NodeRef nodeRef = folderTarget.getFolder().getGuid();
        if (!nodes.nodeMatches(nodeRef, Collections.singleton(ContentModel.TYPE_FOLDER), Collections.singleton(SiteModel.TYPE_SITE))) {
            throw new RelationshipResourceNotFoundException(personId, nodeRef.getId());
        }
        PersonFavourite personFavourite = favouritesService.addFavourite(personId, nodeRef);
        ret = getFavourite(personFavourite, parameters);
    }
    return ret;
}
Also used : RelationshipResourceNotFoundException(org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException) SiteInfo(org.alfresco.service.cmr.site.SiteInfo) NodeRef(org.alfresco.service.cmr.repository.NodeRef) SiteTarget(org.alfresco.rest.api.model.SiteTarget) DocumentTarget(org.alfresco.rest.api.model.DocumentTarget) FolderTarget(org.alfresco.rest.api.model.FolderTarget) Target(org.alfresco.rest.api.model.Target) DocumentTarget(org.alfresco.rest.api.model.DocumentTarget) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) PersonFavourite(org.alfresco.repo.favourites.PersonFavourite) SiteDoesNotExistException(org.alfresco.repo.site.SiteDoesNotExistException) SiteTarget(org.alfresco.rest.api.model.SiteTarget) FolderTarget(org.alfresco.rest.api.model.FolderTarget) PersonFavourite(org.alfresco.repo.favourites.PersonFavourite) Favourite(org.alfresco.rest.api.model.Favourite)

Example 2 with DocumentTarget

use of org.alfresco.rest.api.model.DocumentTarget in project alfresco-remote-api by Alfresco.

the class FavouritesImpl method getTarget.

private Target getTarget(PersonFavourite personFavourite, Parameters parameters) {
    Target target = null;
    NodeRef nodeRef = personFavourite.getNodeRef();
    Type type = personFavourite.getType();
    if (type.equals(Type.FILE)) {
        Document document = nodes.getDocument(nodeRef);
        setPathInfo(document, parameters.getInclude());
        target = new DocumentTarget(document);
    } else if (type.equals(Type.FOLDER)) {
        Folder folder = nodes.getFolder(nodeRef);
        setPathInfo(folder, parameters.getInclude());
        target = new FolderTarget(folder);
    } else if (type.equals(Type.SITE)) {
        SiteInfo siteInfo = siteService.getSite(nodeRef);
        String role = sites.getSiteRole(siteInfo.getShortName());
        Site site = new Site(siteInfo, role);
        target = new SiteTarget(site);
    } else {
        throw new AlfrescoRuntimeException("Unexpected favourite target type: " + type);
    }
    return target;
}
Also used : Site(org.alfresco.rest.api.model.Site) NodeRef(org.alfresco.service.cmr.repository.NodeRef) SiteInfo(org.alfresco.service.cmr.site.SiteInfo) SiteTarget(org.alfresco.rest.api.model.SiteTarget) DocumentTarget(org.alfresco.rest.api.model.DocumentTarget) FolderTarget(org.alfresco.rest.api.model.FolderTarget) Target(org.alfresco.rest.api.model.Target) Type(org.alfresco.service.cmr.favourites.FavouritesService.Type) DocumentTarget(org.alfresco.rest.api.model.DocumentTarget) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) SiteTarget(org.alfresco.rest.api.model.SiteTarget) FolderTarget(org.alfresco.rest.api.model.FolderTarget) Document(org.alfresco.rest.api.model.Document) Folder(org.alfresco.rest.api.model.Folder)

Example 3 with DocumentTarget

use of org.alfresco.rest.api.model.DocumentTarget in project alfresco-remote-api by Alfresco.

the class TargetDeserializer method deserialize.

@Override
public Target deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    Target target = null;
    JsonToken curr = jp.getCurrentToken();
    if (curr == JsonToken.START_OBJECT) {
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String fieldname = jp.getCurrentName();
            if (Type.SITE.toString().equals(fieldname.toUpperCase())) {
                jp.nextToken();
                try {
                    JavaType t = SimpleType.construct(Site.class);
                    BeanProperty p = new BeanProperty.Std(new PropertyName(""), t, null, null, null, null);
                    JsonDeserializer<?> siteDeserializer = ctxt.findContextualValueDeserializer(t, p);
                    Site site = (Site) siteDeserializer.deserialize(jp, ctxt);
                    target = new SiteTarget(site);
                } catch (JsonMappingException e) {
                    throw new IllegalArgumentException("Target body is invalid for target type");
                }
            } else if (Type.FILE.toString().equals(fieldname.toUpperCase())) {
                jp.nextToken();
                try {
                    JavaType t = SimpleType.construct(Document.class);
                    BeanProperty p = new BeanProperty.Std(new PropertyName(""), t, null, null, null, null);
                    JsonDeserializer<?> documentDeserializer = ctxt.findContextualValueDeserializer(t, p);
                    Document document = (Document) documentDeserializer.deserialize(jp, ctxt);
                    target = new DocumentTarget(document);
                } catch (JsonMappingException e) {
                    throw new IllegalArgumentException("Target body is invalid for target type");
                }
            } else if (Type.FOLDER.toString().equals(fieldname.toUpperCase())) {
                jp.nextToken();
                try {
                    JavaType t = SimpleType.construct(Folder.class);
                    BeanProperty p = new BeanProperty.Std(new PropertyName(""), t, null, null, null, null);
                    JsonDeserializer<?> folderDeserializer = ctxt.findContextualValueDeserializer(t, p);
                    Folder folder = (Folder) folderDeserializer.deserialize(jp, ctxt);
                    target = new FolderTarget(folder);
                } catch (JsonMappingException e) {
                    throw new IllegalArgumentException("Target body is invalid for target type");
                }
            }
        }
        return target;
    } else {
        throw new IOException("Unable to deserialize favourite: " + curr.asString());
    }
}
Also used : Site(org.alfresco.rest.api.model.Site) PropertyName(com.fasterxml.jackson.databind.PropertyName) DocumentTarget(org.alfresco.rest.api.model.DocumentTarget) FolderTarget(org.alfresco.rest.api.model.FolderTarget) IOException(java.io.IOException) Document(org.alfresco.rest.api.model.Document) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) Folder(org.alfresco.rest.api.model.Folder) BeanProperty(com.fasterxml.jackson.databind.BeanProperty) Target(org.alfresco.rest.api.model.Target) SiteTarget(org.alfresco.rest.api.model.SiteTarget) DocumentTarget(org.alfresco.rest.api.model.DocumentTarget) FolderTarget(org.alfresco.rest.api.model.FolderTarget) JavaType(com.fasterxml.jackson.databind.JavaType) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonToken(com.fasterxml.jackson.core.JsonToken) SiteTarget(org.alfresco.rest.api.model.SiteTarget)

Aggregations

DocumentTarget (org.alfresco.rest.api.model.DocumentTarget)3 FolderTarget (org.alfresco.rest.api.model.FolderTarget)3 SiteTarget (org.alfresco.rest.api.model.SiteTarget)3 Target (org.alfresco.rest.api.model.Target)3 Document (org.alfresco.rest.api.model.Document)2 Folder (org.alfresco.rest.api.model.Folder)2 Site (org.alfresco.rest.api.model.Site)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 SiteInfo (org.alfresco.service.cmr.site.SiteInfo)2 JsonToken (com.fasterxml.jackson.core.JsonToken)1 BeanProperty (com.fasterxml.jackson.databind.BeanProperty)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 PropertyName (com.fasterxml.jackson.databind.PropertyName)1 IOException (java.io.IOException)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 PersonFavourite (org.alfresco.repo.favourites.PersonFavourite)1 SiteDoesNotExistException (org.alfresco.repo.site.SiteDoesNotExistException)1 Favourite (org.alfresco.rest.api.model.Favourite)1