Search in sources :

Example 1 with PersonFavouriteKey

use of org.alfresco.repo.favourites.PersonFavourite.PersonFavouriteKey in project alfresco-repository by Alfresco.

the class FavouritesServiceImpl method addFavouriteDocumentOrFolder.

private PersonFavourite addFavouriteDocumentOrFolder(String userName, Type type, NodeRef nodeRef) {
    Map<PersonFavouriteKey, PersonFavourite> personFavourites = getFavouriteNodes(userName, type);
    PersonFavourite personFavourite = getPersonFavourite(userName, type, nodeRef);
    if (personFavourite == null) {
        Date createdAt = new Date();
        final String name = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
        personFavourite = new PersonFavourite(userName, nodeRef, type, name, createdAt);
        personFavourites.put(personFavourite.getKey(), personFavourite);
        updateFavouriteNodes(userName, type, personFavourites);
        QName nodeClass = nodeService.getType(nodeRef);
        final String finalRef = nodeRef.getId();
        final QName nodeType = nodeClass;
        eventPublisher.publishEvent(new EventPreparator() {

            @Override
            public Event prepareEvent(String user, String networkId, String transactionId) {
                return new ActivityEvent("favorite.added", transactionId, networkId, user, finalRef, null, nodeType == null ? null : nodeType.toString(), Client.asType(ClientType.script), null, name, null, 0l, null);
            }
        });
        OnAddFavouritePolicy policy = onAddFavouriteDelegate.get(nodeRef, nodeClass);
        policy.onAddFavourite(userName, nodeRef);
    }
    return personFavourite;
}
Also used : EventPreparator(org.alfresco.sync.repo.events.EventPreparator) ActivityEvent(org.alfresco.sync.events.types.ActivityEvent) QName(org.alfresco.service.namespace.QName) Event(org.alfresco.sync.events.types.Event) ActivityEvent(org.alfresco.sync.events.types.ActivityEvent) PersonFavouriteKey(org.alfresco.repo.favourites.PersonFavourite.PersonFavouriteKey) Date(java.util.Date)

Example 2 with PersonFavouriteKey

use of org.alfresco.repo.favourites.PersonFavourite.PersonFavouriteKey in project alfresco-repository by Alfresco.

the class FavouritesServiceImpl method getFavouriteDocumentOrFolder.

private PersonFavourite getFavouriteDocumentOrFolder(String userName, Type type, NodeRef nodeRef) {
    PersonFavourite favourite = null;
    PrefKeys prefKeys = getPrefKeys(type);
    Map<String, Serializable> preferences = preferenceService.getPreferences(userName, prefKeys.getSharePrefKey());
    String nodes = (String) preferences.get(prefKeys.getSharePrefKey());
    if (nodes != null) {
        Map<PersonFavouriteKey, PersonFavourite> favouriteNodes = extractFavouriteNodes(userName, type, nodes);
        String title = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_TITLE);
        PersonFavouriteKey key = new PersonFavouriteKey(userName, title, type, nodeRef);
        favourite = favouriteNodes.get(key);
    }
    return favourite;
}
Also used : Serializable(java.io.Serializable) PersonFavouriteKey(org.alfresco.repo.favourites.PersonFavourite.PersonFavouriteKey)

Example 3 with PersonFavouriteKey

use of org.alfresco.repo.favourites.PersonFavourite.PersonFavouriteKey in project alfresco-repository by Alfresco.

the class FavouritesServiceImpl method isFavouriteNode.

private boolean isFavouriteNode(String userName, Type type, NodeRef nodeRef) {
    Map<PersonFavouriteKey, PersonFavourite> personFavourites = getFavouriteNodes(userName, type);
    PersonFavouriteKey personFavouriteKey = new PersonFavouriteKey(userName, null, type, nodeRef);
    boolean isFavourite = personFavourites.containsKey(personFavouriteKey);
    return isFavourite;
}
Also used : PersonFavouriteKey(org.alfresco.repo.favourites.PersonFavourite.PersonFavouriteKey)

