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