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.apache.commons.lang.StringUtils;
019import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
020import org.kuali.rice.krad.uif.UifConstants;
021import org.kuali.rice.krad.uif.component.BindingInfo;
022import org.kuali.rice.krad.uif.component.Component;
023import org.kuali.rice.krad.uif.container.CollectionGroup;
024import org.kuali.rice.krad.uif.container.Group;
025import org.kuali.rice.krad.uif.container.GroupBase;
026import org.kuali.rice.krad.uif.field.DataField;
027import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
028import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleRestriction;
029import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleUtils;
030import org.kuali.rice.krad.uif.util.LifecycleElement;
031import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
032import org.kuali.rice.krad.uif.view.View;
033import org.kuali.rice.krms.dto.AgendaEditor;
034import org.kuali.rice.krms.service.RuleViewHelperService;
035
036import java.util.ArrayList;
037import java.util.List;
038
039/**
040 * @author Kuali Student Team
041 */
042public class AgendaSection extends GroupBase {
043
044    private String propertyName;
045    private BindingInfo bindingInfo;
046
047    private Group agendaPrototype;
048    private Group rulePrototype;
049
050    private AgendaBuilder agendaBuilder;
051
052    public AgendaSection(){
053        super();
054    }
055
056    @Override
057    public void performInitialization(Object model) {
058        setFieldBindingObjectPath(getBindingInfo().getBindingObjectPath());
059
060        super.performInitialization(model);
061
062        View view = ViewLifecycle.getView();
063
064        if (bindingInfo != null) {
065            bindingInfo.setDefaults(view, getPropertyName());
066        }
067
068        setCollectionPath();
069
070    }
071
072    protected void setCollectionPath() {
073        // set static collection path on items
074        String collectionPath = "";
075        if (StringUtils.isNotBlank(getBindingInfo().getCollectionPath())) {
076            collectionPath += getBindingInfo().getCollectionPath() + ".";
077        }
078        if (StringUtils.isNotBlank(getBindingInfo().getBindByNamePrefix())) {
079            collectionPath += getBindingInfo().getBindByNamePrefix() + ".";
080        }
081        collectionPath += getBindingInfo().getBindingName();
082
083        List<DataField> agendafields = ViewLifecycleUtils.getElementsOfTypeDeep(agendaPrototype, DataField.class);
084        for (DataField collectionField : agendafields) {
085            collectionField.getBindingInfo().setCollectionPath(collectionPath);
086        }
087
088        List<DataField> rulefields = ViewLifecycleUtils.getElementsOfTypeDeep(rulePrototype, DataField.class);
089        for (DataField collectionField : rulefields) {
090            collectionField.getBindingInfo().setCollectionPath(collectionPath);
091        }
092    }
093
094    @Override
095    public void performApplyModel(Object model, LifecycleElement parent) {
096        // get the collection for this group from the model
097        List<Object> modelCollection = ObjectPropertyUtils.getPropertyValue(model,
098                this.getBindingInfo().getBindingPath());
099        if (null == modelCollection)
100        {
101            modelCollection = new ArrayList<Object>();
102        }
103        View view = ViewLifecycle.getView();
104
105        //Set the ruleviewhelperservice on the agendabuilder.
106        this.getAgendaBuilder().setViewHelperService((RuleViewHelperService) view.getViewHelperService());
107
108        // create agenda sections for each agenda
109        List<Component> items = new ArrayList<Component>();
110        for (int index = 0; index < modelCollection.size(); index++) {
111            AgendaEditor agenda = (AgendaEditor) modelCollection.get(index);
112            items.add(this.getAgendaBuilder().buildAgenda(agenda, index, this));
113        }
114        this.setItems(items);
115
116        super.performApplyModel(model, parent);
117    }
118
119
120    public AgendaBuilder getAgendaBuilder() {
121        if (this.agendaBuilder == null) {
122            this.agendaBuilder = new AgendaBuilder();
123        }
124        return this.agendaBuilder;
125    }
126
127    public String getPropertyName() {
128        return propertyName;
129    }
130
131    public void setPropertyName(String propertyName) {
132        this.propertyName = propertyName;
133    }
134
135    public BindingInfo getBindingInfo() {
136        return bindingInfo;
137    }
138
139    public void setBindingInfo(BindingInfo bindingInfo) {
140        this.bindingInfo = bindingInfo;
141    }
142
143    @ViewLifecycleRestriction(UifConstants.ViewPhases.INITIALIZE)
144    @BeanTagAttribute(name = "agendaPrototype", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
145    public Group getAgendaPrototype() {
146        return agendaPrototype;
147    }
148
149    public void setAgendaPrototype(Group agendaPrototype) {
150        this.agendaPrototype = agendaPrototype;
151    }
152
153    @ViewLifecycleRestriction(UifConstants.ViewPhases.INITIALIZE)
154    @BeanTagAttribute(name = "rulePrototype", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
155    public Group getRulePrototype() {
156        return rulePrototype;
157    }
158
159    public void setRulePrototype(Group rulePrototype) {
160        this.rulePrototype = rulePrototype;
161    }
162}