Example 4 with PersonFavouriteKey

use of org.alfresco.repo.favourites.PersonFavourite.PersonFavouriteKey in project alfresco-repository by Alfresco.

the class FavouritesServiceImpl method getPagedFavourites.

@Override
public PagingResults<PersonFavourite> getPagedFavourites(String userName, Set<Type> types, List<Pair<FavouritesService.SortFields, Boolean>> sortProps, PagingRequest pagingRequest) {
    // Get the user node reference
    final NodeRef personNodeRef = personService.getPerson(userName);
    if (personNodeRef == null) {
        throw new AlfrescoRuntimeException("Can not get preferences for " + userName + " because he/she does not exist.");
    }
    boolean includeFiles = types.contains(Type.FILE);
    boolean includeFolders = types.contains(Type.FOLDER);
    boolean includeSites = types.contains(Type.SITE);
    String currentUserName = AuthenticationUtil.getFullyAuthenticatedUser();
    if (authenticationContext.isSystemUserName(currentUserName) == true || permissionService.hasPermission(personNodeRef, PermissionService.WRITE) == AccessStatus.ALLOWED || userName.equals(currentUserName) == true) {
        // we may have more than one favourite that is considered the same w.r.t. the PersonFavourite comparator
        final Map<PersonFavouriteKey, PersonFavourite> sortedFavouriteNodes = new TreeMap<PersonFavouriteKey, PersonFavourite>(getComparator(sortProps));
        PrefKeys sitePrefKeys = getPrefKeys(Type.SITE);
        PrefKeys documentsPrefKeys = getPrefKeys(Type.FILE);
        PrefKeys foldersPrefKeys = getPrefKeys(Type.FOLDER);
        Map<String, Serializable> preferences = preferenceService.getPreferences(userName);
        for (String key : preferences.keySet()) {
            if (includeFiles && key.startsWith(documentsPrefKeys.sharePrefKey)) {
                String nodes = (String) preferences.get(key);
                if (nodes != null) {
                    sortedFavouriteNodes.putAll(extractFavouriteNodes(userName, Type.FILE, nodes));
                }
            } else if (includeFolders && key.startsWith(foldersPrefKeys.sharePrefKey)) {
                String nodes = (String) preferences.get(key);
                if (nodes != null) {
                    sortedFavouriteNodes.putAll(extractFavouriteNodes(userName, Type.FOLDER, nodes));
                }
            } else if (includeSites && key.startsWith(sitePrefKeys.getSharePrefKey()) && !key.endsWith(".createdAt")) {
                // key is either of the form org.alfresco.share.sites.favourites.<siteId>.favourited or
                // org.alfresco.share.sites.favourites.<siteId>
                extractFavouriteSite(userName, Type.SITE, sortedFavouriteNodes, preferences, key);
            }
        }
        int totalSize = sortedFavouriteNodes.size();
        final PageDetails pageDetails = PageDetails.getPageDetails(pagingRequest, totalSize);
        final List<PersonFavourite> page = new ArrayList<PersonFavourite>(pageDetails.getPageSize());
        Iterator<PersonFavourite> it = sortedFavouriteNodes.values().iterator();
        for (int counter = 0; counter < pageDetails.getEnd() && it.hasNext(); counter++) {
            PersonFavourite favouriteNode = it.next();
            if (counter < pageDetails.getSkipCount()) {
                continue;
            }
            if (counter > pageDetails.getEnd() - 1) {
                break;
            }
            page.add(favouriteNode);
        }
        return new PagingResults<PersonFavourite>() {

            @Override
            public List<PersonFavourite> getPage() {
                return page;
            }

            @Override
            public boolean hasMoreItems() {
                return pageDetails.hasMoreItems();
            }

            @Override
            public Pair<Integer, Integer> getTotalResultCount() {
                Integer total = Integer.valueOf(sortedFavouriteNodes.size());
                return new Pair<Integer, Integer>(total, total);
            }

            @Override
            public String getQueryExecutionId() {
                return null;
            }
        };
    } else {
        // The current user does not have sufficient permissions to update the preferences for this user
        throw new AccessDeniedException("The current user " + currentUserName + " does not have sufficient permissions to get the favourites of the user " + userName);
    }
}
Also used : Serializable(java.io.Serializable) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) PagingResults(org.alfresco.query.PagingResults) ArrayList(java.util.ArrayList) PersonFavouriteKey(org.alfresco.repo.favourites.PersonFavourite.PersonFavouriteKey) TreeMap(java.util.TreeMap) PageDetails(org.alfresco.query.PageDetails) NodeRef(org.alfresco.service.cmr.repository.NodeRef) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) Pair(org.alfresco.util.Pair)

