View Javadoc

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