View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.service.impl;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.rice.core.api.criteria.PredicateFactory;
23  import org.kuali.rice.core.api.criteria.QueryByCriteria;
24  import org.kuali.rice.core.api.criteria.QueryResults;
25  import org.kuali.rice.kim.api.group.Group;
26  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
27  import org.kuali.rice.krad.bo.AdHocRoutePerson;
28  import org.kuali.rice.krad.bo.AdHocRouteRecipient;
29  import org.kuali.rice.krad.bo.AdHocRouteWorkgroup;
30  import org.kuali.rice.krad.data.DataObjectService;
31  import org.kuali.rice.krad.document.Document;
32  import org.kuali.rice.krad.service.DocumentAdHocService;
33  
34  /**
35   * Implementation for {@link DocumentAdHocService}
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   *
39   */
40  public class DocumentAdHocServiceImpl implements DocumentAdHocService {
41  
42  	protected DataObjectService dataObjectService;
43  
44  	/**
45  	 * {@inheritDoc}
46  	 */
47  	@Override
48  	public void replaceAdHocsForDocument( String documentNumber, List<AdHocRouteRecipient> adHocRoutingRecipients ) {
49  		if ( StringUtils.isBlank(documentNumber) ) {
50  			return;
51  		}
52  		dataObjectService.deleteMatching( AdHocRoutePerson.class,
53  				QueryByCriteria.Builder.forAttribute("documentNumber", documentNumber).build() );
54  		dataObjectService.deleteMatching( AdHocRouteWorkgroup.class,
55  				QueryByCriteria.Builder.forAttribute("documentNumber", documentNumber).build() );
56  
57  		if ( adHocRoutingRecipients != null ) {
58  			for ( AdHocRouteRecipient recipient : adHocRoutingRecipients ) {
59  				recipient.setdocumentNumber(documentNumber);
60  				dataObjectService.save(recipient);
61  			}
62  		}
63  	}
64  
65  	/**
66  	 * {@inheritDoc}
67  	 */
68  	@Override
69  	public void addAdHocs(Document document) {
70  		if ( document == null ) {
71  			return;
72  		}
73          /* Instead of reading the doc header to see if doc is in saved status
74           * its probably easier and faster to just do this all the time and
75           * store a null when appropriate.
76           */
77          QueryResults<AdHocRoutePerson> adHocRoutePersons = dataObjectService.findMatching(AdHocRoutePerson.class,
78  			    QueryByCriteria.Builder.fromPredicates(
79  			    		PredicateFactory.equal("documentNumber", document.getDocumentNumber()),
80  			    		PredicateFactory.equal("type", AdHocRoutePerson.PERSON_TYPE) ) );
81  
82          QueryResults<AdHocRouteWorkgroup> adHocRouteWorkgroups = dataObjectService.findMatching(AdHocRouteWorkgroup.class,
83  			    QueryByCriteria.Builder.fromPredicates(
84  			    		PredicateFactory.equal("documentNumber", document.getDocumentNumber()),
85  			    		PredicateFactory.equal("type", AdHocRoutePerson.WORKGROUP_TYPE) ) );
86  
87          //populate group namespace and names on adHocRouteWorkgroups
88          for (AdHocRouteWorkgroup adHocRouteWorkgroup : adHocRouteWorkgroups.getResults()) {
89              Group group = KimApiServiceLocator.getGroupService().getGroup(adHocRouteWorkgroup.getId());
90              adHocRouteWorkgroup.setRecipientName(group.getName());
91              adHocRouteWorkgroup.setRecipientNamespaceCode(group.getNamespaceCode());
92          }
93  
94          // We *must* copy these into new arrays.  The returned lists are unmodifiable.
95          document.setAdHocRoutePersons( new ArrayList<AdHocRoutePerson>( adHocRoutePersons.getResults() ) );
96          document.setAdHocRouteWorkgroups( new ArrayList<AdHocRouteWorkgroup>( adHocRouteWorkgroups.getResults() ) );
97  	}
98  
99  	public void setDataObjectService(DataObjectService dataObjectService) {
100 		this.dataObjectService = dataObjectService;
101 	}
102 
103 }