Coverage Report - org.kuali.student.core.statement.ui.client.widgets.rules.ReqCompEditWidget
 
Classes in this File Line Coverage Branch Coverage Complexity
ReqCompEditWidget
0%
0/259
0%
0/92
2.921
ReqCompEditWidget$1
0%
0/10
0%
0/8
2.921
ReqCompEditWidget$2
0%
0/12
0%
0/4
2.921
ReqCompEditWidget$3
0%
0/16
0%
0/8
2.921
ReqCompEditWidget$4
0%
0/14
0%
0/4
2.921
ReqCompEditWidget$5
0%
0/3
N/A
2.921
ReqCompEditWidget$6
0%
0/16
0%
0/2
2.921
ReqCompEditWidget$ReqCompEditView
0%
0/1
N/A
2.921
 
 1  
 package org.kuali.student.core.statement.ui.client.widgets.rules;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.HashMap;
 5  
 import java.util.Iterator;
 6  
 import java.util.List;
 7  
 import java.util.Map;
 8  
 
 9  
 import org.kuali.student.common.assembly.data.Data;
 10  
 import org.kuali.student.common.assembly.data.Metadata;
 11  
 import org.kuali.student.common.assembly.data.QueryPath;
 12  
 import org.kuali.student.common.dto.RichTextInfo;
 13  
 import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor;
 14  
 import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle;
 15  
 import org.kuali.student.common.ui.client.configurable.mvc.layouts.BasicLayout;
 16  
 import org.kuali.student.common.ui.client.configurable.mvc.views.VerticalSectionView;
 17  
 import org.kuali.student.common.ui.client.mvc.Callback;
 18  
 import org.kuali.student.common.ui.client.mvc.DataModel;
 19  
 import org.kuali.student.common.ui.client.mvc.DataModelDefinition;
 20  
 import org.kuali.student.common.ui.client.mvc.ModelProvider;
 21  
 import org.kuali.student.common.ui.client.mvc.ModelRequestCallback;
 22  
 import org.kuali.student.common.ui.client.widgets.KSDropDown;
 23  
 import org.kuali.student.common.ui.client.widgets.KSLabel;
 24  
 import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonEnumerations;
 25  
 import org.kuali.student.common.ui.client.widgets.field.layout.button.ActionCancelGroup;
 26  
 import org.kuali.student.common.ui.client.widgets.field.layout.element.MessageKeyInfo;
 27  
 import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract;
 28  
 import org.kuali.student.common.ui.client.widgets.list.ListItems;
 29  
 import org.kuali.student.common.ui.client.widgets.list.SelectionChangeEvent;
 30  
 import org.kuali.student.common.ui.client.widgets.list.SelectionChangeHandler;
 31  
 import org.kuali.student.common.validation.dto.ValidationResultInfo;
 32  
 import org.kuali.student.core.statement.dto.ReqCompFieldInfo;
 33  
 import org.kuali.student.core.statement.dto.ReqCompFieldTypeInfo;
 34  
 import org.kuali.student.core.statement.dto.ReqComponentInfo;
 35  
 import org.kuali.student.core.statement.dto.ReqComponentTypeInfo;
 36  
 
 37  
 import com.google.gwt.core.client.GWT;
 38  
 import com.google.gwt.user.client.Window;
 39  
 import com.google.gwt.user.client.ui.FlowPanel;
 40  
 import com.google.gwt.user.client.ui.Widget;
 41  
 
 42  0
 public class ReqCompEditWidget extends FlowPanel {
 43  
 
 44  
     //widgets
 45  0
     private FlowPanel reqCompTypePanel = new FlowPanel();
 46  0
     private FlowPanel ReqCompEditPanel = new FlowPanel();
 47  0
     private KSDropDown reqCompTypesList = new KSDropDown();
 48  
     private VerticalSectionView reqCompFieldsPanel;
 49  0
     private ActionCancelGroup actionCancelButtons = new ActionCancelGroup(ButtonEnumerations.AddCancelEnum.ADD, ButtonEnumerations.AddCancelEnum.CANCEL);
 50  0
     private final String NO_SELECTION_TEXT = "Select rule type";
 51  
 
 52  
     //data
 53  
     private List<ReqComponentTypeInfo> reqCompTypeInfoList;     //list of all Requirement Component Types based on selected Statement Type
 54  
     private ListItems listItemReqCompTypes;                         //list of all Requirement Component Types wrapped in a list for drop down
 55  0
     private Map<String, String> compositionTemplates = new HashMap<String, String>();
 56  
     private ReqComponentInfo editedReqComp;                                                //Req. Component that user is editing or adding
 57  
     private ReqComponentTypeInfo selectedReqCompType;           //the type of Req. Component that the user is editing or adding
 58  
     private List<String> selectedReqCompFieldTypes;             //types of all fields for given selected req. component type
 59  
     private boolean addingNewReqComp;                           //adding (true) or editing (false) req. component
 60  
     private DataModel ruleFieldsData;
 61  
     private BasicLayout reqCompFieldsLayout;
 62  0
     private Map<String, Widget> customWidgets = new HashMap<String, Widget>();
 63  
     private Map<String, Widget> customWidgetsNew;
 64  
 
 65  
     //other
 66  
     private Callback reqCompConfirmCallback;
 67  
     private Callback newReqCompSelectedCallback;
 68  
     private Callback fieldsMetadataTemplateCallback;
 69  
     private Callback compositionTemplateCallback;
 70  
     private Callback displayCustomWidgetCallback;
 71  
     private String newReqCompId;
 72  0
     private static int tempReqCompInfoID = 999999;
 73  
     private static final String REQ_COMP_MODEL_ID = "reqCompModelId";
 74  
 
 75  0
     private enum ReqCompEditView {VIEW}
 76  
 
 77  
     //TODO use app context for text
 78  
 
 79  
     public ReqCompEditWidget(String newReqCompId) {
 80  0
         super();
 81  
 
 82  0
         ruleFieldsData = new DataModel();
 83  0
         ruleFieldsData.setRoot(new Data());
 84  0
         this.newReqCompId = newReqCompId;
 85  
    //     this.relatedProgramReqInfoId = relatedProgramReqInfoId;
 86  
 
 87  
         //wait until req. comp. types are loaded and user actually selects a type from drop down
 88  0
         reqCompTypesList.setEnabled(false);
 89  0
         setEnableAddRuleButtons(false);
 90  
 
 91  0
         setupReqCompTypesList();
 92  0
         setupHandlers();
 93  
 
 94  0
         displayReqCompListPanel();
 95  0
         add(ReqCompEditPanel);
 96  0
         displayConfirmButton();
 97  0
     }
 98  
 
 99  
     private void setupHandlers() {
 100  
 
 101  0
         actionCancelButtons.addCallback(new Callback<ButtonEnumerations.ButtonEnum>(){
 102  
                 @Override
 103  
                public void exec(ButtonEnumerations.ButtonEnum result) {
 104  
 
 105  0
                     setEnableAddRuleButtons(false);
 106  
 
 107  0
                     if (result == ButtonEnumerations.AddCancelEnum.ADD) {
 108  
 
 109  
                         //true if we have no fields for this req. component type
 110  0
                         if (((selectedReqCompFieldTypes == null) || selectedReqCompFieldTypes.size() == 0) && (customWidgets.size() == 0)) {
 111  0
                             finalizeRuleUpdate();
 112  0
                             return;
 113  
                         }
 114  
 
 115  
                         //validate and retrieve fields
 116  0
                         validateAndRetrieveFields();
 117  
 
 118  
                     } else { //user canceled add/edit rule action
 119  0
                         setupNewReqComp();
 120  0
                         reqCompConfirmCallback.exec(null);
 121  
                     }
 122  0
                }
 123  
         });
 124  
 
 125  0
         reqCompTypesList.addSelectionChangeHandler(new SelectionChangeHandler() {
 126  
                         @Override
 127  
             public void onSelectionChange(SelectionChangeEvent event) {
 128  
 
 129  0
                             if (!event.isUserInitiated()) {
 130  0
                                 return;
 131  
                             }
 132  
 
 133  
                 //disable Step 2 section and enable 'Add Rule' and 'cancel' buttons
 134  0
                  newReqCompSelectedCallback.exec(null);
 135  0
                  setEnableAddRuleButtons(true);
 136  
 
 137  0
                  List<String> ids = ((KSSelectItemWidgetAbstract)event.getWidget()).getSelectedItems();
 138  0
                  selectedReqCompType = reqCompTypeInfoList.get(Integer.valueOf(ids.get(0)));
 139  
 
 140  0
                  if (addingNewReqComp) {
 141  0
                      createReqComp(selectedReqCompType);
 142  
                  } else {
 143  
                          //editedReqComp.setRequiredComponentType(selectedReqCompType);
 144  0
                      editedReqComp.setType(selectedReqCompType.getId());
 145  
                  }
 146  
 
 147  0
                  displayFieldsSection();
 148  0
          }});
 149  
 
 150  0
     }
 151  
 
 152  
     private void validateAndRetrieveFields() {
 153  
 
 154  0
         final List<ReqCompFieldInfo> editedFields = new ArrayList<ReqCompFieldInfo>();
 155  0
         reqCompFieldsLayout.updateModel();
 156  
 
 157  
         //1. validate and retrieve non-custom fields
 158  0
         if (ruleFieldsData.getRoot().size() > 0) {
 159  0
             ruleFieldsData.validate(new Callback<List<ValidationResultInfo>>() {
 160  
                 @Override
 161  
                 public void exec(List<ValidationResultInfo> validationResults) {
 162  
 
 163  
                     //do not proceed if the user input is not valid
 164  0
                     if (!reqCompFieldsLayout.isValid(validationResults, true, true)) {
 165  0
                         setEnableAddRuleButtons(true);
 166  0
                         return;
 167  
                     }
 168  
 
 169  
                     //2. retrieve entered values and set the rule info
 170  0
                     for (ReqCompFieldTypeInfo fieldTypeInfo : selectedReqCompType.getReqCompFieldTypeInfos()) {
 171  
 
 172  
                         //we handle custom widgets elsewhere in order to deal with callbacks
 173  0
                         if (customWidgets.containsKey(fieldTypeInfo.getId())) {
 174  0
                             continue;
 175  
                         }
 176  
 
 177  0
                         ReqCompFieldInfo fieldInfo = new ReqCompFieldInfo();
 178  0
                         fieldInfo.setId(null);
 179  0
                         fieldInfo.setType(fieldTypeInfo.getId());
 180  0
                         String fieldValue = ruleFieldsData.getRoot().get(fieldTypeInfo.getId()).toString();
 181  0
                         fieldInfo.setValue((fieldValue == null ? "" : fieldValue.toString()));
 182  0
                         editedFields.add(fieldInfo);
 183  0
                     }
 184  
 
 185  
                     //2. retrieve non-custom fields
 186  0
                     retrieveValuesFromCustomWidgets(editedFields);
 187  0
                 }
 188  
             });
 189  
         } else {
 190  
             //2. retrieve non-custom fields
 191  0
             retrieveValuesFromCustomWidgets(editedFields);
 192  
         }
 193  0
     }
 194  
 
 195  
     private void retrieveValuesFromCustomWidgets(final List<ReqCompFieldInfo> editedFields) {
 196  
 
 197  0
         if (customWidgets.size() == 0) {
 198  0
             editedReqComp.setReqCompFields(editedFields);
 199  0
             finalizeRuleUpdate();
 200  0
             return;
 201  
         }
 202  
 
 203  0
         final Iterator iter = customWidgets.entrySet().iterator();
 204  0
         while (iter.hasNext()) {
 205  0
             final String fieldType = (String)((Map.Entry)iter.next()).getKey();
 206  0
             ((AccessWidgetValue)customWidgets.get(fieldType)).getValue(new Callback<String>() {
 207  
                 @Override
 208  
                 public void exec(String widgetValue) {
 209  
                     //clu set validation failed...
 210  0
                     if (widgetValue == null) {
 211  0
                         setEnableAddRuleButtons(true);
 212  0
                         return;
 213  
                     }
 214  0
                     ReqCompFieldInfo fieldInfo = new ReqCompFieldInfo();
 215  0
                     fieldInfo.setId(null);
 216  0
                     fieldInfo.setType(fieldType);
 217  0
                     fieldInfo.setValue(widgetValue);
 218  0
                     editedFields.add(fieldInfo);
 219  
 
 220  
                     //TODO not sure if this will work all the time due to parallel nature of this code but running out of time
 221  0
                     iter.remove();
 222  0
                     if (customWidgets.size() == 0) {
 223  
                         //3. update req. component being edited
 224  0
                         editedReqComp.setReqCompFields(editedFields);
 225  0
                         finalizeRuleUpdate();
 226  
                     }
 227  0
                 }
 228  
             });
 229  0
         }
 230  0
     }
 231  
 
 232  
     private void finalizeRuleUpdate() {
 233  0
         editedReqComp.setType(selectedReqCompType.getId());
 234  
 
 235  
         //callback needs to update NL for given req. component and the rule
 236  0
         reqCompConfirmCallback.exec(editedReqComp);
 237  0
     }
 238  
 
 239  
     public void setupNewReqComp() {
 240  0
         addingNewReqComp = true;
 241  0
         selectedReqCompType = null;
 242  0
         createReqComp(null);
 243  0
         ReqCompEditPanel.clear();
 244  0
         redraw();
 245  0
     }
 246  
 
 247  
     /* create a new Req. Component Type based on user selection or empty at first */
 248  
     private void createReqComp(ReqComponentTypeInfo reqCompTypeInfo) {
 249  0
         RichTextInfo desc = new RichTextInfo();
 250  0
         desc.setPlain("");
 251  0
         desc.setFormatted("");
 252  0
         editedReqComp = new ReqComponentInfoUi();
 253  0
         editedReqComp.setDesc(desc);
 254  0
         editedReqComp.setId(newReqCompId + Integer.toString(tempReqCompInfoID++));
 255  0
         editedReqComp.setReqCompFields(null);
 256  
         //editedReqComp.setRequiredComponentType(reqCompTypeInfo);
 257  0
         if (reqCompTypeInfo != null) {
 258  0
             editedReqComp.setType(reqCompTypeInfo.getId());
 259  
         }
 260  0
     }
 261  
 
 262  
     //call when user wants to edit an existing req. component
 263  
     public void setupExistingReqComp(ReqComponentInfo existingReqComp) {
 264  0
         if (!actionCancelButtons.getButton(ButtonEnumerations.AddCancelEnum.ADD).isEnabled()) {
 265  0
             return;
 266  
         }
 267  
 
 268  0
         addingNewReqComp = false;
 269  0
         editedReqComp = existingReqComp;
 270  
         //originalReqType = editedReqComp.getRequiredComponentType();
 271  
 
 272  0
         selectedReqCompType = null;
 273  0
         for (ReqComponentTypeInfo aReqCompTypeInfoList : reqCompTypeInfoList) {
 274  0
             if (editedReqComp.getType().equals(aReqCompTypeInfoList.getId())) {
 275  0
                 selectedReqCompType = aReqCompTypeInfoList;
 276  0
                 break;
 277  
             }
 278  
         }
 279  0
         if (selectedReqCompType == null) {
 280  0
             GWT.log("Unknown Requirement Component Type found: " + existingReqComp.getType(), null);
 281  0
             Window.alert("Unknown Requirement Component Type found: " + existingReqComp.getType());
 282  
         }
 283  
 
 284  0
         redraw();
 285  0
     }
 286  
 
 287  
     private void redraw() {
 288  
 
 289  0
         selectReqCompTypeInList();
 290  
 
 291  0
         displayFieldsSection();
 292  
 
 293  0
         if (addingNewReqComp) {
 294  0
             actionCancelButtons.getButton(ButtonEnumerations.AddCancelEnum.ADD).setText("Add Rule");
 295  0
             setEnableAddRuleButtons(false);
 296  
         } else {
 297  0
             actionCancelButtons.getButton(ButtonEnumerations.AddCancelEnum.ADD).setText("Update Rule");
 298  0
             setEnableAddRuleButtons(false);
 299  0
             if (selectedReqCompType != null) {
 300  0
                 setEnableAddRuleButtons(true);
 301  
             }
 302  
         }
 303  0
     }
 304  
 
 305  
     public void displayFieldsSection() {
 306  
 
 307  
         //if no req. comp. type is selected then don't display anything
 308  0
         if (selectedReqCompType == null) {
 309  0
             return;
 310  
         }
 311  
 
 312  
         //no display if the req. comp. type has no fields
 313  0
         if ((selectedReqCompType.getReqCompFieldTypeInfos() == null) || selectedReqCompType.getReqCompFieldTypeInfos().isEmpty()) {
 314  0
             ReqCompEditPanel.clear();
 315  0
             selectedReqCompFieldTypes = new ArrayList<String>();
 316  0
             return;
 317  
         }
 318  
 
 319  
         //get composition template either from local cache or from service through user of this widget
 320  0
         String compositionTemplate = compositionTemplates.get(selectedReqCompType.getId());
 321  0
         if (compositionTemplate == null) {
 322  0
             compositionTemplateCallback.exec(editedReqComp);
 323  0
             return;
 324  
         }
 325  
 
 326  0
         displayFieldsStart(compositionTemplate);
 327  0
     }
 328  
 
 329  
     public void displayFieldsStart(String compositionTemplate) {
 330  
 
 331  0
         compositionTemplates.put(selectedReqCompType.getId(), compositionTemplate);
 332  0
         setEnableAddRuleButtons(true);
 333  
 
 334  0
         selectedReqCompFieldTypes = new ArrayList<String>();
 335  0
         for (ReqCompFieldTypeInfo fieldTypeInfo : selectedReqCompType.getReqCompFieldTypeInfos()) {
 336  0
             selectedReqCompFieldTypes.add(fieldTypeInfo.getId());
 337  
         }
 338  
 
 339  0
         fieldsMetadataTemplateCallback.exec(selectedReqCompFieldTypes);
 340  0
     }
 341  
 
 342  
     public void displayFieldsEnd(List<Metadata> fieldsMetadataList) {
 343  
 
 344  0
         customWidgets = new HashMap<String, Widget>();
 345  0
         List<ReqCompFieldInfo> reqCompFields = (editedReqComp == null ? null : editedReqComp.getReqCompFields());
 346  0
         reqCompFieldsPanel = new VerticalSectionView(ReqCompEditView.VIEW, "", REQ_COMP_MODEL_ID, false);
 347  0
         reqCompFieldsPanel.addStyleName("KS-Rule-FieldsList");
 348  0
         Map<String, FieldDescriptor> fields = new HashMap<String, FieldDescriptor>();
 349  
 
 350  0
         int ix = 0;
 351  0
         Metadata fieldGradeTypeMetadata = null;
 352  0
         Metadata fieldGradeMetadata = null;
 353  0
         ReqCompEditPanel.clear();
 354  0
         Map<String, Metadata> fieldDefinitionMetadata = new HashMap<String,Metadata>();
 355  0
         for (Metadata oneFieldMetadata : fieldsMetadataList) {
 356  
 
 357  0
             Metadata fieldMetadata = oneFieldMetadata.getProperties().get("value");
 358  0
             String fieldType = selectedReqCompFieldTypes.get(ix++);
 359  
 
 360  
             //add clusets separately
 361  0
             if (RulesUtil.isCluSetWidget(fieldType)) {
 362  0
                 displayCustomWidgetCallback.exec(fieldType);
 363  0
                 continue;
 364  
             }
 365  
 
 366  0
             String fieldLabel = getFieldLabel(fieldType);
 367  
             FieldDescriptor fd;
 368  0
             if (RulesUtil.isGradeWidget(fieldType)) {
 369  
 
 370  0
                 if (fieldType.equals("kuali.reqComponent.field.type.gradeType.id")) {
 371  0
                     fieldGradeTypeMetadata = fieldMetadata;
 372  
                 }
 373  0
                 if (fieldType.equals("kuali.reqComponent.field.type.grade.id")) {
 374  0
                     fieldGradeMetadata = fieldMetadata;
 375  
                 }
 376  
 
 377  0
                 if ((fieldGradeTypeMetadata != null) && (fieldGradeMetadata != null)) {
 378  0
                     fieldType = "kuali.reqComponent.field.type.grade.id";
 379  0
                     Widget gradeWidget = customWidgetsNew.get(fieldType);
 380  0
                     List<Metadata> fieldsMetadata = new ArrayList<Metadata>();
 381  0
                     fieldsMetadata.add(fieldGradeTypeMetadata);
 382  0
                     fieldsMetadata.add(fieldGradeMetadata);
 383  0
                     ((AccessWidgetValue)gradeWidget).initWidget(fieldsMetadata);
 384  0
                     fieldLabel = getFieldLabel(fieldType);
 385  0
                     fieldMetadata = fieldGradeMetadata;
 386  0
                     fd = new FieldDescriptor(fieldType, new MessageKeyInfo(fieldLabel), fieldMetadata, gradeWidget);
 387  0
                 } else {
 388  
                     continue;
 389  
                 }
 390  0
             } else if (RulesUtil.isCluWidget(fieldType)) {
 391  0
                 Widget customWidget = customWidgetsNew.get(fieldType);
 392  0
                 ((AccessWidgetValue)customWidget).initWidget(null);
 393  0
                 fd = new FieldDescriptor(fieldType, new MessageKeyInfo(fieldLabel), fieldMetadata, customWidget);
 394  0
             } else {
 395  0
                 fd = new FieldDescriptor(fieldType, new MessageKeyInfo(fieldLabel), fieldMetadata);
 396  
             }
 397  
 
 398  
             //reqCompFieldsPanel.addField(fd);
 399  0
             fields.put(fieldType, fd);
 400  
 
 401  
             //add field to the data model metadata
 402  0
             fieldDefinitionMetadata.put(fieldType, fieldMetadata);
 403  0
         }
 404  
 
 405  
         //now we add fields to the panel in proper order based on composition template
 406  0
         for (String type : getFieldSequence()) {
 407  0
             if (RulesUtil.isCluSetWidget(type) || type.equals("kuali.reqComponent.field.type.gradeType.id")) {
 408  0
                 continue;
 409  
             }
 410  0
             reqCompFieldsPanel.addField(fields.get(type));
 411  
         }
 412  
 
 413  
         //setup data model
 414  0
         Metadata modelDefinitionMetadata = new Metadata();
 415  0
         modelDefinitionMetadata.setCanView(true);
 416  0
         modelDefinitionMetadata.setDataType(Data.DataType.DATA);
 417  0
         modelDefinitionMetadata.setProperties(fieldDefinitionMetadata);
 418  0
         ruleFieldsData = new DataModel();
 419  0
         ruleFieldsData.setRoot(new Data());
 420  0
         ruleFieldsData.setDefinition(new DataModelDefinition(modelDefinitionMetadata));
 421  
 
 422  0
         if (selectedReqCompFieldTypes.contains("kuali.reqComponent.field.type.grade.id")) {
 423  0
             selectedReqCompFieldTypes.remove("kuali.reqComponent.field.type.gradeType.id");
 424  
         }
 425  
 
 426  
         //initialize fields with values if user is editing an existing rule
 427  0
         if (!addingNewReqComp) {
 428  0
             for (String fieldType : selectedReqCompFieldTypes) {
 429  0
                 String fieldValue = getFieldValue(reqCompFields, fieldType);
 430  0
                 if (fieldValue != null) {
 431  0
                     if (RulesUtil.isCluSetWidget(fieldType)) {
 432  0
                         ((AccessWidgetValue)customWidgets.get(fieldType)).setValue(fieldValue);
 433  0
                     } else if (RulesUtil.isCluWidget(fieldType)) {
 434  0
                         ((AccessWidgetValue)customWidgetsNew.get(fieldType)).setValue(fieldValue);
 435  0
                         ruleFieldsData.set(QueryPath.parse(fieldType), fieldValue);
 436  
                     } else {
 437  0
                         ruleFieldsData.set(QueryPath.parse(fieldType), fieldValue);
 438  
                     }
 439  
                 }
 440  0
             }
 441  
         }
 442  
 
 443  
         //setup controller
 444  0
         reqCompFieldsLayout = new BasicLayout(null);
 445  0
         reqCompFieldsLayout.addView(reqCompFieldsPanel);
 446  0
         reqCompFieldsLayout.setDefaultModelId(REQ_COMP_MODEL_ID);
 447  0
         reqCompFieldsLayout.registerModel(REQ_COMP_MODEL_ID, new ModelProvider<DataModel>() {
 448  
             @Override
 449  
             public void requestModel(final ModelRequestCallback<DataModel> callback) {
 450  0
                 callback.onModelReady(ruleFieldsData);
 451  0
             }
 452  
         });
 453  
 
 454  
         //show fields
 455  0
         ReqCompEditPanel.add(reqCompFieldsLayout);
 456  0
         reqCompFieldsLayout.showView(ReqCompEditView.VIEW);
 457  
 
 458  
         //TODO save history
 459  0
     }
 460  
 
 461  
     //TODO we should mix the custom widgets with regular widgets
 462  
     public void displayCustomWidget(String fieldType, Widget customWidget) {
 463  0
         KSLabel label = new KSLabel(getFieldLabel(fieldType));
 464  0
         label.addStyleName("KS-Rule-ReqComp-Custom-Widget-label");
 465  0
         ReqCompEditPanel.add(label);
 466  0
         customWidgets.put(fieldType, customWidget);
 467  0
         ReqCompEditPanel.add(customWidget);
 468  0
     }
 469  
 
 470  
     private String getFieldLabel(String fieldType) {
 471  0
         String compositionTemplate = compositionTemplates.get(selectedReqCompType.getId());
 472  0
         String label = compositionTemplate.substring(compositionTemplate.indexOf(fieldType) + fieldType.length());
 473  0
         int ix = label.indexOf("reqCompFieldLabel") + "reqCompFieldLabel".length();
 474  0
         int ix2 = label.indexOf(">", ix);
 475  0
         return label.substring(ix, ix2).replace("=", "").trim();
 476  
     }
 477  
 
 478  
     private List<String> getFieldSequence() {
 479  0
         List<String> fieldTypes = new ArrayList<String>();
 480  0
         String compositionTemplate = compositionTemplates.get(selectedReqCompType.getId());
 481  
 
 482  0
         int stIx = 0;
 483  0
         while (compositionTemplate.indexOf("reqCompFieldType", stIx) > 0) {
 484  0
             stIx = compositionTemplate.indexOf("reqCompFieldType") + "reqCompFieldType".length();
 485  0
             compositionTemplate = compositionTemplate.substring(stIx);
 486  0
             stIx = 0;
 487  0
             int ix = compositionTemplate.indexOf(";");
 488  0
             String type = compositionTemplate.substring(0, ix).replace("=", "").replace(" ", "");
 489  0
             fieldTypes.add(type);
 490  0
         }
 491  
 
 492  0
         return fieldTypes;
 493  
     }
 494  
 
 495  
     private String getFieldValue(List<ReqCompFieldInfo> fields, String key) {
 496  
 
 497  0
         if (fields == null) {
 498  0
             return null;
 499  
         }
 500  
 
 501  0
         for (ReqCompFieldInfo fieldInfo : fields) {
 502  0
             if (fieldInfo.getType().equals(key)) {
 503  0
                 return (fieldInfo.getValue() == null ? "" : fieldInfo.getValue());
 504  
             }
 505  
         }
 506  
 
 507  0
         return "";
 508  
     }
 509  
 
 510  
     private void displayConfirmButton() {
 511  0
         actionCancelButtons.addStyleName("KS-Rule-ReqComp-btn");
 512  0
         actionCancelButtons.getButton(ButtonEnumerations.AddCancelEnum.ADD).setText("Add Rule");
 513  0
         add(actionCancelButtons);
 514  0
     }
 515  
 
 516  
     private void displayReqCompListPanel() {
 517  
 
 518  0
         reqCompTypePanel.setStyleName("KS-Rule-ReqCompList-box");
 519  
 
 520  0
         SectionTitle subHeader = SectionTitle.generateH5Title("Select rule type");
 521  0
         subHeader.setStyleName("KS-Rule-ReqComp-header");
 522  0
         reqCompTypePanel.add(subHeader);
 523  
 
 524  0
         KSLabel instructions = new KSLabel("Use the list below to select the type of rule you would like to add to this requirement");
 525  0
         reqCompTypePanel.add(instructions);
 526  
 
 527  0
         reqCompTypesList.addStyleName("KS-Rule-ReqCompList");
 528  0
         reqCompTypePanel.add(reqCompTypesList);
 529  0
         add(reqCompTypePanel);
 530  0
     }
 531  
 
 532  
     private void selectReqCompTypeInList() {
 533  0
         if (selectedReqCompType == null) {
 534  0
             if (reqCompTypesList.getSelectedItem() != null) {
 535  0
                 reqCompTypesList.deSelectItem(reqCompTypesList.getSelectedItem());
 536  
             }
 537  0
             return;
 538  
         }
 539  
 
 540  0
         int i = 0;
 541  0
         for (ReqComponentTypeInfo comp : reqCompTypeInfoList) {
 542  0
             if (comp.getId().equals(selectedReqCompType.getId())) {
 543  0
                 reqCompTypesList.selectItem(Integer.toString(i));
 544  0
                 break;
 545  
             }
 546  0
             i++;
 547  
         }
 548  0
     }
 549  
 
 550  
     private void setupReqCompTypesList() {
 551  0
             listItemReqCompTypes = new ListItems() {
 552  
                 @Override
 553  
                 public List<String> getAttrKeys() {
 554  0
                     List<String> attributes = new ArrayList<String>();
 555  0
                     attributes.add("Key");
 556  0
                     return attributes;
 557  
                 }
 558  
 
 559  
                 @Override
 560  
                 public String getItemAttribute(String id, String attrkey) {
 561  0
                     String value = null;
 562  
                     Integer index;
 563  
                     try{
 564  0
                         index = Integer.valueOf(id);
 565  0
                         value = reqCompTypeInfoList.get(index).getDescr();
 566  0
                     } catch (Exception e) {
 567  0
                     }
 568  
 
 569  0
                     return value;
 570  
                 }
 571  
 
 572  
                 @Override
 573  
                 public int getItemCount() {
 574  0
                     return reqCompTypeInfoList.size();
 575  
                 }
 576  
 
 577  
                 @Override
 578  
                 public List<String> getItemIds() {
 579  0
                     List<String> ids = new ArrayList<String>();
 580  0
                     for(int i = 0; i < reqCompTypeInfoList.size(); i++){
 581  0
                         ids.add(String.valueOf(i));
 582  
                     }
 583  0
                     return ids;
 584  
                 }
 585  
 
 586  
                 @Override
 587  
                 public String getItemText(String id) {
 588  0
                     return getItemAttribute(id, "?");
 589  
                 }
 590  
             };
 591  0
     }
 592  
 
 593  
     private void setEnableAddRuleButtons(boolean enable) {
 594  0
         actionCancelButtons.getButton(ButtonEnumerations.AddCancelEnum.ADD).setEnabled(enable);
 595  0
         actionCancelButtons.getButton(ButtonEnumerations.AddCancelEnum.CANCEL).setEnabled(enable);
 596  
 
 597  0
         if (!enable) {
 598  0
             reqCompFieldsPanel = null;
 599  
         }
 600  0
     }
 601  
 
 602  
     //called by view that manages this widget, passing list of req. component types
 603  
     public void setReqCompList(List<ReqComponentTypeInfo> reqComponentTypeInfoList) {
 604  
 
 605  0
         if (reqComponentTypeInfoList == null || reqComponentTypeInfoList.size() == 0) {
 606  0
             GWT.log("Missing Requirement Component Types", null);
 607  0
             Window.alert("Missing Requirement Component Types");
 608  0
             return;
 609  
         }
 610  
 
 611  
         //store all requirement components locally
 612  0
         reqCompTypeInfoList = reqComponentTypeInfoList;
 613  
 
 614  0
         reqCompTypesList.setListItems(listItemReqCompTypes);
 615  0
         if (reqCompTypesList.getSelectedItem() != null) {
 616  0
             reqCompTypesList.deSelectItem(reqCompTypesList.getSelectedItem());
 617  
         }
 618  
 
 619  0
         reqCompTypesList.setEnabled(true);
 620  0
         setEnableAddRuleButtons(false);
 621  0
     }
 622  
 
 623  
     public void setCustomWidgets(Map<String, Widget> customWidgets) {
 624  0
         customWidgetsNew = customWidgets;
 625  0
     }
 626  
 
 627  
     public void setReqCompConfirmButtonClickCallback(Callback<ReqComponentInfoUi> actionButtonClickedReqCompCallback) {
 628  0
         reqCompConfirmCallback = actionButtonClickedReqCompCallback;
 629  0
     }
 630  
 
 631  
     public void setRetrieveCompositionTemplateCallback(Callback<ReqComponentInfo> callback) {
 632  0
         compositionTemplateCallback = callback;
 633  0
     }
 634  
 
 635  
     public void setRetrieveFieldsMetadataCallback(Callback<List<String>> callback) {
 636  0
         fieldsMetadataTemplateCallback = callback;
 637  0
     }
 638  
 
 639  
     public void setNewReqCompSelectedCallbackCallback(Callback<ReqComponentInfo> callback) {
 640  0
         newReqCompSelectedCallback = callback;
 641  0
     }
 642  
 
 643  
     public void setRetrieveCustomWidgetCallback(Callback<String> callback) {
 644  0
         displayCustomWidgetCallback = callback;
 645  0
     }
 646  
 }
 647  
 
 648