Coverage Report - org.kuali.student.common.ui.client.widgets.table.summary.SummaryTableSection
 
Classes in this File Line Coverage Branch Coverage Complexity
SummaryTableSection
0%
0/251
0%
0/142
4.083
SummaryTableSection$1
0%
0/7
N/A
4.083
 
 1  
 package org.kuali.student.common.ui.client.widgets.table.summary;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.Iterator;
 5  
 import java.util.List;
 6  
 import java.util.Map;
 7  
 
 8  
 import org.kuali.student.common.assembly.data.Data;
 9  
 import org.kuali.student.common.assembly.data.MetadataInterrogator;
 10  
 import org.kuali.student.common.assembly.data.QueryPath;
 11  
 import org.kuali.student.common.assembly.data.Data.Property;
 12  
 import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor;
 13  
 import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptorReadOnly;
 14  
 import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle;
 15  
 import org.kuali.student.common.ui.client.configurable.mvc.binding.ModelWidgetBinding;
 16  
 import org.kuali.student.common.ui.client.configurable.mvc.multiplicity.MultiplicityConfiguration;
 17  
 import org.kuali.student.common.ui.client.configurable.mvc.multiplicity.MultiplicityFieldConfiguration;
 18  
 import org.kuali.student.common.ui.client.configurable.mvc.sections.Section;
 19  
 import org.kuali.student.common.ui.client.configurable.mvc.sections.VerticalSection;
 20  
 import org.kuali.student.common.ui.client.mvc.Controller;
 21  
 import org.kuali.student.common.ui.client.mvc.DataModel;
 22  
 import org.kuali.student.common.ui.client.mvc.ModelRequestCallback;
 23  
 import org.kuali.student.common.validation.dto.ValidationResultInfo;
 24  
 import org.kuali.student.common.validation.dto.ValidationResultInfo.ErrorLevel;
 25  
 
 26  
 import com.google.gwt.core.client.GWT;
 27  
 import com.google.gwt.user.client.ui.Widget;
 28  
 
 29  0
 public class SummaryTableSection extends VerticalSection {
 30  0
     SummaryTable summaryTable = new SummaryTable();
 31  0
     SummaryTableModel summaryTableModel = new SummaryTableModel();
 32  
     Controller controller;
 33  0
     DataModel comparisonModel = null;
 34  0
     List<ShowRowConditionCallback> showRowCallbacks = new ArrayList<ShowRowConditionCallback>();
 35  
 
 36  
     public SummaryTableSection(Controller controller) {
 37  0
         super();
 38  0
         this.controller = controller;
 39  0
         this.addWidget(summaryTable);
 40  0
         summaryTable.setModel(summaryTableModel);
 41  0
     }
 42  
 
 43  
     public SummaryTableSection(Controller controller, SectionTitle title) {
 44  0
         super(title);
 45  0
         this.controller = controller;
 46  0
         this.addWidget(summaryTable);
 47  0
         summaryTable.setModel(summaryTableModel);
 48  0
     }
 49  
 
 50  
     public void setContentColumnHeader1(String title) {
 51  0
         summaryTableModel.setContentColumnHeader1(title);
 52  0
     }
 53  
 
 54  
     public void setContentColumnHeader2(String title) {
 55  0
         summaryTableModel.setContentColumnHeader2(title);
 56  0
     }
 57  
 
 58  
     public String getContentColumnHeader1() {
 59  0
         return summaryTableModel.getContentColumnHeader1();
 60  
     }
 61  
 
 62  
     public String getContentColumnHeader2() {
 63  0
         return summaryTableModel.getContentColumnHeader2();
 64  
     }
 65  
 
 66  
     public void setEditable(boolean bool) {
 67  0
         summaryTableModel.setEditable(bool);
 68  0
         summaryTable.doLayout();
 69  0
         summaryTable.markDiffs("rowDiffHighlight");
 70  0
     }
 71  
     
 72  
     public void addShowRowCallback(ShowRowConditionCallback callback){
 73  0
             this.showRowCallbacks.add(callback);
 74  0
     }
 75  
     
 76  
     private void processShowConditions(SummaryTableFieldRow row, DataModel model, DataModel comparisonModel){
 77  0
             for(int i =0; i < showRowCallbacks.size(); i++){
 78  0
                     showRowCallbacks.get(i).processShowConditions(row, model, comparisonModel);
 79  
             }
 80  0
     }
 81  
 
 82  
     public void addSummaryTableFieldBlock(SummaryTableFieldBlock section) {
 83  0
         summaryTableModel.addSection(section);
 84  0
     }
 85  
     
 86  
     @Override
 87  
     public ErrorLevel processValidationResults(
 88  
                     List<ValidationResultInfo> results) {
 89  0
             ErrorLevel status = ErrorLevel.OK;
 90  0
             for(int i = 0; i < results.size(); i++){
 91  0
                     if(summaryTable.containsKey(results.get(i).getElement())){
 92  0
                             System.out.println(results.get(i).getElement() + " *** " + results.get(i).getErrorLevel() + " *** " + results.get(i).getMessage());
 93  0
                             if(results.get(i).getLevel().getLevel() > status.getLevel()){
 94  0
                                     status = results.get(i).getLevel();
 95  
                             }
 96  0
                             if(this.isValidationEnabled){
 97  0
                                 summaryTable.highlightRow(results.get(i).getElement(), "rowHighlight");
 98  
                         }
 99  
                     }
 100  
             }
 101  0
             return status;
 102  
     }
 103  
     
 104  
     @Override
 105  
     public ErrorLevel processValidationResults(
 106  
                     List<ValidationResultInfo> results, boolean clearAllValidation) {
 107  0
             if(clearAllValidation){
 108  0
                     this.removeValidationHighlighting();
 109  
             }
 110  0
             return this.processValidationResults(results);
 111  
     }
 112  
     
 113  
     public void removeValidationHighlighting(){
 114  0
             summaryTable.clearHighlightedRows("rowHighlight");
 115  0
     }
 116  
     
 117  
     private int buildMultiplicityRows(DataModel model, DataModel compModel, SummaryTableMultiplicityFieldRow parentRow, 
 118  
                     List<SummaryTableRow> rowList, int styleLevel, Integer parentNum){
 119  0
             MultiplicityConfiguration config = parentRow.getConfig();
 120  0
             int index = rowList.indexOf(parentRow) + 1;
 121  0
             int fieldRowsCreated = 0;
 122  0
             int number = 0;
 123  0
             String path = parentRow.getConfig().getParentFd().getFieldKey();
 124  0
             if(parentNum != null){
 125  0
                     path = path.replace("*", "" + parentNum);
 126  
             }
 127  0
             Data data = null;
 128  0
             if(model != null && model.isValidPath(path)){
 129  0
                     data = model.get(QueryPath.parse(path));
 130  
             }
 131  0
                 Data compData = null;
 132  0
                 if(compModel != null && compModel.isValidPath(path)){
 133  0
                         compData = compModel.get(QueryPath.parse(path));
 134  
                 }
 135  0
             if((data != null && data.size() > 0) || (compData != null && compData.size() > 0)){
 136  0
                     Iterator<Property> itr = null;
 137  0
                     if(data != null && compData != null){
 138  0
                             if(data.size() >= compData.size()){
 139  0
                                     itr = data.iterator();
 140  
                             }
 141  
                             else{
 142  0
                                     itr = compData.iterator();
 143  
                             }
 144  
                     }
 145  0
                     else if(data != null){
 146  0
                             itr = data.iterator();
 147  
                     }
 148  
                     else{
 149  0
                             itr = compData.iterator();
 150  
                     }
 151  0
                     SummaryTableMultiplicityFieldRow currentMultiplicityRow = parentRow;
 152  0
             while (itr.hasNext()) {
 153  0
                 Property p = (Property) itr.next();
 154  0
                 if (p.getKey() instanceof Integer){
 155  0
                         number = (Integer)p.getKey();
 156  
                 }
 157  0
                         if(config.getItemLabel() != null && !config.getItemLabel().isEmpty()){
 158  0
                                 currentMultiplicityRow.setTitle(config.getItemLabel() + " "+ (number + 1));
 159  0
                                 currentMultiplicityRow.setKey(path);
 160  0
                             if(MetadataInterrogator.isRequired(config.getMetaData()) || 
 161  
                                         MetadataInterrogator.isRequiredForNextState(config.getMetaData())){
 162  0
                                     currentMultiplicityRow.setRequired(true);
 163  
                             }
 164  0
                             currentMultiplicityRow.addTitleCellStyleName("summary-table-multiplicity-level-" + styleLevel);
 165  
                         }
 166  
                         else{
 167  0
                                 currentMultiplicityRow.setShown(false);
 168  
                         }
 169  
                             //set has-data/requires data style here
 170  0
                             Map<Integer, List<MultiplicityFieldConfiguration>> fieldsCopy = config.getFields();
 171  0
                                 for(int i = 0; i < config.getFields().size(); i++){
 172  0
                                         for(int j = 0; j < config.getFields().get(i).size(); j++){
 173  
                                                 //TODO handle custom widgets (multiplicity field widget initializer)
 174  0
                                                 MultiplicityFieldConfiguration field = fieldsCopy.get(i).get(j);
 175  0
                                             String fieldKey = translatePath(field.getFieldPath(), path, number);
 176  0
                                             FieldDescriptorReadOnly fd1 = new FieldDescriptorReadOnly(fieldKey, field.getMessageKeyInfo(), field.getMetadata());
 177  0
                                             fd1.setOptional(field.isOptional());
 178  0
                                             if(field.getModelWidgetBinding() != null){
 179  0
                                                     fd1.setWidgetBinding(field.getModelWidgetBinding());
 180  
                                             }
 181  0
                                             FieldDescriptorReadOnly fd2 = new FieldDescriptorReadOnly(fieldKey, field.getMessageKeyInfo(), field.getMetadata());
 182  0
                                             fd2.setOptional(field.isOptional());
 183  0
                                             if(field.getModelWidgetBinding() != null){
 184  0
                                                     fd2.setWidgetBinding(field.getModelWidgetBinding());
 185  
                                             }
 186  0
                                             SummaryTableFieldRow row = new SummaryTableFieldRow(fd1, fd2);
 187  0
                                             row.setTemporaryRowFlag(true);
 188  0
                                             rowList.add(index, row);
 189  0
                                             index++;
 190  0
                                             fieldRowsCreated++;
 191  
                                     }
 192  
                                 }
 193  
                                 
 194  0
                             if(config.getNestedConfig() != null){
 195  0
                                     MultiplicityConfiguration nestedConfig = config.getNestedConfig();
 196  0
                                     nestedConfig.getParentFd().getFieldKey().replace(config.getParentFd().getFieldKey(), path);
 197  0
                                     SummaryTableMultiplicityFieldRow mRow = new SummaryTableMultiplicityFieldRow(nestedConfig);
 198  0
                                     mRow.setTemporaryRowFlag(true);
 199  0
                                     rowList.add(index, mRow);
 200  0
                                     index++;
 201  0
                                     fieldRowsCreated++;
 202  0
                                     int result = buildMultiplicityRows(model, compModel, mRow, rowList, styleLevel + 1, number);
 203  0
                                     index = index + result;
 204  
                                     
 205  
                             }
 206  
                             
 207  0
                             if(itr.hasNext()){
 208  0
                                     SummaryTableMultiplicityFieldRow mRow = new SummaryTableMultiplicityFieldRow(config);
 209  0
                                     mRow.setTemporaryRowFlag(true);
 210  0
                                     rowList.add(index, mRow);
 211  0
                                     index++;
 212  0
                                     fieldRowsCreated++;
 213  0
                                     currentMultiplicityRow = mRow;
 214  
                             }
 215  0
             }
 216  0
             }
 217  
             else{
 218  0
                 if(MetadataInterrogator.isRequired(config.getMetaData()) || 
 219  
                                 MetadataInterrogator.isRequiredForNextState(config.getMetaData())){
 220  0
                         if(config.getItemLabel() != null && !config.getItemLabel().isEmpty()){
 221  0
                                 parentRow.setTitle(config.getItemLabel() + " "+ (number + 1));
 222  0
                                 parentRow.setKey(path);
 223  0
                             parentRow.setRequired(true);
 224  0
                             parentRow.addTitleCellStyleName("summary-table-multiplicity-level-" + styleLevel);
 225  
                         }
 226  
                         else{
 227  0
                                 parentRow.setShown(false);
 228  
                         }
 229  
                         //set has-data/requires data style here
 230  0
                         Map<Integer, List<MultiplicityFieldConfiguration>> fields = config.getFields();
 231  0
                                 for(int i = 0; i < fields.size(); i++){
 232  0
                                         for(int j = 0; j < fields.get(i).size(); j++){
 233  
                                                 //TODO handle custom widgets (multiplicity field widget initializer)
 234  0
                                                 MultiplicityFieldConfiguration field = fields.get(i).get(j);
 235  0
                                             String fieldKey = translatePath(field.getFieldPath(), path, number);
 236  0
                                             FieldDescriptorReadOnly fd1 = new FieldDescriptorReadOnly(fieldKey, field.getMessageKeyInfo(), field.getMetadata());
 237  0
                                             fd1.setOptional(field.isOptional());
 238  0
                                             if(field.getModelWidgetBinding() != null){
 239  0
                                                     fd1.setWidgetBinding(field.getModelWidgetBinding());
 240  
                                             }
 241  0
                                             FieldDescriptorReadOnly fd2 = new FieldDescriptorReadOnly(fieldKey, field.getMessageKeyInfo(), field.getMetadata());
 242  0
                                             fd2.setOptional(field.isOptional());
 243  0
                                             if(field.getModelWidgetBinding() != null){
 244  0
                                                     fd2.setWidgetBinding(field.getModelWidgetBinding());
 245  
                                             }
 246  0
                                             SummaryTableFieldRow row = new SummaryTableFieldRow(fd1, fd2);
 247  0
                                             row.setTemporaryRowFlag(true);
 248  0
                                             rowList.add(index, row);
 249  0
                                             index++;
 250  0
                                             fieldRowsCreated++;
 251  
                                     }
 252  
                                 }
 253  
                                 
 254  
                             
 255  0
                             if(config.getNestedConfig() != null){
 256  0
                                     MultiplicityConfiguration nestedConfig = config.getNestedConfig();
 257  0
                                     nestedConfig.getParentFd().getFieldKey().replace(config.getParentFd().getFieldKey(), path);
 258  0
                                     SummaryTableMultiplicityFieldRow mRow = new SummaryTableMultiplicityFieldRow(nestedConfig);
 259  0
                                     mRow.setTemporaryRowFlag(true);
 260  0
                                     rowList.add(index, mRow);
 261  0
                                     index++;
 262  0
                                     fieldRowsCreated++;
 263  0
                                     buildMultiplicityRows(null, null, mRow, rowList, styleLevel + 1, number);
 264  
                             }
 265  0
                 }
 266  
                 else{
 267  
                         //Alternate label possibly here
 268  0
                         parentRow.setTitle(config.getItemLabel());
 269  0
                         parentRow.setRequired(false);
 270  0
                         parentRow.setKey(config.getParentFd().getFieldKey());
 271  
                         //set unrequired style here
 272  
                 }
 273  
             }
 274  0
             return fieldRowsCreated;
 275  
     }
 276  
     
 277  
     public String translatePath(String path, String parentPath, int num) {
 278  
         String fieldPath;
 279  0
         if (parentPath != null) {
 280  0
             QueryPath parent = QueryPath.concat(parentPath);
 281  0
             int i = parent.size();
 282  
 
 283  0
             QueryPath subPath = QueryPath.concat(path);
 284  0
             String itemPath =  subPath.subPath(i, subPath.size()).toString();
 285  
 
 286  0
             QueryPath qp = QueryPath.concat(parentPath, itemPath);
 287  0
             fieldPath = qp.toString();
 288  0
         }
 289  
         else {
 290  0
             fieldPath = path;
 291  
         }
 292  
 
 293  0
         fieldPath = fieldPath.replace("*", "" + num);
 294  0
         return fieldPath;
 295  
     }
 296  
     
 297  
     @Override
 298  
     public void updateWidgetData(final DataModel model) {
 299  
             
 300  0
         controller.requestModel("ComparisonModel", new ModelRequestCallback<DataModel>() {
 301  
             @Override
 302  
             public void onModelReady(DataModel otherModel) {
 303  0
                 comparisonModel = otherModel;
 304  0
                 updateTableData(model);
 305  0
             }
 306  
 
 307  
             @Override
 308  
             public void onRequestFail(Throwable cause) {
 309  0
                     comparisonModel = null;
 310  0
                     updateTableData(model);
 311  
                 //GWT.log("ComparisonModel cannot be found. " + cause.getLocalizedMessage(), null);
 312  0
             }
 313  
         });
 314  0
     }
 315  
     
 316  
     private void resetSummaryTableRows(SummaryTableFieldBlock fieldBlock){
 317  0
             List<SummaryTableRow> rowList = fieldBlock.getSectionRowList();
 318  0
             List<SummaryTableRow> removeList = new ArrayList<SummaryTableRow>();
 319  0
         for (int j = 0; j < rowList.size(); j++) {
 320  0
                  SummaryTableFieldRow fieldRow = (SummaryTableFieldRow) rowList.get(j);
 321  0
                  if(fieldRow.isTemporaryRow()){
 322  0
                          removeList.add(fieldRow);
 323  
                  }
 324  0
                  if(!fieldRow.isShown()){
 325  0
                          fieldRow.setShown(true);
 326  
                  }
 327  
         }
 328  0
         rowList.removeAll(removeList);
 329  0
     }
 330  
     
 331  
     private void buildSummaryTableMultiplicity(DataModel model, DataModel compModel, SummaryTableFieldBlock fieldBlock){
 332  0
             List<SummaryTableMultiplicityFieldRow> mRows = fieldBlock.getMultiplicityList();
 333  0
             for(int i = 0; i < mRows.size(); i++){
 334  0
                     SummaryTableMultiplicityFieldRow mRow = mRows.get(i);
 335  
                     
 336  0
                     buildMultiplicityRows(model, compModel, mRow, fieldBlock.getSectionRowList(), 1, null);
 337  
             }
 338  0
     }
 339  
     
 340  
     @SuppressWarnings("unchecked")
 341  
         private void updateTableData(DataModel model){
 342  0
             List<SummaryTableBlock> sectionList = summaryTableModel.getSectionList();
 343  0
         for (int i = 0; i < sectionList.size(); i++) {
 344  0
             SummaryTableFieldBlock fieldBlock = (SummaryTableFieldBlock) sectionList.get(i);
 345  0
             resetSummaryTableRows(fieldBlock);
 346  0
             if(!fieldBlock.getMultiplicityList().isEmpty()){
 347  0
                     buildSummaryTableMultiplicity(model, comparisonModel, fieldBlock);
 348  
             }
 349  0
             List<SummaryTableRow> rowList = fieldBlock.getSectionRowList();
 350  
             
 351  0
             for (int j = 0; j < rowList.size(); j++) {
 352  0
                 SummaryTableFieldRow fieldRow = (SummaryTableFieldRow) rowList.get(j);
 353  0
                 FieldDescriptor field = fieldRow.getFieldDescriptor1();
 354  0
                 final FieldDescriptor field2 = fieldRow.getFieldDescriptor2();
 355  0
                 boolean optional = false;
 356  0
                 boolean firstValueEmpty = true;
 357  0
                 boolean secondValueEmpty = true;
 358  
                 // for the first column
 359  0
                 if(field != null){
 360  
                         
 361  0
                         if(field instanceof FieldDescriptorReadOnly){
 362  0
                                 optional = ((FieldDescriptorReadOnly)field).isOptional();
 363  
                         }
 364  0
                         String fieldPath = QueryPath.getPathSeparator() + field.getFieldKey();
 365  0
                         if(model.isValidPath(fieldPath)){
 366  
                         
 367  0
                                 Object value = model.get(QueryPath.parse(fieldPath));
 368  0
                                 if(value != null){
 369  0
                                     if(value instanceof String && ((String)value).isEmpty()){
 370  0
                                                 firstValueEmpty = true;
 371  
                                         }
 372  0
                                     else if(value instanceof Data && ((Data) value).size() == 0){
 373  0
                                             firstValueEmpty = true;
 374  
                                     }
 375  
                                     else{
 376  0
                                             firstValueEmpty = false;
 377  
                                     }
 378  
                                 }
 379  
                                 
 380  
                                 
 381  0
                                 ModelWidgetBinding binding = field.getModelWidgetBinding();
 382  
                         
 383  0
                                 if (binding != null) {
 384  0
                                     Widget w = field.getFieldWidget();
 385  0
                                     binding.setWidgetValue(w, model, fieldPath);
 386  0
                                 } else {
 387  0
                                     GWT.log(field.getFieldKey() + " has no widget binding.", null);
 388  
                                 }
 389  
                         }
 390  
                         
 391  
                 }
 392  
 
 393  
                 // the second column
 394  0
                 if (comparisonModel == null) {
 395  0
                         if(fieldRow.getContentCellCount() == 2){
 396  0
                                 fieldRow.setContentCellCount(1);
 397  
                         }
 398  
                 }else{
 399  0
                         summaryTableModel.setContentColumnHeader1(model.getModelName());
 400  0
                         summaryTableModel.setContentColumnHeader2(comparisonModel.getModelName());
 401  0
                         if(fieldRow.getContentCellCount() == 1){
 402  0
                                 fieldRow.setContentCellCount(2);
 403  
                         }
 404  0
                         if(field2 != null){
 405  
                             
 406  0
                             String fieldPath2 = QueryPath.getPathSeparator() + field2.getFieldKey();
 407  0
                             if(comparisonModel.isValidPath(fieldPath2)){
 408  
                                     
 409  0
                                     Object value = model.get(QueryPath.parse(fieldPath2));
 410  0
                                     if(value != null){
 411  0
                                             if(value instanceof String && ((String)value).isEmpty()){
 412  0
                                                         secondValueEmpty = true;
 413  
                                                 }
 414  0
                                             else if(value instanceof Data && ((Data) value).size() == 0){
 415  0
                                                     secondValueEmpty = true;
 416  
                                             }
 417  
                                             else{
 418  0
                                                     secondValueEmpty = false;
 419  
                                             }
 420  
                                         }
 421  
                                     
 422  0
                                     ModelWidgetBinding binding2 = field2.getModelWidgetBinding();
 423  
                                     
 424  0
                                     if (binding2 != null) {
 425  0
                                         Widget w = field2.getFieldWidget();
 426  0
                                         binding2.setWidgetValue(w, comparisonModel, fieldPath2);
 427  0
                                     } else {
 428  0
                                         GWT.log(field2.getFieldKey() + " has no widget binding for the ComparisonModel.", null);
 429  
                                     }
 430  
                             }
 431  
                         }
 432  
                 }
 433  
                 
 434  0
                 if(firstValueEmpty && secondValueEmpty && optional){
 435  0
                         fieldRow.setShown(false);
 436  
                 }
 437  0
                 processShowConditions(fieldRow, model, comparisonModel);
 438  
             }
 439  
             
 440  
         }
 441  
 
 442  0
         summaryTable.doLayout();
 443  0
         summaryTable.markDiffs("rowDiffHighlight");
 444  0
     }
 445  
 
 446  
     @Override
 447  
     public String addField(FieldDescriptor fieldDescriptor) {
 448  0
         GWT.log("addField(FieldDescriptor fieldDescriptor) method not supported");
 449  0
         throw new UnsupportedOperationException("SummaryTableSection.addField(FieldDescriptor fieldDescriptor) method not supported");
 450  
     }
 451  
 
 452  
     @Override
 453  
     public String addSection(Section section) {
 454  0
         GWT.log("addSection(Section section) method not supported");
 455  0
         throw new UnsupportedOperationException("SummaryTableSection.addSection(Section section) method not supported");
 456  
     }
 457  
 
 458  
     @Override
 459  
     public String addSection(String key, Section section) {
 460  0
         GWT.log("addSection(String key, Section section) method not supported");
 461  0
         throw new UnsupportedOperationException("SummaryTableSection.addSection(String key, Section section) method not supported");
 462  
     }
 463  
 
 464  
 }