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