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;
}
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;
}
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;
}
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);
}
}
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;
}
Aggregations