/* * Copyright (C) 2005-2009 Alfresco Software Limited. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * As a special exception to the terms and conditions of version 2.0 of * the GPL, you may redistribute this Program in connection with Free/Libre * and Open Source Software ("FLOSS") applications as described in Alfresco's * FLOSS exception. You should have recieved a copy of the text describing * the FLOSS exception, and it is also available here: * http://www.alfresco.com/legal/licensing" */ package org.alfresco.repo.activities; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.repo.domain.activities.ActivityFeedDAO; import org.alfresco.repo.domain.activities.ActivityFeedEntity; import org.alfresco.repo.domain.activities.FeedControlDAO; import org.alfresco.repo.domain.activities.FeedControlEntity; import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.permissions.AccessDeniedException; import org.alfresco.repo.tenant.TenantService; import org.alfresco.service.cmr.activities.ActivityPostService; import org.alfresco.service.cmr.activities.ActivityService; import org.alfresco.service.cmr.activities.FeedControl; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.security.AuthorityService; import org.alfresco.service.cmr.site.SiteInfo; import org.alfresco.service.cmr.site.SiteService; import org.alfresco.service.namespace.QName; import org.alfresco.util.ParameterCheck; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.json.JSONException; /** * Activity Service Implementation * * @author janv */ public class ActivityServiceImpl implements ActivityService { private static final Log logger = LogFactory.getLog(ActivityServiceImpl.class); private ActivityFeedDAO feedDAO; private FeedControlDAO feedControlDAO; private AuthorityService authorityService; private TenantService tenantService; private SiteService siteService; private ActivityPostService activityPostService; private int maxFeedItems = 100; private boolean userNamesAreCaseSensitive = false; public void setMaxFeedItems(int maxFeedItems) { this.maxFeedItems = maxFeedItems; } public void setUserNamesAreCaseSensitive(boolean userNamesAreCaseSensitive) { this.userNamesAreCaseSensitive = userNamesAreCaseSensitive; } public void setFeedDAO(ActivityFeedDAO feedDAO) { this.feedDAO = feedDAO; } public void setFeedControlDAO(FeedControlDAO feedControlDAO) { this.feedControlDAO = feedControlDAO; } public void setAuthorityService(AuthorityService authorityService) { this.authorityService = authorityService; } public void setTenantService(TenantService tenantService) { this.tenantService = tenantService; } public void setSiteService(SiteService siteService) { this.siteService = siteService; } public void setActivityPostService(ActivityPostService activityPostService) { this.activityPostService = activityPostService; } /* (non-Javadoc) * @see org.alfresco.service.cmr.activities.ActivityService#postActivity(java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ public void postActivity(String activityType, String siteId, String appTool, String activityData) { // delegate activityPostService.postActivity(activityType, siteId, appTool, activityData); } /* (non-Javadoc) * @see org.alfresco.service.cmr.activities.ActivityService#postActivity(java.lang.String, java.lang.String, java.lang.String, org.alfresco.service.cmr.repository.NodeRef) */ public void postActivity(String activityType, String siteId, String appTool, NodeRef nodeRef) { // delegate activityPostService.postActivity(activityType, siteId, appTool, nodeRef); } /* (non-Javadoc) * @see org.alfresco.service.cmr.activities.ActivityService#postActivity(java.lang.String, java.lang.String, java.lang.String, org.alfresco.service.cmr.repository.NodeRef, java.lang.String) */ public void postActivity(String activityType, String siteId, String appTool, NodeRef nodeRef, String name) { // delegate activityPostService.postActivity(activityType, siteId, appTool, nodeRef, name); } /* (non-Javadoc) * @see org.alfresco.service.cmr.activities.ActivityService#postActivity(java.lang.String, java.lang.String, java.lang.String, org.alfresco.service.cmr.repository.NodeRef, java.lang.String, org.alfresco.service.namespace.QName, org.alfresco.service.cmr.repository.NodeRef) */ public void postActivity(String activityType, String siteId, String appTool, NodeRef nodeRef, String name, QName typeQName, NodeRef parentNodeRef) { // delegate activityPostService.postActivity(activityType, siteId, appTool, nodeRef, name, typeQName, parentNodeRef); } /* (non-Javadoc) * @see org.alfresco.service.cmr.activities.ActivityService#getUserFeedEntries(java.lang.String, java.lang.String, java.lang.String) */ public List getUserFeedEntries(String feedUserId, String format, String siteId) { return getUserFeedEntries(feedUserId, format, siteId, false, false); } /* (non-Javadoc) * @see org.alfresco.service.cmr.activities.ActivityService#getUserFeedEntries(java.lang.String, java.lang.String, java.lang.String, boolean, boolean) */ public List getUserFeedEntries(String feedUserId, String format, String siteId, boolean excludeThisUser, boolean excludeOtherUsers) { // NOTE: siteId is optional ParameterCheck.mandatoryString("feedUserId", feedUserId); ParameterCheck.mandatoryString("format", format); List activityFeedEntries = new ArrayList(); if (! userNamesAreCaseSensitive) { feedUserId = feedUserId.toLowerCase(); } String currentUser = AuthenticationUtil.getFullyAuthenticatedUser(); if (! ((currentUser == null) || (currentUser.equals(AuthenticationUtil.getSystemUserName())) || (authorityService.isAdminAuthority(currentUser)) || (currentUser.equals(feedUserId)))) { throw new AccessDeniedException("Unable to get user feed entries for '" + feedUserId + "' - currently logged in as '" + currentUser +"'"); } try { List activityFeeds = null; if (siteId != null) { siteId = tenantService.getName(siteId); } activityFeeds = feedDAO.selectUserFeedEntries(feedUserId, format, siteId, excludeThisUser, excludeOtherUsers); int count = 0; for (ActivityFeedEntity activityFeed : activityFeeds) { count++; if (count > maxFeedItems) { break; } activityFeed.setSiteNetwork(tenantService.getBaseName(activityFeed.getSiteNetwork())); activityFeedEntries.add(activityFeed.getJSONString()); } } catch (SQLException se) { AlfrescoRuntimeException are = new AlfrescoRuntimeException("Unable to get user feed entries: " + se.getMessage()); logger.error(are); throw are; } catch (JSONException je) { AlfrescoRuntimeException are = new AlfrescoRuntimeException("Unable to get user feed entries: " + je.getMessage()); logger.error(are); throw are; } return activityFeedEntries; } /* (non-Javadoc) * @see org.alfresco.service.cmr.activities.ActivityService#getSiteFeedEntries(java.lang.String, java.lang.String) */ public List getSiteFeedEntries(String siteId, String format) { ParameterCheck.mandatoryString("siteId", siteId); ParameterCheck.mandatoryString("format", format); List activityFeedEntries = new ArrayList(); try { if (siteService != null) { SiteInfo siteInfo = siteService.getSite(siteId); if (siteInfo == null) { throw new AccessDeniedException("No such site: " + siteId); } } siteId = tenantService.getName(siteId); List activityFeeds = feedDAO.selectSiteFeedEntries(siteId, format); int count = 0; for (ActivityFeedEntity activityFeed : activityFeeds) { count++; if (count > maxFeedItems) { break; } activityFeed.setSiteNetwork(tenantService.getBaseName(activityFeed.getSiteNetwork())); activityFeedEntries.add(activityFeed.getJSONString()); } } catch (SQLException se) { AlfrescoRuntimeException are = new AlfrescoRuntimeException("Unable to get site feed entries: " + se.getMessage()); logger.error(are); throw are; } catch (JSONException je) { AlfrescoRuntimeException are = new AlfrescoRuntimeException("Unable to get site feed entries: " + je.getMessage()); logger.error(are); throw are; } return activityFeedEntries; } /* (non-Javadoc) * @see org.alfresco.service.cmr.activities.ActivityService#setFeedControl(org.alfresco.service.cmr.activities.FeedControl) */ public void setFeedControl(FeedControl feedControl) { ParameterCheck.mandatory("feedControl", feedControl); String userId = AuthenticationUtil.getFullyAuthenticatedUser(); if (! userNamesAreCaseSensitive) { userId = userId.toLowerCase(); } try { if (! existsFeedControl(feedControl)) { feedControlDAO.insertFeedControl(new FeedControlEntity(userId, feedControl)); } } catch (SQLException e) { AlfrescoRuntimeException are = new AlfrescoRuntimeException("Failed to set feed control: " + e, e); logger.error(are); throw are; } } /* (non-Javadoc) * @see org.alfresco.service.cmr.activities.ActivityService#getFeedControls() */ public List getFeedControls() { String userId = AuthenticationUtil.getFullyAuthenticatedUser(); return getFeedControlsImpl(userId); } /* (non-Javadoc) * @see org.alfresco.service.cmr.activities.ActivityService#getFeedControls(java.lang.String) */ public List getFeedControls(String userId) { ParameterCheck.mandatoryString("userId", userId); String currentUser = AuthenticationUtil.getFullyAuthenticatedUser(); if ((currentUser == null) || ((! currentUser.equals(AuthenticationUtil.getSystemUserName())) && (! currentUser.equals(userId)) && (! authorityService.isAdminAuthority(currentUser)))) { throw new AlfrescoRuntimeException("Current user " + currentUser + " is not permitted to get feed controls for " + userId); } return getFeedControlsImpl(userId); } private List getFeedControlsImpl(String userId) { ParameterCheck.mandatoryString("userId", userId); if (! userNamesAreCaseSensitive) { userId = userId.toLowerCase(); } try { List feedControlDaos = feedControlDAO.selectFeedControls(userId); List feedControls = new ArrayList(feedControlDaos.size()); for (FeedControlEntity feedControlDao : feedControlDaos) { feedControls.add(feedControlDao.getFeedControl()); } return feedControls; } catch (SQLException e) { AlfrescoRuntimeException are = new AlfrescoRuntimeException("Failed to get feed controls: " + e, e); logger.error(are); throw are; } } /* (non-Javadoc) * @see org.alfresco.service.cmr.activities.ActivityService#deleteFeedControl(org.alfresco.service.cmr.activities.FeedControl) */ public void unsetFeedControl(FeedControl feedControl) { ParameterCheck.mandatory("feedControl", feedControl); String userId = AuthenticationUtil.getFullyAuthenticatedUser(); if (! userNamesAreCaseSensitive) { userId = userId.toLowerCase(); } try { feedControlDAO.deleteFeedControl(new FeedControlEntity(userId, feedControl)); } catch (SQLException e) { AlfrescoRuntimeException are = new AlfrescoRuntimeException("Failed to unset feed control: " + e, e); logger.error(are); throw are; } } /* (non-Javadoc) * @see org.alfresco.service.cmr.activities.ActivityService#existsFeedControl(java.lang.String, org.alfresco.service.cmr.activities.FeedControl) */ public boolean existsFeedControl(FeedControl feedControl) { ParameterCheck.mandatory("feedControl", feedControl); String userId = AuthenticationUtil.getFullyAuthenticatedUser(); if (! userNamesAreCaseSensitive) { userId = userId.toLowerCase(); } try { long id = feedControlDAO.selectFeedControl(new FeedControlEntity(userId, feedControl)); return (id != -1); } catch (SQLException e) { AlfrescoRuntimeException are = new AlfrescoRuntimeException("Failed to query feed control: " + e, e); logger.error(are); throw are; } } private FeedControl getTenantFeedControl(FeedControl feedControl) { // TODO return null; } private FeedControl getBaseFeedControl(FeedControl feedControl) { // TODO return null; } }