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