001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.krad.service.impl;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.apache.commons.lang.StringUtils;
022import org.kuali.rice.core.api.criteria.PredicateFactory;
023import org.kuali.rice.core.api.criteria.QueryByCriteria;
024import org.kuali.rice.core.api.criteria.QueryResults;
025import org.kuali.rice.kim.api.group.Group;
026import org.kuali.rice.kim.api.services.KimApiServiceLocator;
027import org.kuali.rice.krad.bo.AdHocRoutePerson;
028import org.kuali.rice.krad.bo.AdHocRouteRecipient;
029import org.kuali.rice.krad.bo.AdHocRouteWorkgroup;
030import org.kuali.rice.krad.data.DataObjectService;
031import org.kuali.rice.krad.document.Document;
032import org.kuali.rice.krad.service.DocumentAdHocService;
033
034/**
035 * Implementation for {@link DocumentAdHocService}
036 *
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 *
039 */
040public class DocumentAdHocServiceImpl implements DocumentAdHocService {
041
042        protected DataObjectService dataObjectService;
043
044        /**
045         * {@inheritDoc}
046         */
047        @Override
048        public void replaceAdHocsForDocument( String documentNumber, List<AdHocRouteRecipient> adHocRoutingRecipients ) {
049                if ( StringUtils.isBlank(documentNumber) ) {
050                        return;
051                }
052                dataObjectService.deleteMatching( AdHocRoutePerson.class,
053                                QueryByCriteria.Builder.forAttribute("documentNumber", documentNumber).build() );
054                dataObjectService.deleteMatching( AdHocRouteWorkgroup.class,
055                                QueryByCriteria.Builder.forAttribute("documentNumber", documentNumber).build() );
056
057                if ( adHocRoutingRecipients != null ) {
058                        for ( AdHocRouteRecipient recipient : adHocRoutingRecipients ) {
059                                recipient.setdocumentNumber(documentNumber);
060                                dataObjectService.save(recipient);
061                        }
062                }
063        }
064
065        /**
066         * {@inheritDoc}
067         */
068        @Override
069        public void addAdHocs(Document document) {
070                if ( document == null ) {
071                        return;
072                }
073        /* Instead of reading the doc header to see if doc is in saved status
074         * its probably easier and faster to just do this all the time and
075         * store a null when appropriate.
076         */
077        QueryResults<AdHocRoutePerson> adHocRoutePersons = dataObjectService.findMatching(AdHocRoutePerson.class,
078                            QueryByCriteria.Builder.fromPredicates(
079                                        PredicateFactory.equal("documentNumber", document.getDocumentNumber()),
080                                        PredicateFactory.equal("type", AdHocRoutePerson.PERSON_TYPE) ) );
081
082        QueryResults<AdHocRouteWorkgroup> adHocRouteWorkgroups = dataObjectService.findMatching(AdHocRouteWorkgroup.class,
083                            QueryByCriteria.Builder.fromPredicates(
084                                        PredicateFactory.equal("documentNumber", document.getDocumentNumber()),
085                                        PredicateFactory.equal("type", AdHocRoutePerson.WORKGROUP_TYPE) ) );
086
087        //populate group namespace and names on adHocRouteWorkgroups
088        for (AdHocRouteWorkgroup adHocRouteWorkgroup : adHocRouteWorkgroups.getResults()) {
089            Group group = KimApiServiceLocator.getGroupService().getGroup(adHocRouteWorkgroup.getId());
090            adHocRouteWorkgroup.setRecipientName(group.getName());
091            adHocRouteWorkgroup.setRecipientNamespaceCode(group.getNamespaceCode());
092        }
093
094        // We *must* copy these into new arrays.  The returned lists are unmodifiable.
095        document.setAdHocRoutePersons( new ArrayList<AdHocRoutePerson>( adHocRoutePersons.getResults() ) );
096        document.setAdHocRouteWorkgroups( new ArrayList<AdHocRouteWorkgroup>( adHocRouteWorkgroups.getResults() ) );
097        }
098
099        public void setDataObjectService(DataObjectService dataObjectService) {
100                this.dataObjectService = dataObjectService;
101        }
102
103}