001 /** 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php 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 package org.kuali.rice.kew.actionitem.dao.impl; 017 018 import org.apache.commons.collections.CollectionUtils; 019 import org.kuali.rice.core.api.delegation.DelegationType; 020 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils; 021 import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria; 022 import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria; 023 import org.kuali.rice.kew.actionitem.ActionItem; 024 import org.kuali.rice.kew.actionitem.OutboxItemActionListExtension; 025 import org.kuali.rice.kew.actionitem.dao.ActionItemDAO; 026 import org.kuali.rice.kew.actionrequest.KimGroupRecipient; 027 import org.kuali.rice.kew.actionrequest.Recipient; 028 import org.kuali.rice.kew.util.WebFriendlyRecipient; 029 import org.kuali.rice.kim.api.services.KimApiServiceLocator; 030 031 import javax.persistence.EntityManager; 032 import javax.persistence.PersistenceContext; 033 import java.sql.Timestamp; 034 import java.util.ArrayList; 035 import java.util.Collection; 036 import java.util.Date; 037 import java.util.HashMap; 038 import java.util.Iterator; 039 import java.util.List; 040 import java.util.Map; 041 042 /** 043 * OJB implementation of {@link ActionItemDAO}. 044 * 045 * @author Kuali Rice Team (rice.collab@kuali.org) 046 */ 047 public class ActionItemDAOJpaImpl implements ActionItemDAO { 048 @PersistenceContext(unitName="kew-unit") 049 private EntityManager entityManager; 050 051 public ActionItem findByActionItemId(String actionItemId) { 052 return entityManager.find(ActionItem.class, actionItemId); 053 } 054 055 public void deleteActionItems(Long actionRequestId) { 056 Criteria crit = new Criteria(ActionItem.class.getName()); 057 crit.eq("actionRequestId", actionRequestId); 058 for(Object actionItem:new QueryByCriteria(entityManager,crit).toQuery().getResultList()){ 059 if( ! (actionItem instanceof OutboxItemActionListExtension)) { 060 entityManager.remove(actionItem); 061 } 062 } 063 064 } 065 066 public void deleteActionItem(ActionItem actionItem) { 067 entityManager.remove(findByActionItemId(actionItem.getId())); 068 } 069 070 public void deleteByDocumentIdWorkflowUserId(String documentId, String workflowUserId) { 071 Criteria crit = new Criteria(ActionItem.class.getName()); 072 crit.eq("documentId", documentId); 073 crit.eq("principalId", workflowUserId); 074 for(Object actionItem: new QueryByCriteria(entityManager,crit).toQuery().getResultList()){ 075 if( ! (actionItem instanceof OutboxItemActionListExtension)) { 076 entityManager.remove(actionItem); 077 } 078 } 079 } 080 081 public void deleteByDocumentId(String documentId) { 082 Criteria crit = new Criteria(ActionItem.class.getName()); 083 crit.eq("documentId", documentId); 084 for(Object actionItem: new QueryByCriteria(entityManager,crit).toQuery().getResultList()){ 085 if( ! (actionItem instanceof OutboxItemActionListExtension)) { 086 entityManager.remove(actionItem); 087 } 088 } 089 } 090 091 /** 092 * This method replaces findByWorkflowUser 093 * 094 * @see org.kuali.rice.kew.actionitem.dao.ActionItemDAO#findByPrincipalId(java.lang.String) 095 */ 096 public Collection<ActionItem> findByPrincipalId(String principalId) { 097 Criteria crit = new Criteria(ActionItem.class.getName()); 098 crit.eq("principalId", principalId); 099 crit.orderBy("documentId", true); 100 List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList(); 101 102 return removeOutBoxItems(results); 103 } 104 105 public Collection<ActionItem> findByWorkflowUserDocumentId(String workflowId, String documentId) { 106 Criteria crit = new Criteria(ActionItem.class.getName()); 107 crit.eq("principalId", workflowId); 108 crit.eq("documentId", documentId); 109 List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList(); 110 111 return removeOutBoxItems(results); 112 } 113 114 public Collection<ActionItem> findByDocumentTypeName(String documentTypeName) { 115 Criteria crit = new Criteria(ActionItem.class.getName()); 116 crit.eq("docName", documentTypeName); 117 List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList(); 118 119 return removeOutBoxItems(results); 120 } 121 122 public Collection<OutboxItemActionListExtension> getOutboxItemsByDocumentType(String documentTypeName) { 123 Criteria crit = new Criteria(ActionItem.class.getName()); 124 crit.eq("docName", documentTypeName); 125 List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList(); 126 List<OutboxItemActionListExtension> finalResults = new ArrayList<OutboxItemActionListExtension>(); 127 for (ActionItem actionItem : results) { 128 if (actionItem instanceof OutboxItemActionListExtension) { 129 finalResults.add((OutboxItemActionListExtension)actionItem); 130 } 131 } 132 return finalResults; 133 } 134 135 public Collection<ActionItem> findByDocumentId(String documentId) { 136 Criteria crit = new Criteria(ActionItem.class.getName()); 137 crit.eq("documentId", documentId); 138 List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList(); 139 140 return removeOutBoxItems(results); 141 } 142 143 public Collection<ActionItem> findByActionRequestId(String actionRequestId) { 144 Criteria crit = new Criteria(ActionItem.class.getName()); 145 crit.eq("actionRequestId", actionRequestId); 146 List<ActionItem> results = new QueryByCriteria(entityManager, crit).toQuery().getResultList(); 147 148 return removeOutBoxItems(results); 149 } 150 151 public void saveActionItem(ActionItem actionItem) { 152 if (actionItem.getDateAssigned() == null) { 153 actionItem.setDateAssigned(new Timestamp(new Date().getTime())); 154 } 155 156 if(actionItem.getId()==null){ 157 entityManager.persist(actionItem); 158 }else{ 159 OrmUtils.merge(entityManager, actionItem); 160 } 161 } 162 163 public Collection<Recipient> findSecondaryDelegators(String principalId) { 164 Criteria notNullWorkflowCriteria = new Criteria(ActionItem.class.getName()); 165 notNullWorkflowCriteria.notNull("delegatorPrincipalId"); 166 Criteria notNullWorkgroupCriteria = new Criteria(ActionItem.class.getName()); 167 notNullWorkgroupCriteria.notNull("delegatorGroupId"); 168 Criteria orCriteria = new Criteria(ActionItem.class.getName()); 169 orCriteria.or(notNullWorkflowCriteria); 170 orCriteria.or(notNullWorkgroupCriteria); 171 Criteria criteria = new Criteria(ActionItem.class.getName()); 172 criteria.eq("principalId", principalId); 173 criteria.eq("delegationType", DelegationType.SECONDARY.getCode()); 174 criteria.and(orCriteria); 175 176 Map<Object, Recipient> delegators = new HashMap<Object, Recipient>(); 177 178 for(Object actionItem:new QueryByCriteria(entityManager, criteria).toQuery().getResultList()){ 179 final String delegatorPrincipalId = ((ActionItem)actionItem).getDelegatorPrincipalId(); 180 String delegatorGroupId = ((ActionItem)actionItem).getDelegatorGroupId(); 181 182 if (delegatorPrincipalId != null && !delegators.containsKey(delegatorPrincipalId)) { 183 delegators.put(delegatorPrincipalId,new WebFriendlyRecipient(KimApiServiceLocator.getPersonService().getPerson(delegatorPrincipalId))); 184 }else if (delegatorGroupId != null) { 185 if (!delegators.containsKey(delegatorGroupId)) { 186 delegators.put(delegatorGroupId, new KimGroupRecipient(KimApiServiceLocator.getGroupService().getGroup( 187 delegatorGroupId))); 188 } 189 } 190 } 191 return delegators.values(); 192 } 193 194 public Collection<Recipient> findPrimaryDelegationRecipients(String principalId) { 195 List<String> workgroupIds = KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(principalId); 196 Criteria orCriteria = new Criteria(ActionItem.class.getName()); 197 Criteria delegatorPrincipalIdCriteria = new Criteria(ActionItem.class.getName()); 198 delegatorPrincipalIdCriteria.eq("delegatorPrincipalId", principalId); 199 if (CollectionUtils.isNotEmpty(workgroupIds)) { 200 Criteria delegatorWorkgroupCriteria = new Criteria(ActionItem.class.getName()); 201 delegatorWorkgroupCriteria.in("delegatorGroupId", new ArrayList<String>(workgroupIds)); 202 orCriteria.or(delegatorWorkgroupCriteria); 203 orCriteria.or(delegatorPrincipalIdCriteria); 204 } 205 else { 206 orCriteria.and(delegatorPrincipalIdCriteria); 207 } 208 Criteria criteria = new Criteria(ActionItem.class.getName()); 209 criteria.eq("delegationType", DelegationType.PRIMARY.getCode()); 210 criteria.and(orCriteria); 211 212 Map<Object, Recipient> delegators = new HashMap<Object, Recipient>(); 213 for(Object actionItem:new QueryByCriteria(entityManager, criteria).toQuery().getResultList()){ 214 String princlId = ((ActionItem)actionItem).getPrincipalId(); 215 if (princlId != null && !delegators.containsKey(princlId)) { 216 delegators.put(princlId, new WebFriendlyRecipient(KimApiServiceLocator.getPersonService().getPerson(princlId))); 217 } 218 } 219 220 return delegators.values(); 221 } 222 223 private List<ActionItem> removeOutBoxItems(List<ActionItem> results) { 224 Iterator<ActionItem> iterator = results.iterator(); 225 while(iterator.hasNext()) { 226 if(iterator.next() instanceof OutboxItemActionListExtension) { 227 iterator.remove(); 228 } 229 } 230 231 return results; 232 } 233 234 public EntityManager getEntityManager() { 235 return this.entityManager; 236 } 237 238 public void setEntityManager(EntityManager entityManager) { 239 this.entityManager = entityManager; 240 } 241 242 243 }