View Javadoc

1   /**
2    * Copyright 2005-2012 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.krms.impl.ui;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
20  import org.kuali.rice.krad.maintenance.MaintainableImpl;
21  import org.kuali.rice.krad.service.BusinessObjectService;
22  import org.kuali.rice.krad.service.KRADServiceLocator;
23  import org.kuali.rice.krad.uif.container.CollectionGroup;
24  import org.kuali.rice.krad.uif.view.View;
25  import org.kuali.rice.krms.impl.repository.ContextBo;
26  import org.kuali.rice.krms.impl.repository.ContextValidTermBo;
27  import org.kuali.rice.krms.impl.repository.TermSpecificationBo;
28  
29  import java.util.Collection;
30  import java.util.Collections;
31  import java.util.Map;
32  
33  /**
34   * {@link org.kuali.rice.krad.maintenance.Maintainable} for the {@link AgendaEditor}
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   *
38   */
39  public class TermSpecificationMaintainable extends MaintainableImpl {
40  	
41  	private static final long serialVersionUID = 1L;
42  
43      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(TermSpecificationMaintainable.class);
44  
45  	/**
46  	 * @return the boService
47  	 */
48  	public BusinessObjectService getBoService() {
49  		return KRADServiceLocator.getBusinessObjectService();
50  	}
51  
52      @Override
53      public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) {
54  
55          TermSpecificationBo termSpecificationBo = (TermSpecificationBo) super.retrieveObjectForEditOrCopy(document,
56                  dataObjectKeys);
57  
58          // find contexts for this term spec
59          Collection<ContextValidTermBo> validContextMappings =
60                  getBoService().findMatching(ContextValidTermBo.class,
61                          Collections.singletonMap("termSpecificationId", termSpecificationBo.getId()));
62  
63          if (!CollectionUtils.isEmpty(validContextMappings)) for (ContextValidTermBo validContextMapping : validContextMappings) {
64              ContextBo context = getBoService().findBySinglePrimaryKey(ContextBo.class, validContextMapping.getContextId());
65              termSpecificationBo.getContexts().add(context);
66          }
67  
68          return termSpecificationBo;
69      }
70  
71      /**
72  	 * {@inheritDoc}
73  	 */
74  	@Override
75  	public void processAfterNew(MaintenanceDocument document,
76  		Map<String, String[]> requestParameters) {
77  
78  		super.processAfterNew(document, requestParameters);
79          document.getDocumentHeader().setDocumentDescription("New Term Specification Document");
80  
81  	}
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> requestParameters) {
88  
89  
90          super.processAfterEdit(document,
91                  requestParameters);
92  
93          document.getDocumentHeader().setDocumentDescription("Edited Term Specification Document");
94      }
95  
96      @Override
97      public void saveDataObject() {
98          TermSpecificationBo termSpec = (TermSpecificationBo) getDataObject();
99  
100         super.saveDataObject();    // save it, it should get an id assigned
101 
102         if (termSpec.getId() != null) {
103             // clear all context valid term mappings
104             getBoService().deleteMatching(ContextValidTermBo.class,
105                     Collections.singletonMap("termSpecificationId", termSpec.getId()));
106 
107             // add a new mapping for each context in the collection
108             if (!CollectionUtils.isEmpty(termSpec.getContexts())) for (ContextBo context : termSpec.getContexts()) {
109                 ContextValidTermBo contextValidTerm = new ContextValidTermBo();
110                 contextValidTerm.setContextId(context.getId());
111                 contextValidTerm.setTermSpecificationId(termSpec.getId());
112                 getBoService().save(contextValidTerm);
113             }
114         }
115 
116     }
117 
118     @Override
119     public Class getDataObjectClass() {
120         return TermSpecificationBo.class;
121     }
122 
123     @Override
124     protected void processBeforeAddLine(View view, CollectionGroup collectionGroup, Object model, Object addLine) {
125         super.processBeforeAddLine(view, collectionGroup, model, addLine);
126     }
127 
128 
129 }