Example 5 with PersonFavouriteKey

use of org.alfresco.repo.favourites.PersonFavourite.PersonFavouriteKey in project alfresco-repository by Alfresco.

the class FavouritesServiceImpl method extractFavouriteNodes.

/*
     * Extract favourite nodes of the given type from the comma-separated list in "nodes".
     */
private Map<PersonFavouriteKey, PersonFavourite> extractFavouriteNodes(String userName, Type type, String nodes) {
    PrefKeys prefKeys = getPrefKeys(type);
    Map<PersonFavouriteKey, PersonFavourite> favouriteNodes = new HashMap<PersonFavouriteKey, PersonFavourite>();
    StringTokenizer st = new StringTokenizer(nodes, ",");
    while (st.hasMoreTokens()) {
        String nodeRefStr = st.nextToken();
        nodeRefStr = nodeRefStr.trim();
        if (!NodeRef.isNodeRef((String) nodeRefStr)) {
            continue;
        }
        NodeRef nodeRef = new NodeRef((String) nodeRefStr);
        if (!nodeService.exists(nodeRef)) {
            continue;
        }
        if (permissionService.hasPermission(nodeRef, PermissionService.READ_PROPERTIES) == AccessStatus.DENIED) {
            continue;
        }
        // get createdAt for this favourited node
        // use ISO8601
        StringBuilder builder = new StringBuilder(prefKeys.getAlfrescoPrefKey());
        builder.append(nodeRef.toString());
        builder.append(".createdAt");
        String prefKey = builder.toString();
        String createdAtStr = (String) preferenceService.getPreference(userName, prefKey);
        Date createdAt = (createdAtStr != null ? ISO8601DateFormat.parse(createdAtStr) : null);
        String name = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
        PersonFavourite personFavourite = new PersonFavourite(userName, nodeRef, type, name, createdAt);
        PersonFavouriteKey key = personFavourite.getKey();
        favouriteNodes.put(key, personFavourite);
    }
    return favouriteNodes;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) StringTokenizer(java.util.StringTokenizer) HashMap(java.util.HashMap) PersonFavouriteKey(org.alfresco.repo.favourites.PersonFavourite.PersonFavouriteKey) Date(java.util.Date)

Aggregations

PersonFavouriteKey (org.alfresco.repo.favourites.PersonFavourite.PersonFavouriteKey)6 Serializable (java.io.Serializable)2 Date (java.util.Date)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 QName (org.alfresco.service.namespace.QName)2 ActivityEvent (org.alfresco.sync.events.types.ActivityEvent)2 Event (org.alfresco.sync.events.types.Event)2 EventPreparator (org.alfresco.sync.repo.events.EventPreparator)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 StringTokenizer (java.util.StringTokenizer)1 TreeMap (java.util.TreeMap)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 PageDetails (org.alfresco.query.PageDetails)1 PagingResults (org.alfresco.query.PagingResults)1 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)1 Pair (org.alfresco.util.Pair)1