001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krms.impl.ui;
017    
018    import org.apache.commons.collections.CollectionUtils;
019    import org.kuali.rice.core.api.uif.DataType;
020    import org.kuali.rice.core.api.uif.RemotableAttributeField;
021    import org.kuali.rice.core.api.uif.RemotableTextInput;
022    import org.kuali.rice.krad.document.MaintenanceDocument;
023    import org.kuali.rice.krad.maintenance.MaintainableImpl;
024    import org.kuali.rice.krad.service.BusinessObjectService;
025    import org.kuali.rice.krad.service.KRADServiceLocator;
026    import org.kuali.rice.krad.uif.container.CollectionGroup;
027    import org.kuali.rice.krad.uif.container.Container;
028    import org.kuali.rice.krad.uif.view.View;
029    import org.kuali.rice.krad.web.form.MaintenanceForm;
030    import org.kuali.rice.krms.impl.repository.ActionBo;
031    import org.kuali.rice.krms.impl.repository.CategoryBo;
032    import org.kuali.rice.krms.impl.repository.TermBo;
033    import org.kuali.rice.krms.impl.repository.TermResolverBo;
034    import org.kuali.rice.krms.impl.repository.TermResolverParameterSpecificationBo;
035    
036    import java.util.ArrayList;
037    import java.util.Collection;
038    import java.util.Collections;
039    import java.util.Comparator;
040    import java.util.List;
041    import java.util.Map;
042    
043    /**
044     * {@link org.kuali.rice.krad.maintenance.Maintainable} for the {@link org.kuali.rice.krms.impl.ui.AgendaEditor}
045     *
046     * @author Kuali Rice Team (rice.collab@kuali.org)
047     *
048     */
049    public class TermMaintainable extends MaintainableImpl {
050            
051            private static final long serialVersionUID = 1L;
052    
053        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(TermMaintainable.class);
054    
055            /**
056             * @return the boService
057             */
058            public BusinessObjectService getBoService() {
059                    return KRADServiceLocator.getBusinessObjectService();
060            }
061    
062    
063    
064        public List<RemotableAttributeField> retrieveCustomAttributes(View view, Object model, Container container) {
065    
066            List<RemotableAttributeField> results = new ArrayList<RemotableAttributeField>();
067    
068            String termSpecId = ((TermBo)((MaintenanceForm)model).getDocument().getNewMaintainableObject().getDataObject()).getSpecificationId();
069    
070            Collection<TermResolverBo> termResolvers = getBoService().findMatching(TermResolverBo.class,
071                    Collections.singletonMap("outputId", termSpecId)
072            );
073    
074            TermResolverBo termResolver = null;
075    
076            if (termResolvers != null && termResolvers.size() == 1) {
077                termResolver = termResolvers.iterator().next();
078            }
079    
080            if (termResolver != null && !CollectionUtils.isEmpty(termResolver.getParameterSpecifications())) {
081                List<TermResolverParameterSpecificationBo> params = new ArrayList<TermResolverParameterSpecificationBo>(termResolver.getParameterSpecifications());
082    
083                Collections.sort(params, new Comparator<TermResolverParameterSpecificationBo>() {
084                    @Override
085                    public int compare(TermResolverParameterSpecificationBo o1, TermResolverParameterSpecificationBo o2) {
086                        return o1.getName().compareTo(o2.getName());
087                    }
088                });
089    
090                for (TermResolverParameterSpecificationBo param : params) {
091                    RemotableAttributeField.Builder builder = RemotableAttributeField.Builder.create(param.getName());
092                    RemotableTextInput.Builder inputBuilder = RemotableTextInput.Builder.create();
093                    inputBuilder.setSize(80);
094                    builder.setControl(inputBuilder);
095                    builder.setDataType(DataType.STRING);
096                    builder.setLongLabel(param.getName());
097                    builder.setShortLabel(param.getName());
098    
099                    results.add(builder.build());
100                }
101            }
102    
103            return results;
104        }
105    
106    //    private AgendaTypeService getAgendaTypeService(String krmsTypeId) {
107    //        //
108    //        // Get the AgendaTypeService by hook or by crook
109    //        //
110    //
111    //        KrmsTypeDefinition krmsType =
112    //                    KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().
113    //                            getTypeById(krmsTypeId);
114    //
115    //        AgendaTypeService agendaTypeService = null;
116    //
117    //        if (!StringUtils.isBlank(krmsTypeId)) {
118    //            String serviceName = krmsType.getServiceName();
119    //
120    //            if (!StringUtils.isBlank(serviceName)) {
121    //                agendaTypeService = KrmsRepositoryServiceLocator.getService(serviceName);
122    //            }
123    //        }
124    //
125    //        if (agendaTypeService == null) { agendaTypeService = AgendaTypeServiceBase.defaultAgendaTypeService; }
126    //
127    //        return agendaTypeService;
128    //    }
129    
130        @Override
131        public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) {
132    
133            TermBo termBo = (TermBo)document.getNewMaintainableObject().getDataObject();
134            termBo.exportToParametersMap();
135    
136            return super.retrieveObjectForEditOrCopy(document,
137                    dataObjectKeys);    //To change body of overridden methods use File | Settings | File Templates.
138        }
139    
140        /**
141             * {@inheritDoc}
142             */
143            @Override
144            public void processAfterNew(MaintenanceDocument document,
145                    Map<String, String[]> requestParameters) {
146    
147                    super.processAfterNew(document, requestParameters);
148            document.getDocumentHeader().setDocumentDescription("New Term Document");
149    
150            }
151    
152        @Override
153        public void saveDataObject() {
154            TermBo term = (TermBo) getDataObject();
155            term.importFromParametersMap();
156    
157            super.saveDataObject();    //To change body of overridden methods use File | Settings | File Templates.
158        }
159    
160        @Override
161        public Class getDataObjectClass() {
162            return TermBo.class;
163        }
164    
165        @Override
166        protected void processBeforeAddLine(View view, CollectionGroup collectionGroup, Object model, Object addLine) {
167            super.processBeforeAddLine(view, collectionGroup, model, addLine);
168        }
169    
170    
171    }