View Javadoc

1   /**
2    * Copyright 2005-2013 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.kns.service.KNSServiceLocator;
20  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
21  import org.kuali.rice.krad.maintenance.MaintainableImpl;
22  import org.kuali.rice.krad.service.BusinessObjectService;
23  import org.kuali.rice.krad.service.KRADServiceLocator;
24  import org.kuali.rice.krad.util.KRADConstants;
25  import org.kuali.rice.krms.api.repository.context.ContextDefinition;
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.KrmsRepositoryServiceLocator;
29  import org.kuali.rice.krms.impl.repository.TermSpecificationBo;
30  
31  import java.util.ArrayList;
32  import java.util.Collection;
33  import java.util.Collections;
34  import java.util.Map;
35  
36  /**
37   * {@link org.kuali.rice.krad.maintenance.Maintainable} for the {@link AgendaEditor}
38   *
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   *
41   */
42  public class TermSpecificationMaintainable extends MaintainableImpl {
43  	
44  	private static final long serialVersionUID = 1L;
45  
46      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(TermSpecificationMaintainable.class);
47  
48  	/**
49  	 * @return the boService
50  	 */
51  	public BusinessObjectService getBoService() {
52  		return KNSServiceLocator.getBusinessObjectService();
53  	}
54  
55      @Override
56      public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) {
57  
58          TermSpecificationBo termSpecificationBo = (TermSpecificationBo) super.retrieveObjectForEditOrCopy(document,
59                  dataObjectKeys);
60  
61          if (!CollectionUtils.isEmpty(termSpecificationBo.getContextIds())) {
62              for (String contextId : termSpecificationBo.getContextIds()) {
63                  ContextDefinition context =
64                          KrmsRepositoryServiceLocator.getContextBoService().getContextByContextId(contextId);
65                  if (context != null) {
66                      termSpecificationBo.getContexts().add(ContextBo.from(context));
67                  }
68              }
69          }
70  
71          if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(getMaintenanceAction())) {
72              document.getDocumentHeader().setDocumentDescription("New Term Specification Document");
73          }
74  
75          return termSpecificationBo;
76      }
77  
78      /**
79       * Add the Term Specification's Context to the given termSpecificationBo.  Note that there is no check for the Context
80       * already having been added to the Term Specification.
81       * @param termSpecificationBo with
82       */
83      private void findContexts(TermSpecificationBo termSpecificationBo) {
84          Collection<ContextValidTermBo> validContextMappings =
85              getBoService().findMatching(ContextValidTermBo.class,
86                      Collections.singletonMap("termSpecificationId", termSpecificationBo.getId()));
87  
88          if (!CollectionUtils.isEmpty(validContextMappings)) for (ContextValidTermBo validContextMapping : validContextMappings) {
89              termSpecificationBo.getContextIds().add(validContextMapping.getContextId());
90          }
91      }
92  
93      /**
94  	 * {@inheritDoc}
95  	 */
96  	@Override
97  	public void processAfterNew(MaintenanceDocument document,
98  		Map<String, String[]> requestParameters) {
99  
100 		super.processAfterNew(document, requestParameters);
101         document.getDocumentHeader().setDocumentDescription("New Term Specification Document");
102 
103 	}
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> requestParameters) {
110         super.processAfterEdit(document, requestParameters);
111         copyContextsOldToNewBo(document);
112         document.getDocumentHeader().setDocumentDescription("Edited Term Specification Document");
113     }
114 
115     @Override
116     public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> requestParameters) {
117         super.processAfterCopy(document, requestParameters);
118         copyContextsOldToNewBo(document);
119     }
120 
121     /**
122      * Copy the contexts from the old TermSpecificationBo to the newTermSpecificationBo of the maintenance document.
123      * <p>
124      * Since the contexts is a transient field it doesn't get copied by the deepCopy in
125      * MaintenanceDocumentServiceImpl.setupMaintenanceObject, we manually need to copy the values over.
126      * For performance reasons a shallow copy is done since the ContextBo themselves are never changed.
127      * </p>
128      * @param document that contains the old and new TermSpecificationBos
129      */
130     private void copyContextsOldToNewBo(MaintenanceDocument document) {
131         TermSpecificationBo oldTermSpec = (TermSpecificationBo) document.getOldMaintainableObject().getDataObject();
132         TermSpecificationBo newTermSpec = (TermSpecificationBo) document.getNewMaintainableObject().getDataObject();
133         newTermSpec.setContexts(new ArrayList<ContextBo>());
134         for (ContextBo contextBo : oldTermSpec.getContexts()) {
135             newTermSpec.getContexts().add(contextBo);
136         }
137     }
138 
139     /**
140      * Store contexts in contextIds (needed for serialization)
141      *
142      * @see org.kuali.rice.krad.maintenance.Maintainable#prepareForSave
143      */
144     @Override
145     public void prepareForSave() {
146         super.prepareForSave();
147 
148         TermSpecificationBo termSpec = (TermSpecificationBo) getDataObject();
149         termSpec.setContextIds(new ArrayList<String>());
150         for (ContextBo context : termSpec.getContexts()) {
151             termSpec.getContextIds().add(context.getId());
152         }
153     }
154 
155     @Override
156     public void saveDataObject() {
157         TermSpecificationBo termSpec = (TermSpecificationBo) getDataObject();
158 
159         super.saveDataObject();    // save it, it should get an id assigned
160 
161         if (termSpec.getId() != null) {
162             // clear all context valid term mappings
163             getBoService().deleteMatching(ContextValidTermBo.class,
164                     Collections.singletonMap("termSpecificationId", termSpec.getId()));
165 
166             // add a new mapping for each context in the collection
167             for (String contextId : termSpec.getContextIds()) {
168                 ContextValidTermBo contextValidTerm = new ContextValidTermBo();
169                 contextValidTerm.setContextId(contextId);
170                 contextValidTerm.setTermSpecificationId(termSpec.getId());
171                 getBoService().save(contextValidTerm);
172             }
173         }
174     }
175 
176     @Override
177     public Class getDataObjectClass() {
178         return TermSpecificationBo.class;
179     }
180 
181     /**
182      * Recreate the contexts from the contextIDs (needed due to serialization)
183      *
184      * @see org.kuali.rice.krad.maintenance.Maintainable#processAfterRetrieve
185      */
186     @Override
187     public void processAfterRetrieve() {
188         super.processAfterRetrieve();
189 
190         TermSpecificationBo termSpec = (TermSpecificationBo) getDataObject();
191         termSpec.setContexts(new ArrayList<ContextBo>());
192         for (String contextId : termSpec.getContextIds()) {
193             ContextDefinition context = KrmsRepositoryServiceLocator.getContextBoService().getContextByContextId(contextId);
194             termSpec.getContexts().add(ContextBo.from(context));
195         }
196     }
197 }