001/**
002 * Copyright 2005-2017 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.impl.repository;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.kuali.rice.krad.inquiry.InquirableImpl;
022import org.kuali.rice.krad.uif.component.Component;
023import org.kuali.rice.krad.uif.container.Container;
024import org.kuali.rice.krad.uif.container.Group;
025import org.kuali.rice.krad.uif.element.Message;
026import org.kuali.rice.krad.uif.field.SpaceField;
027import org.kuali.rice.krad.uif.layout.GridLayoutManager;
028import org.kuali.rice.krad.uif.util.ComponentFactory;
029import org.kuali.rice.krad.uif.view.ViewModel;
030import org.kuali.rice.krad.web.form.InquiryForm;
031import org.kuali.rice.krms.api.repository.proposition.PropositionType;
032
033/**
034 * This class is responsible for building the recursive components on the Rule Inquiry View.
035 */
036public class RuleStudentInquiryViewHelperServiceImpl extends InquirableImpl {
037
038    private static int ID = 1;
039
040    private static final String PROPOSITION_GROUP_ID = "propositionGroup";
041
042    private static final String PROPOSITION_ID = "proposition";
043
044    private enum Operator {
045        OR("|"), AND("&");
046
047        private String code;
048
049        Operator(String code) {
050            this.code = code;
051        }
052
053        private static Operator fromCode(String code) {
054            if (code == null) {
055                return null;
056            }
057            for (Operator operator : values()) {
058                if (operator.code.equals(code)) {
059                    return operator;
060                }
061            }
062            throw new IllegalArgumentException("Failed to locate the Operator with the given code: " + code);
063        }
064    }
065
066
067
068    @Override
069    public void addCustomContainerComponents(ViewModel model, Container container) {
070        if (PROPOSITION_GROUP_ID.equals(container.getId())) {
071            RuleBo ruleDataObj = (RuleBo)((InquiryForm)model).getDataObject();
072            PropositionBo proposition = ruleDataObj.getProposition();
073            if (proposition != null) {
074                if (PropositionType.COMPOUND.getCode().equals(proposition.getPropositionTypeCode())) {
075                    List<Component> groups = new ArrayList<Component>();
076                    handleCompoundPropositions(groups, proposition);
077                    container.setItems(groups);
078                } else {
079                    Message simplePropName = ComponentFactory.getMessage();
080                    simplePropName.setId(PROPOSITION_ID + "_" + ID++);
081                    simplePropName.setMessageText(proposition.getDescription());
082                    List<Message> simpleProps = new ArrayList<Message>();
083                    simpleProps.add(simplePropName);
084                    container.setItems(simpleProps);
085                }
086            }
087        }
088    }
089
090    private void handleCompoundPropositions(List<Component> components, PropositionBo proposition) {
091        Group compoundGroup = getPropositionGroup();
092        compoundGroup.setId(String.valueOf(PROPOSITION_GROUP_ID + "_" + ID++));
093        ((GridLayoutManager)compoundGroup.getLayoutManager()).setNumberOfColumns(2);
094
095        List<Component> componentItems = new ArrayList<Component>();
096
097        //Heading
098        Message propositionName = ComponentFactory.getMessage();
099        propositionName.setId(PROPOSITION_ID + "_" + ID++);
100        propositionName.setMessageText(proposition.getDescription());
101
102        componentItems.add(propositionName);
103
104        //Space (for layout purposes)
105        SpaceField spaceField1 = ComponentFactory.getSpaceField();
106        spaceField1.setId("space" + "_" + ID++);
107        componentItems.add(spaceField1);
108
109        //Space (for layout purposes)
110        SpaceField spaceField2 = ComponentFactory.getSpaceField();
111        spaceField2.setId("space" + "_" + ID++);
112        componentItems.add(spaceField2);
113
114        if (proposition.getCompoundComponents() != null) {
115            int loopCounter = 0;
116            for (PropositionBo nestedProposition : proposition.getCompoundComponents()) {
117                if (loopCounter != 0) {
118
119                    //Space (for layout purposes)
120                    SpaceField spaceField3 = ComponentFactory.getSpaceField();
121                    spaceField3.setId("space" + "_" + ID++);
122                    componentItems.add(spaceField3);
123
124                    Message operator = ComponentFactory.getMessage();
125                    operator.setId(PROPOSITION_ID + "_" + ID++);
126                    operator.setMessageText(Operator.fromCode(proposition.getCompoundOpCode()).toString());
127                    componentItems.add(operator);
128
129                    //Space (for layout purposes)
130                    SpaceField spaceField4 = ComponentFactory.getSpaceField();
131                    spaceField4.setId("space" + "_" + ID++);
132                    componentItems.add(spaceField4);
133                }
134                if (PropositionType.COMPOUND.getCode().equals(nestedProposition.getPropositionTypeCode())) {
135                    handleCompoundPropositions(components, nestedProposition);
136                } else {
137                    Message simplePropName = ComponentFactory.getMessage();
138                    simplePropName.setId(PROPOSITION_ID + "_" + ID++);
139                    simplePropName.setMessageText(nestedProposition.getDescription());
140
141                    componentItems.add(simplePropName);
142                }
143                loopCounter++;
144            }
145        }
146        compoundGroup.setItems(componentItems);
147
148        components.add(compoundGroup);
149    }
150
151    private static Group getPropositionGroup() {
152        Group group = (Group) ComponentFactory.getGroupGridBodyOnly();
153        group.getDisclosure().setId(String.valueOf(ID++));
154        return group;
155    }
156
157}