001/* 002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Thomas Roger <troger@nuxeo.com> 018 */ 019package org.nuxeo.ecm.activity; 020 021import java.io.Serializable; 022import java.util.ArrayList; 023import java.util.Collection; 024import java.util.HashMap; 025import java.util.List; 026import java.util.Locale; 027import java.util.Map; 028import java.util.Set; 029 030import org.nuxeo.ecm.core.api.CoreSession; 031import org.nuxeo.ecm.core.api.IterableQueryResult; 032import org.nuxeo.runtime.api.Framework; 033 034/** 035 * Default implementation of {@link ActivitiesList}. 036 * 037 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 038 * @since 5.5 039 */ 040public class ActivitiesListImpl extends ArrayList<Activity> implements ActivitiesList { 041 042 private static final long serialVersionUID = 1L; 043 044 public ActivitiesListImpl() { 045 super(); 046 } 047 048 public ActivitiesListImpl(Collection<? extends Activity> c) { 049 super(c); 050 } 051 052 @Override 053 public ActivitiesList filterActivities(CoreSession session) { 054 ActivitiesList filteredActivities = new ActivitiesListImpl(this); 055 056 Map<String, List<Activity>> activitiesByDocument = getActivitiesByDocument(); 057 058 List<String> authorizedDocuments = filterAuthorizedDocuments(activitiesByDocument.keySet(), session); 059 // remove all activities the user has access to 060 for (String authorizedDocument : authorizedDocuments) { 061 activitiesByDocument.remove(authorizedDocument); 062 } 063 064 // extract all unauthorized activities 065 List<Activity> unauthorizedActivities = new ArrayList<>(); 066 for (List<Activity> activities : activitiesByDocument.values()) { 067 unauthorizedActivities.addAll(activities); 068 } 069 070 // remove all unauthorized activities 071 filteredActivities.removeAll(unauthorizedActivities); 072 return filteredActivities; 073 } 074 075 protected Map<String, List<Activity>> getActivitiesByDocument() { 076 Map<String, List<Activity>> activitiesByDocuments = new HashMap<>(); 077 for (Activity activity : this) { 078 List<String> relatedDocuments = getRelatedDocuments(activity); 079 for (String doc : relatedDocuments) { 080 List<Activity> value = activitiesByDocuments.get(doc); 081 if (value == null) { 082 value = new ArrayList<>(); 083 activitiesByDocuments.put(doc, value); 084 } 085 value.add(activity); 086 } 087 } 088 return activitiesByDocuments; 089 } 090 091 protected List<String> getRelatedDocuments(Activity activity) { 092 List<String> relatedDocuments = new ArrayList<>(); 093 094 String activityObject = activity.getActor(); 095 if (activityObject != null && ActivityHelper.isDocument(activityObject)) { 096 relatedDocuments.add(ActivityHelper.getDocumentId(activityObject)); 097 } 098 activityObject = activity.getObject(); 099 if (activityObject != null && ActivityHelper.isDocument(activityObject)) { 100 relatedDocuments.add(ActivityHelper.getDocumentId(activityObject)); 101 } 102 activityObject = activity.getTarget(); 103 if (activityObject != null && ActivityHelper.isDocument(activityObject)) { 104 relatedDocuments.add(ActivityHelper.getDocumentId(activityObject)); 105 } 106 107 return relatedDocuments; 108 } 109 110 protected List<String> filterAuthorizedDocuments(Set<String> allDocuments, CoreSession session) { 111 String idsParam = "('" + String.join("', '", allDocuments) + "')"; 112 String query = String.format("SELECT ecm:uuid FROM Document WHERE ecm:uuid IN %s", idsParam); 113 IterableQueryResult res = session.queryAndFetch(query, "NXQL"); 114 115 try { 116 List<String> authorizedDocuments = new ArrayList<>(); 117 for (Map<String, Serializable> map : res) { 118 authorizedDocuments.add((String) map.get("ecm:uuid")); 119 } 120 return authorizedDocuments; 121 } finally { 122 if (res != null) { 123 res.close(); 124 } 125 } 126 } 127 128 @Override 129 public List<ActivityMessage> toActivityMessages(Locale locale) { 130 ActivityStreamService activityStreamService = Framework.getLocalService(ActivityStreamService.class); 131 List<ActivityMessage> messages = new ArrayList<>(); 132 for (Activity activity : this) { 133 messages.add(activityStreamService.toActivityMessage(activity, locale)); 134 } 135 return messages; 136 } 137 138 @Override 139 public List<ActivityMessage> toActivityMessages(Locale locale, String activityLinkBuilderName) { 140 ActivityStreamService activityStreamService = Framework.getLocalService(ActivityStreamService.class); 141 List<ActivityMessage> messages = new ArrayList<>(); 142 for (Activity activity : this) { 143 messages.add(activityStreamService.toActivityMessage(activity, locale, activityLinkBuilderName)); 144 } 145 return messages; 146 } 147 148 @Override 149 public List<Serializable> toActivityIds() { 150 List<Serializable> activityIds = new ArrayList<>(); 151 for (Activity activity : this) { 152 activityIds.add(activity.getId()); 153 } 154 return activityIds; 155 } 156 157}