001/* 002 * (C) Copyright 2014 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 * <a href="mailto:grenard@nuxeo.com">Guillaume</a> 018 */ 019package org.nuxeo.ecm.collections.core; 020 021import java.util.Locale; 022import java.util.MissingResourceException; 023 024import org.nuxeo.common.utils.i18n.I18NUtils; 025import org.nuxeo.ecm.collections.api.CollectionManager; 026import org.nuxeo.ecm.collections.api.FavoritesConstants; 027import org.nuxeo.ecm.collections.api.FavoritesManager; 028import org.nuxeo.ecm.core.api.CoreSession; 029import org.nuxeo.ecm.core.api.DocumentModel; 030import org.nuxeo.ecm.core.api.DocumentRef; 031import org.nuxeo.ecm.core.api.PathRef; 032import org.nuxeo.ecm.core.api.security.ACE; 033import org.nuxeo.ecm.core.api.security.ACL; 034import org.nuxeo.ecm.core.api.security.ACP; 035import org.nuxeo.ecm.core.api.security.SecurityConstants; 036import org.nuxeo.ecm.core.api.security.impl.ACLImpl; 037import org.nuxeo.ecm.core.api.security.impl.ACPImpl; 038import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService; 039import org.nuxeo.ecm.platform.web.common.locale.LocaleProvider; 040import org.nuxeo.runtime.api.Framework; 041import org.nuxeo.runtime.model.DefaultComponent; 042import org.nuxeo.runtime.transaction.TransactionHelper; 043 044/** 045 * @since 5.9.4 046 */ 047public class FavoritesManagerImpl extends DefaultComponent implements FavoritesManager { 048 049 @Override 050 public void addToFavorites(DocumentModel document, CoreSession session) { 051 final CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class); 052 collectionManager.addToCollection(getFavorites(document, session), document, session); 053 } 054 055 @Override 056 public boolean canAddToFavorites(DocumentModel document) { 057 final CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class); 058 return collectionManager.isCollectable(document); 059 } 060 061 protected DocumentModel createFavorites(CoreSession session, DocumentModel userWorkspace) { 062 DocumentModel doc = session.createDocumentModel(userWorkspace.getPath().toString(), 063 FavoritesConstants.DEFAULT_FAVORITES_NAME, FavoritesConstants.FAVORITES_TYPE); 064 String title = null; 065 try { 066 title = I18NUtils.getMessageString("messages", FavoritesConstants.DEFAULT_FAVORITES_TITLE, new Object[0], 067 getLocale(session)); 068 } catch (MissingResourceException e) { 069 title = FavoritesConstants.DEFAULT_FAVORITES_NAME; 070 } 071 doc.setPropertyValue("dc:title", title); 072 doc.setPropertyValue("dc:description", ""); 073 doc = session.createDocument(doc); 074 075 ACP acp = new ACPImpl(); 076 ACE denyEverything = new ACE(SecurityConstants.EVERYONE, SecurityConstants.EVERYTHING, false); 077 ACE allowEverything = new ACE(session.getPrincipal().getName(), SecurityConstants.EVERYTHING, true); 078 ACL acl = new ACLImpl(); 079 acl.setACEs(new ACE[] { allowEverything, denyEverything }); 080 acp.addACL(acl); 081 doc.setACP(acp, true); 082 083 return doc; 084 } 085 086 @Override 087 public DocumentModel getFavorites(final DocumentModel context, final CoreSession session) { 088 final UserWorkspaceService userWorkspaceService = Framework.getLocalService(UserWorkspaceService.class); 089 final DocumentModel userWorkspace = userWorkspaceService.getCurrentUserPersonalWorkspace(session, context); 090 final DocumentRef lookupRef = new PathRef(userWorkspace.getPath().toString(), 091 FavoritesConstants.DEFAULT_FAVORITES_NAME); 092 if (session.exists(lookupRef)) { 093 return session.getChild(userWorkspace.getRef(), FavoritesConstants.DEFAULT_FAVORITES_NAME); 094 } else { 095 // does not exist yet, let's create it 096 synchronized (this) { 097 TransactionHelper.commitOrRollbackTransaction(); 098 TransactionHelper.startTransaction(); 099 if (!session.exists(lookupRef)) { 100 boolean succeed = false; 101 try { 102 createFavorites(session, userWorkspace); 103 succeed = true; 104 } finally { 105 if (succeed) { 106 TransactionHelper.commitOrRollbackTransaction(); 107 TransactionHelper.startTransaction(); 108 } 109 } 110 } 111 return session.getDocument(lookupRef); 112 } 113 } 114 } 115 116 protected Locale getLocale(final CoreSession session) { 117 Locale locale = null; 118 locale = Framework.getLocalService(LocaleProvider.class).getLocale(session); 119 if (locale == null) { 120 locale = Locale.getDefault(); 121 } 122 return new Locale(Locale.getDefault().getLanguage()); 123 } 124 125 @Override 126 public boolean isFavorite(DocumentModel document, CoreSession session) { 127 final CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class); 128 return collectionManager.isInCollection(getFavorites(document, session), document, session); 129 } 130 131 @Override 132 public void removeFromFavorites(DocumentModel document, CoreSession session) { 133 final CollectionManager collectionManager = Framework.getLocalService(CollectionManager.class); 134 collectionManager.removeFromCollection(getFavorites(document, session), document, session); 135 } 136 137}