View Javadoc

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