001    /**
002     * Copyright 2005-2012 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.apache.log4j.Logger;
020    import org.apache.ojb.broker.query.Criteria;
021    import org.apache.ojb.broker.query.QueryByCriteria;
022    import org.apache.ojb.broker.query.QueryFactory;
023    import org.apache.ojb.broker.query.ReportQueryByCriteria;
024    import org.kuali.rice.core.api.delegation.DelegationType;
025    import org.kuali.rice.kew.actionitem.ActionItem;
026    import org.kuali.rice.kew.actionitem.OutboxItemActionListExtension;
027    import org.kuali.rice.kew.actionitem.dao.ActionItemDAO;
028    import org.kuali.rice.kew.actionrequest.KimGroupRecipient;
029    import org.kuali.rice.kew.actionrequest.Recipient;
030    import org.kuali.rice.kew.util.WebFriendlyRecipient;
031    import org.kuali.rice.kim.api.identity.Person;
032    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
033    import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
034    
035    import java.sql.Timestamp;
036    import java.util.Collection;
037    import java.util.Date;
038    import java.util.HashMap;
039    import java.util.Iterator;
040    import java.util.List;
041    import java.util.Map;
042    /**
043     * OJB implementation of {@link ActionItemDAO}.
044     *
045     * @author Kuali Rice Team (rice.collab@kuali.org)
046     */
047    public class ActionItemDAOOjbImpl extends PersistenceBrokerDaoSupport implements ActionItemDAO {
048            
049     
050            private static final Logger LOG = Logger.getLogger(ActionItemDAOOjbImpl.class);
051    
052            
053        public ActionItem findByActionItemId(String actionItemId) {
054            Criteria crit = new Criteria();
055            crit.addEqualTo("id", actionItemId);
056            return (ActionItem) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(ActionItem.class, crit));
057        }
058    
059        public void deleteActionItems(Long actionRequestId) {
060            Criteria crit = new Criteria();
061            crit.addEqualTo("actionRequestId", actionRequestId);
062            this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ActionItem.class, crit));
063        }
064    
065        public void deleteActionItem(ActionItem actionItem) {
066            this.getPersistenceBrokerTemplate().delete(actionItem);
067        }
068    
069        public void deleteByDocumentIdWorkflowUserId(String documentId, String workflowUserId) {
070            Criteria crit = new Criteria();
071            crit.addEqualTo("documentId", documentId);
072            crit.addEqualTo("principalId", workflowUserId);
073            this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ActionItem.class, crit));
074        }
075    
076        public void deleteByDocumentId(String documentId) {
077            Criteria crit = new Criteria();
078            crit.addEqualTo("documentId", documentId);
079            this.getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(ActionItem.class, crit));
080        }
081    
082        public Collection<ActionItem> findByWorkflowUserDocumentId(String workflowId, String documentId) {
083            Criteria crit = new Criteria();
084            crit.addEqualTo("principalId", workflowId);
085            crit.addEqualTo("documentId", documentId);
086            return this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionItem.class, crit));
087        }
088    
089        public Collection<ActionItem> findByDocumentId(String documentId) {
090            Criteria crit = new Criteria();
091            crit.addEqualTo("documentId", documentId);
092            return this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionItem.class, crit));
093        }
094    
095        public Collection<ActionItem> findByActionRequestId(String actionRequestId) {
096            Criteria crit = new Criteria();
097            crit.addEqualTo("actionRequestId", actionRequestId);
098            return this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(ActionItem.class, crit));
099        }
100    
101        public Collection<ActionItem> findByDocumentTypeName(String documentTypeName) {
102            return getItemsByDocumentType(ActionItem.class, documentTypeName);
103        }
104    
105        public Collection<ActionItem> getOutboxItemsByDocumentType(String documentTypeName) {
106            return getItemsByDocumentType(OutboxItemActionListExtension.class, documentTypeName);
107        }
108    
109        private Collection<ActionItem> getItemsByDocumentType(Class<? extends ActionItem> objectClass, String documentTypeName) {
110            Criteria crit = new Criteria();
111            crit.addEqualTo("docName", documentTypeName);
112            return this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(objectClass, crit));
113        }
114    
115        public void saveActionItem(ActionItem actionItem) {
116            if (actionItem.getDateAssigned() == null) {
117                actionItem.setDateAssigned(new Timestamp(new Date().getTime()));
118            }
119            this.getPersistenceBrokerTemplate().store(actionItem);
120        }
121    
122        public Collection<Recipient> findSecondaryDelegators(String principalId) {
123            Criteria notNullWorkflowCriteria = new Criteria();
124            notNullWorkflowCriteria.addNotNull("delegatorPrincipalId");
125            Criteria notNullWorkgroupCriteria = new Criteria();
126            notNullWorkgroupCriteria.addNotNull("delegatorGroupId");
127            Criteria orCriteria = new Criteria();
128            orCriteria.addOrCriteria(notNullWorkflowCriteria);
129            orCriteria.addOrCriteria(notNullWorkgroupCriteria);
130            Criteria criteria = new Criteria();
131            criteria.addEqualTo("principalId", principalId);
132            criteria.addEqualTo("delegationType", DelegationType.SECONDARY.getCode());
133            criteria.addAndCriteria(orCriteria);
134            ReportQueryByCriteria query = QueryFactory.newReportQuery(ActionItem.class, criteria);
135    
136            query.setAttributes(new String[]{"delegatorPrincipalId", "delegatorGroupId"});
137            Map<Object, Recipient> delegators = new HashMap<Object, Recipient>();
138            Iterator iterator = this.getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
139            while (iterator.hasNext()) {
140                Object[] ids = (Object[]) iterator.next();
141                if (ids[0] != null && !delegators.containsKey((String) ids[0])) {
142                    WebFriendlyRecipient rec = new WebFriendlyRecipient(KimApiServiceLocator.getPersonService().getPerson((String) ids[0]));
143                    delegators.put((String) ids[0], rec);
144                } else if (ids[1] != null) {
145                    String workgroupId = ids[1].toString();
146                    if (!delegators.containsKey(workgroupId)) {
147                        delegators.put(workgroupId, new KimGroupRecipient(KimApiServiceLocator.getGroupService().getGroup(workgroupId)));
148                    }
149                }
150            }
151            return delegators.values();
152        }
153    
154        public Collection<Recipient> findPrimaryDelegationRecipients(String principalId) {
155            List<String> workgroupIds = KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(principalId);
156            Criteria orCriteria = new Criteria();
157            Criteria delegatorPrincipalIdCriteria = new Criteria();
158            delegatorPrincipalIdCriteria.addEqualTo("delegatorPrincipalId", principalId);
159            if (CollectionUtils.isNotEmpty(workgroupIds)) {
160                Criteria delegatorWorkgroupCriteria = new Criteria();
161                delegatorWorkgroupCriteria.addIn("delegatorGroupId", workgroupIds);
162                orCriteria.addOrCriteria(delegatorWorkgroupCriteria);
163                orCriteria.addOrCriteria(delegatorPrincipalIdCriteria);
164            }
165            else {
166                orCriteria.addAndCriteria(delegatorPrincipalIdCriteria);
167            }
168            Criteria criteria = new Criteria();
169            criteria.addEqualTo("delegationType", DelegationType.PRIMARY.getCode());
170            criteria.addAndCriteria(orCriteria);
171            ReportQueryByCriteria query = QueryFactory.newReportQuery(ActionItem.class, criteria, true);
172    
173            query.setAttributes(new String[]{"principalId"});
174            Map<Object, Recipient> delegators = new HashMap<Object, Recipient>();
175            Iterator iterator = this.getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
176            while (iterator.hasNext()) {
177                Object[] ids = (Object[]) iterator.next();
178                if (ids[0] != null && !delegators.containsKey((String) ids[0])) {
179                   
180                    Person person = KimApiServiceLocator.getPersonService().getPerson((String) ids[0]);
181                    if (person != null) {
182                            WebFriendlyRecipient rec = new WebFriendlyRecipient(person);
183                        delegators.put((String) ids[0],rec);
184                        LOG.warn("The name for " + (String) ids[0] + " was not added to the primary delegate drop down list because the delegate does not exist.");
185                    }
186                    
187                }
188            }
189            return delegators.values();
190        }
191    
192            /**
193             * This overridden method replaced findByWorkfowUser
194             *
195             * @see org.kuali.rice.kew.actionitem.dao.ActionItemDAO#findByPrincipalId(java.lang.String)
196             */
197            public Collection<ActionItem> findByPrincipalId(String principalId) {
198                     Criteria crit = new Criteria();
199                 crit.addEqualTo("principalId", principalId);
200                 QueryByCriteria query = new QueryByCriteria(ActionItem.class, crit);
201                 query.addOrderByAscending("documentId");
202                 return this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
203            }
204    
205    }