001 /* 002 * Copyright 2005-2008 The Kuali Foundation 003 * 004 * 005 * Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in 006 * compliance with the License. You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS 011 * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific 012 * language governing permissions and limitations under the License. 013 */ 014 package org.kuali.rice.kew.actionitem.dao.impl; 015 016 import java.util.ArrayList; 017 import java.util.Collection; 018 import java.util.HashMap; 019 import java.util.Iterator; 020 import java.util.List; 021 import java.util.Map; 022 023 import javax.persistence.EntityManager; 024 import javax.persistence.PersistenceContext; 025 026 import org.apache.commons.collections.CollectionUtils; 027 import org.kuali.rice.core.jpa.criteria.Criteria; 028 import org.kuali.rice.core.jpa.criteria.QueryByCriteria; 029 import org.kuali.rice.core.util.OrmUtils; 030 import org.kuali.rice.kew.actionitem.ActionItem; 031 import org.kuali.rice.kew.actionitem.OutboxItemActionListExtension; 032 import org.kuali.rice.kew.actionitem.dao.ActionItemDAO; 033 import org.kuali.rice.kew.actionrequest.KimGroupRecipient; 034 import org.kuali.rice.kew.actionrequest.Recipient; 035 import org.kuali.rice.kew.util.KEWConstants; 036 import org.kuali.rice.kew.util.WebFriendlyRecipient; 037 import org.kuali.rice.kim.service.IdentityManagementService; 038 import org.kuali.rice.kim.service.KIMServiceLocator; 039 /** 040 * OJB implementation of {@link ActionItemDAO}. 041 * 042 * @author Kuali Rice Team (rice.collab@kuali.org) 043 */ 044 public class ActionItemDAOJpaImpl implements ActionItemDAO { 045 @PersistenceContext(unitName="kew-unit") 046 private EntityManager entityManager; 047 048 public ActionItem findByActionItemId(Long actionItemId) { 049 return entityManager.find(ActionItem.class, actionItemId); 050 } 051 052 public void deleteActionItems(Long actionRequestId) { 053 Criteria crit = new Criteria(ActionItem.class.getName()); 054 crit.eq("actionRequestId", actionRequestId); 055 for(Object actionItem:new QueryByCriteria(entityManager,crit).toQuery().getResultList()){ 056 if( ! (actionItem instanceof OutboxItemActionListExtension)) { 057 entityManager.remove(actionItem); 058 } 059 } 060 061 } 062 063 public void deleteActionItem(ActionItem actionItem) { 064 entityManager.remove(findByActionItemId(actionItem.getActionItemId())); 065 } 066 067 public void deleteByRouteHeaderIdWorkflowUserId(Long routeHeaderId, String workflowUserId) { 068 Criteria crit = new Criteria(ActionItem.class.getName()); 069 crit.eq("routeHeader.routeHeaderId", routeHeaderId); 070 crit.eq("principalId", workflowUserId); 071 for(Object actionItem: new QueryByCriteria(entityManager,crit).toQuery().getResultList()){ 072 if( ! (actionItem instanceof OutboxItemActionListExtension)) { 073 entityManager.remove(actionItem); 074 } 075 } 076 } 077 078 public void deleteByRouteHeaderId(Long routeHeaderId) { 079 Criteria crit = new Criteria(ActionItem.class.getName()); 080 crit.eq("routeHeader.routeHeaderId", routeHeaderId); 081 for(Object actionItem: new QueryByCriteria(entityManager,crit).toQuery().getResultList()){ 082 if( ! (actionItem instanceof OutboxItemActionListExtension)) { 083 entityManager.remove(actionItem); 084 } 085 } 086 } 087 088 /** 089 * This method replaces findByWorkflowUser 090 * 091 * @see org.kuali.rice.kew.actionitem.dao.ActionItemDAO#findByPrincipalId(java.lang.String) 092 */ 093 public Collection<ActionItem> findByPrincipalId(String principalId) { 094 Criteria crit = new Criteria(ActionItem.class.getName()); 095 crit.eq("principalId", principalId); 096 crit.orderBy("routeHeader.routeHeaderId", true); 097 List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList(); 098 099 return removeOutBoxItems(results); 100 } 101 102 public Collection<ActionItem> findByWorkflowUserRouteHeaderId(String workflowId, Long routeHeaderId) { 103 Criteria crit = new Criteria(ActionItem.class.getName()); 104 crit.eq("principalId", workflowId); 105 crit.eq("routeHeader.routeHeaderId", routeHeaderId); 106 List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList(); 107 108 return removeOutBoxItems(results); 109 } 110 111 public Collection<ActionItem> findByDocumentTypeName(String documentTypeName) { 112 Criteria crit = new Criteria(ActionItem.class.getName()); 113 crit.eq("docName", documentTypeName); 114 List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList(); 115 116 return removeOutBoxItems(results); 117 } 118 119 public Collection<ActionItem> getOutboxItemsByDocumentType(String documentTypeName) { 120 Criteria crit = new Criteria(ActionItem.class.getName()); 121 crit.eq("docName", documentTypeName); 122 List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList(); 123 Iterator<ActionItem> iterator = results.iterator(); 124 while (iterator.hasNext()) { 125 if (!(iterator.next() instanceof OutboxItemActionListExtension)) { 126 iterator.remove(); 127 } 128 } 129 return results; 130 } 131 132 public Collection<ActionItem> findByRouteHeaderId(Long routeHeaderId) { 133 Criteria crit = new Criteria(ActionItem.class.getName()); 134 crit.eq("routeHeader.routeHeaderId", routeHeaderId); 135 List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList(); 136 137 return removeOutBoxItems(results); 138 } 139 140 public Collection<ActionItem> findByActionRequestId(Long actionRequestId) { 141 Criteria crit = new Criteria(ActionItem.class.getName()); 142 crit.eq("actionRequestId", actionRequestId); 143 List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList(); 144 145 return removeOutBoxItems(results); 146 } 147 148 public void saveActionItem(ActionItem actionItem) { 149 if(actionItem.getActionItemId()==null){ 150 entityManager.persist(actionItem); 151 }else{ 152 OrmUtils.merge(entityManager, actionItem); 153 } 154 } 155 156 public Collection<Recipient> findSecondaryDelegators(String principalId) { 157 Criteria notNullWorkflowCriteria = new Criteria(ActionItem.class.getName()); 158 notNullWorkflowCriteria.notNull("delegatorWorkflowId"); 159 Criteria notNullWorkgroupCriteria = new Criteria(ActionItem.class.getName()); 160 notNullWorkgroupCriteria.notNull("delegatorGroupId"); 161 Criteria orCriteria = new Criteria(ActionItem.class.getName()); 162 orCriteria.or(notNullWorkflowCriteria); 163 orCriteria.or(notNullWorkgroupCriteria); 164 Criteria criteria = new Criteria(ActionItem.class.getName()); 165 criteria.eq("principalId", principalId); 166 criteria.eq("delegationType", KEWConstants.DELEGATION_SECONDARY); 167 criteria.and(orCriteria); 168 169 Map<Object, Recipient> delegators = new HashMap<Object, Recipient>(); 170 171 for(Object actionItem:new QueryByCriteria(entityManager, criteria).toQuery().getResultList()){ 172 final String delegatorWorkflowId = ((ActionItem)actionItem).getDelegatorWorkflowId(); 173 String delegatorGroupId = ((ActionItem)actionItem).getDelegatorGroupId(); 174 175 if (delegatorWorkflowId != null && !delegators.containsKey(delegatorWorkflowId)) { 176 delegators.put(delegatorWorkflowId,new WebFriendlyRecipient(KIMServiceLocator.getPersonService().getPerson(delegatorWorkflowId))); 177 }else if (delegatorGroupId != null) { 178 if (!delegators.containsKey(delegatorGroupId)) { 179 delegators.put(delegatorGroupId, new KimGroupRecipient(getIdentityManagementService().getGroup(delegatorGroupId))); 180 } 181 } 182 } 183 return delegators.values(); 184 } 185 186 public Collection<Recipient> findPrimaryDelegationRecipients(String principalId) { 187 List<String> workgroupIds = KIMServiceLocator.getIdentityManagementService().getGroupIdsForPrincipal(principalId); 188 Criteria orCriteria = new Criteria(ActionItem.class.getName()); 189 Criteria delegatorWorkflowIdCriteria = new Criteria(ActionItem.class.getName()); 190 delegatorWorkflowIdCriteria.eq("delegatorWorkflowId", principalId); 191 if (CollectionUtils.isNotEmpty(workgroupIds)) { 192 Criteria delegatorWorkgroupCriteria = new Criteria(ActionItem.class.getName()); 193 delegatorWorkgroupCriteria.in("delegatorGroupId", new ArrayList<String>(workgroupIds)); 194 orCriteria.or(delegatorWorkgroupCriteria); 195 orCriteria.or(delegatorWorkflowIdCriteria); 196 } 197 else { 198 orCriteria.and(delegatorWorkflowIdCriteria); 199 } 200 Criteria criteria = new Criteria(ActionItem.class.getName()); 201 criteria.eq("delegationType", KEWConstants.DELEGATION_PRIMARY); 202 criteria.and(orCriteria); 203 204 Map<Object, Recipient> delegators = new HashMap<Object, Recipient>(); 205 for(Object actionItem:new QueryByCriteria(entityManager, criteria).toQuery().getResultList()){ 206 String princlId = ((ActionItem)actionItem).getPrincipalId(); 207 if (princlId != null && !delegators.containsKey(princlId)) { 208 delegators.put(princlId, new WebFriendlyRecipient(KIMServiceLocator.getPersonService().getPerson(princlId))); 209 } 210 } 211 212 return delegators.values(); 213 } 214 215 private List<ActionItem> removeOutBoxItems(List<ActionItem> results) { 216 Iterator<ActionItem> iterator = results.iterator(); 217 while(iterator.hasNext()) { 218 if(iterator.next() instanceof OutboxItemActionListExtension) { 219 iterator.remove(); 220 } 221 } 222 223 return results; 224 } 225 226 private IdentityManagementService getIdentityManagementService() { 227 return (IdentityManagementService) KIMServiceLocator.getService(KIMServiceLocator.KIM_IDENTITY_MANAGEMENT_SERVICE); 228 } 229 230 public EntityManager getEntityManager() { 231 return this.entityManager; 232 } 233 234 public void setEntityManager(EntityManager entityManager) { 235 this.entityManager = entityManager; 236 } 237 238 239 }