001/**
002 * Copyright 2005-2013 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 */
016package org.kuali.rice.krms.util;
017
018import org.kuali.rice.krad.uif.component.BindingInfo;
019import org.kuali.rice.krad.uif.component.Component;
020import org.kuali.rice.krad.uif.container.CollectionFilter;
021import org.kuali.rice.krad.uif.container.Group;
022import org.kuali.rice.krad.uif.container.TreeGroup;
023import org.kuali.rice.krad.uif.element.Action;
024import org.kuali.rice.krad.uif.element.Message;
025import org.kuali.rice.krad.uif.field.DataField;
026import org.kuali.rice.krad.uif.util.ComponentUtils;
027import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
028import org.kuali.rice.krad.uif.view.View;
029import org.kuali.rice.krad.uif.widget.QuickFinder;
030import org.kuali.rice.krms.dto.AgendaEditor;
031import org.kuali.rice.krms.service.RuleViewHelperService;
032
033import java.util.ArrayList;
034import java.util.List;
035
036/**
037 * @author Kuali Student Team
038 */
039public class AgendaSection extends Group {
040
041    private String propertyName;
042    private BindingInfo bindingInfo;
043
044    private Group agendaPrototype;
045    private Group rulePrototype;
046
047    private AgendaBuilder agendaBuilder;
048
049    public AgendaSection(){
050        super();
051    }
052
053    @Override
054    public void performInitialization(View view, Object model) {
055        setFieldBindingObjectPath(getBindingInfo().getBindingObjectPath());
056
057        super.performInitialization(view, model);
058
059        if (bindingInfo != null) {
060            bindingInfo.setDefaults(view, getPropertyName());
061        }
062
063    }
064
065    @Override
066    public void performApplyModel(View view, Object model, Component parent) {
067        // get the collection for this group from the model
068        List<Object> modelCollection = ObjectPropertyUtils.getPropertyValue(model,
069                this.getBindingInfo().getBindingPath());
070
071        //Set the ruleviewhelperservice on the agendabuilder.
072        this.getAgendaBuilder().setViewHelperService((RuleViewHelperService) view.getViewHelperService());
073
074        // create agenda sections for each agenda
075        List<Component> items = new ArrayList<Component>();
076        for (int index = 0; index < modelCollection.size(); index++) {
077            AgendaEditor agenda = (AgendaEditor) modelCollection.get(index);
078            items.add(this.getAgendaBuilder().buildAgenda(agenda, index, this));
079        }
080        this.setItems(items);
081
082        super.performApplyModel(view, model, parent);
083    }
084
085    @Override
086    public List<Component> getComponentsForLifecycle() {
087        return super.getComponentsForLifecycle();
088    }
089
090    @Override
091    public List<Component> getComponentPrototypes() {
092        List<Component> components = super.getComponentPrototypes();
093        components.add(this.getAgendaPrototype());
094        components.add(this.getRulePrototype());
095
096        return components;
097    }
098
099    /**
100     * @see org.kuali.rice.krad.uif.component.ComponentBase#copy()
101     */
102    @Override
103    protected <T> void copyProperties(T component) {
104        super.copyProperties(component);
105
106        AgendaSection agendaSectionCopy = (AgendaSection) component;
107        agendaSectionCopy.setPropertyName(this.propertyName);
108        if (this.agendaPrototype != null){
109            agendaSectionCopy.setAgendaPrototype((Group) this.agendaPrototype.copy());
110        }
111        if (this.rulePrototype != null){
112            agendaSectionCopy.setRulePrototype((Group) this.rulePrototype.copy());
113        }
114        if (this.bindingInfo != null) {
115            agendaSectionCopy.setBindingInfo((BindingInfo) this.bindingInfo.copy());
116        }
117    }
118
119    public AgendaBuilder getAgendaBuilder() {
120        if (this.agendaBuilder == null) {
121            this.agendaBuilder = new AgendaBuilder();
122        }
123        return this.agendaBuilder;
124    }
125
126    public String getPropertyName() {
127        return propertyName;
128    }
129
130    public void setPropertyName(String propertyName) {
131        this.propertyName = propertyName;
132    }
133
134    public BindingInfo getBindingInfo() {
135        return bindingInfo;
136    }
137
138    public void setBindingInfo(BindingInfo bindingInfo) {
139        this.bindingInfo = bindingInfo;
140    }
141
142    public Group getAgendaPrototype() {
143        return agendaPrototype;
144    }
145
146    public void setAgendaPrototype(Group agendaPrototype) {
147        this.agendaPrototype = agendaPrototype;
148    }
149
150    public Group getRulePrototype() {
151        return rulePrototype;
152    }
153
154    public void setRulePrototype(Group rulePrototype) {
155        this.rulePrototype = rulePrototype;
156    }
157}