Coverage Report - org.kuali.student.common.ui.client.configurable.mvc.sections.BaseSection
 
Classes in this File Line Coverage Branch Coverage Complexity
BaseSection
0%
0/207
0%
0/114
2.465
BaseSection$1
0%
0/16
0%
0/8
2.465
BaseSection$1$1
0%
0/5
N/A
2.465
BaseSection$1$2
0%
0/5
N/A
2.465
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.common.ui.client.configurable.mvc.sections;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import com.google.gwt.user.client.Window;
 22  
 import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor;
 23  
 import org.kuali.student.common.ui.client.configurable.mvc.LayoutController;
 24  
 import org.kuali.student.common.ui.client.configurable.mvc.ValidationEventBinding;
 25  
 import org.kuali.student.common.ui.client.configurable.mvc.ValidationEventBindingImpl;
 26  
 import org.kuali.student.common.ui.client.configurable.mvc.binding.ModelWidgetBinding;
 27  
 import org.kuali.student.common.ui.client.configurable.mvc.binding.ModelWidgetBindingSupport;
 28  
 import org.kuali.student.common.ui.client.configurable.mvc.multiplicity.MultiplicityComposite;
 29  
 import org.kuali.student.common.ui.client.configurable.mvc.multiplicity.MultiplicityGroup;
 30  
 import org.kuali.student.common.ui.client.configurable.mvc.multiplicity.MultiplicityGroupItem;
 31  
 import org.kuali.student.common.ui.client.configurable.mvc.multiplicity.MultiplicityItem;
 32  
 import org.kuali.student.common.ui.client.event.ContentDirtyEvent;
 33  
 import org.kuali.student.common.ui.client.event.ValidateRequestEvent;
 34  
 import org.kuali.student.common.ui.client.mvc.Callback;
 35  
 import org.kuali.student.common.ui.client.mvc.Controller;
 36  
 import org.kuali.student.common.ui.client.mvc.DataModel;
 37  
 import org.kuali.student.common.ui.client.mvc.ModelRequestCallback;
 38  
 import org.kuali.student.common.ui.client.mvc.View;
 39  
 import org.kuali.student.common.ui.client.widgets.field.layout.element.FieldElement;
 40  
 import org.kuali.student.common.ui.client.widgets.field.layout.element.SpanPanel;
 41  
 import org.kuali.student.common.ui.client.widgets.field.layout.layouts.FieldLayout;
 42  
 import org.kuali.student.core.assembly.data.Data;
 43  
 import org.kuali.student.core.assembly.data.QueryPath;
 44  
 import org.kuali.student.core.assembly.data.Data.Key;
 45  
 import org.kuali.student.core.validation.dto.ValidationResultInfo;
 46  
 import org.kuali.student.core.validation.dto.ValidationResultInfo.ErrorLevel;
 47  
 
 48  
 import com.google.gwt.core.client.GWT;
 49  
 import com.google.gwt.user.client.ui.Widget;
 50  
 
 51  0
 public abstract class BaseSection extends SpanPanel implements Section{
 52  
 
 53  
         protected FieldLayout layout;
 54  
 
 55  0
         protected ArrayList<Section> sections = new ArrayList<Section>();
 56  0
         protected ArrayList<FieldDescriptor> fields = new ArrayList<FieldDescriptor>();
 57  0
         protected LayoutController layoutController = null;
 58  0
         protected boolean isValidationEnabled = true;
 59  0
         protected boolean isDirty = false;
 60  
 
 61  
         /**
 62  
          * Note if a field has been marked as hidden in the dictionary, this method will not add this field to the layout.
 63  
          * If it is not visible the key returned by making call to addField is null.
 64  
          */
 65  
         @Override
 66  
         public String addField(final FieldDescriptor fieldDescriptor) {
 67  0
                 String key = null;
 68  
                 
 69  0
                 if (fieldDescriptor.isVisible()){
 70  0
                         fields.add(fieldDescriptor);
 71  0
                 key = layout.addField(fieldDescriptor.getFieldElement());
 72  
         
 73  0
                 ValidationEventBinding binding = new ValidationEventBindingImpl();
 74  0
                 if(fieldDescriptor.getValidationRequestCallback()== null){
 75  0
                     fieldDescriptor.setValidationCallBack(new Callback<Boolean>() {
 76  
                         @Override
 77  
                         public void exec(Boolean result) {
 78  0
                                 final ModelWidgetBinding mwb = fieldDescriptor.getModelWidgetBinding();
 79  0
                                 if (mwb != null) {
 80  0
                                     final Widget w = fieldDescriptor.getFieldWidget();
 81  0
                                     final String modelId = fieldDescriptor.getModelId();
 82  
                                     final Controller parent;
 83  0
                                 Controller findResult = LayoutController.findParentLayout(w);
 84  0
                                 if(BaseSection.this instanceof View){
 85  0
                                         findResult = ((View)BaseSection.this).getController();
 86  
                                 }
 87  0
                                 parent = findResult;
 88  0
                                 if(parent != null){
 89  0
                                         if (modelId == null) {
 90  0
                                                 parent.requestModel(new ModelRequestCallback<DataModel>() {
 91  
         
 92  
                                                         @Override
 93  
                                                         public void onModelReady(DataModel model) {
 94  0
                                                                 validateField(fieldDescriptor, model, parent);
 95  
         
 96  0
                                                         }
 97  
         
 98  
                                                         @Override
 99  
                                                         public void onRequestFail(Throwable cause) {
 100  0
                                                                 GWT.log("Unable to retrieve model to validate " + fieldDescriptor.getFieldKey(), null);
 101  0
                                                         }
 102  
         
 103  
                                                 });
 104  
                                         } else {
 105  0
                                                 parent.requestModel(modelId, new ModelRequestCallback<DataModel>() {
 106  
         
 107  
                                                         @Override
 108  
                                                         public void onModelReady(DataModel model) {
 109  0
                                                                 validateField(fieldDescriptor, model, parent);
 110  0
                                                         }
 111  
         
 112  
                                                         @Override
 113  
                                                         public void onRequestFail(Throwable cause) {
 114  0
                                                                 GWT.log("Unable to retrieve model to validate " + fieldDescriptor.getFieldKey(), null);
 115  0
                                                         }
 116  
         
 117  
                                                 });                                }
 118  
                                         }
 119  0
                             } else {
 120  0
                                 GWT.log(fieldDescriptor.getFieldKey() + " has no widget binding.", null);
 121  
                             }
 122  0
                         }
 123  
                     });
 124  
                 }
 125  0
                 binding.bind(fieldDescriptor);
 126  
                 }               
 127  
                 
 128  0
         return key;
 129  
         }
 130  
 
 131  
         private void validateField(
 132  
                         final FieldDescriptor fieldDescriptor, final DataModel model, Controller controller) {
 133  0
                 Widget w = fieldDescriptor.getFieldWidget();
 134  0
                 ModelWidgetBinding mwb = fieldDescriptor.getModelWidgetBinding();
 135  0
                 if(fieldDescriptor.getFieldKey() != null){
 136  0
                         mwb.setModelValue(w, model, fieldDescriptor.getFieldKey());
 137  0
                         dirtyCheckField(fieldDescriptor, model);
 138  0
                         ValidateRequestEvent e = new ValidateRequestEvent();
 139  0
                         e.setFieldDescriptor(fieldDescriptor);
 140  0
                         e.setValidateSingleField(true);
 141  0
                         controller.fireApplicationEvent(e);
 142  
                 }
 143  0
         }
 144  
 
 145  
         private void dirtyCheckField(FieldDescriptor fieldDescriptor, DataModel model){
 146  0
         QueryPath fieldPath = QueryPath.parse(fieldDescriptor.getFieldKey());
 147  0
                 QueryPath qPathDirty = fieldPath.subPath(0, fieldPath.size() - 1);
 148  0
             qPathDirty.add(ModelWidgetBindingSupport.RUNTIME_ROOT);
 149  0
             qPathDirty.add(ModelWidgetBindingSupport.DIRTY_PATH);
 150  0
             qPathDirty.add(fieldPath.get(fieldPath.size()-1));
 151  0
             Boolean dirty = false;
 152  
 
 153  0
             if(ensureDirtyFlagPath(model.getRoot(), qPathDirty)){
 154  0
                     dirty = model.get(qPathDirty);
 155  
             }
 156  0
             if(dirty){
 157  0
                     setIsDirty(true);
 158  0
                     fieldDescriptor.setDirty(true);
 159  
             }
 160  0
         }
 161  
 
 162  
     protected boolean ensureDirtyFlagPath(Data root, QueryPath path) {
 163  0
         Data current = root;
 164  0
         boolean containsFlag = false;
 165  0
         for (int i = 0; i < path.size(); i++) {
 166  0
             Key key = path.get(i);
 167  0
             if(i == path.size() - 1){
 168  0
                     containsFlag = current.containsKey(key);
 169  0
                     break;
 170  
             }
 171  0
             Data d = current.get(key);
 172  0
             if (d == null) {
 173  0
                 containsFlag = false;
 174  0
                 break;
 175  
             }
 176  0
             current = d;
 177  
         }
 178  0
         return containsFlag;
 179  
     }
 180  
 
 181  
         @Override
 182  
         public String addSection(Section section) {
 183  
 
 184  0
         section.setLayoutController(layoutController);
 185  0
                 sections.add(section);
 186  0
         String key = layout.addLayout(section.getLayout());
 187  0
         return key;
 188  
         }
 189  
 
 190  
         @Override
 191  
         public void removeSection(Section section){
 192  0
                 sections.remove(section);
 193  0
                 layout.removeLayoutElement(section.getLayout());
 194  0
         }
 195  
 
 196  
         protected void clearValidation() {
 197  0
                 layout.clearValidation();
 198  
 
 199  0
         }
 200  
 
 201  
         @Override
 202  
         public List<FieldDescriptor> getFields() {
 203  0
         List<FieldDescriptor> allFields = new ArrayList<FieldDescriptor>();
 204  0
         allFields.addAll(fields);
 205  
 
 206  0
         for(Section ns: sections){
 207  0
             allFields.addAll(ns.getFields());
 208  
         }
 209  0
         return allFields;
 210  
         }
 211  
 
 212  
         @Override
 213  
         public List<FieldDescriptor> getUnnestedFields() {
 214  0
         return fields;
 215  
         }
 216  
 
 217  
         @Override
 218  
         public List<Section> getSections() {
 219  0
                 return sections;
 220  
         }
 221  
 
 222  
         @Override
 223  
         public ErrorLevel processValidationResults(List<ValidationResultInfo> results, boolean clearAllValidation){
 224  0
                 if(clearAllValidation){
 225  0
                         this.clearValidation();
 226  
                 }
 227  0
                 ErrorLevel status = ErrorLevel.OK;
 228  
 
 229  0
                 if (isValidationEnabled){
 230  
 
 231  0
                         for(FieldDescriptor f: this.fields){
 232  
 
 233  0
                                 if(f.hasHadFocus()){
 234  0
                                         for(ValidationResultInfo vr: results){
 235  0
                         String vrElement = vr.getElement();
 236  0
                         if(vrElement.startsWith("/")){
 237  0
                             vrElement = vrElement.substring(1);
 238  
                         }
 239  0
                                                 if(vrElement.equals(f.getFieldKey())){
 240  0
                                                         FieldElement element = f.getFieldElement();
 241  0
                                                         if (element != null){
 242  0
                                                                 ErrorLevel fieldStatus = element.processValidationResult(vr);
 243  0
                                                                 if(fieldStatus.getLevel() > status.getLevel()){
 244  0
                                                                         status = fieldStatus;
 245  
                                                                 }
 246  
                                                         }
 247  
                                                 }
 248  0
                                         }
 249  
                                 }
 250  
 
 251  0
                                 if(f.getFieldWidget() instanceof MultiplicityComposite ){
 252  0
                                         MultiplicityComposite mc = (MultiplicityComposite) f.getFieldWidget();
 253  
 
 254  
                                         //possibly return error state from processValidationResults to give composite title bar a separate color
 255  0
                             for(MultiplicityItem item: mc.getItems()){
 256  0
                                     if(item.getItemWidget() instanceof Section && !item.isDeleted()){
 257  0
                                             ErrorLevel fieldStatus = ((Section)item.getItemWidget()).processValidationResults(results, clearAllValidation);
 258  0
                                                         if(fieldStatus.getLevel() > status.getLevel()){
 259  0
                                                                 status = fieldStatus;
 260  
                                                         }
 261  0
                                     }
 262  
                             }
 263  
                                 }
 264  
                                 //TODO Can we do this without checking for instanceof  MG??
 265  0
                                 if(f.getFieldWidget() instanceof MultiplicityGroup ){
 266  0
                                         MultiplicityGroup mg = (MultiplicityGroup) f.getFieldWidget();
 267  
 
 268  
                                         //possibly return error state from processValidationResults to give composite title bar a separate color
 269  0
                             for(MultiplicityGroupItem item: mg.getItems()){
 270  0
                                     if(item.getItemWidget() instanceof Section && !item.isDeleted()){
 271  0
                                             ErrorLevel fieldStatus = ((Section)item.getItemWidget()).processValidationResults(results, clearAllValidation);
 272  0
                                                         if(fieldStatus.getLevel() > status.getLevel()){
 273  0
                                                                 status = fieldStatus;
 274  
                                                         }
 275  0
                                     }
 276  
                             }
 277  0
                                 }
 278  
 
 279  
                         }
 280  
 
 281  0
                 for(Section s: sections){
 282  0
                     ErrorLevel subsectionStatus = s.processValidationResults(results,clearAllValidation);
 283  0
                     if(subsectionStatus.getLevel() > status.getLevel()){
 284  0
                             status = subsectionStatus;
 285  
                     }
 286  0
                 }
 287  
                 }
 288  
 
 289  0
         return status;
 290  
         }
 291  
 
 292  
         @Override
 293  
         public ErrorLevel processValidationResults(List<ValidationResultInfo> results) {
 294  0
                 return processValidationResults(results, true);
 295  
         }
 296  
 
 297  
         @Override
 298  
         public LayoutController getLayoutController() {
 299  0
                 return this.layoutController;
 300  
         }
 301  
 
 302  
         @Override
 303  
         public void setLayoutController(LayoutController controller) {
 304  0
                 this.layoutController = controller;
 305  0
         }
 306  
 
 307  
 
 308  
         @Override
 309  
         public String addWidget(Widget w) {
 310  0
                 return layout.addWidget(w);
 311  
         }
 312  
 
 313  
     public void setFieldHasHadFocusFlags(boolean hadFocus) {
 314  0
         for(FieldDescriptor f: fields){
 315  0
             f.setHasHadFocus(hadFocus);
 316  0
             if(f.getFieldWidget() instanceof MultiplicityComposite){
 317  0
                                 MultiplicityComposite mc = (MultiplicityComposite) f.getFieldWidget();
 318  
 
 319  0
                     for(MultiplicityItem item: mc.getItems()){
 320  0
                             if(item.getItemWidget() instanceof Section && !item.isDeleted()){
 321  0
                                     ((Section) item.getItemWidget()).setFieldHasHadFocusFlags(hadFocus);
 322  
                             }
 323  
                     }
 324  0
             }
 325  
         }
 326  
 
 327  0
         for(Section s: sections){
 328  0
             s.setFieldHasHadFocusFlags(hadFocus);
 329  
         }
 330  
 
 331  0
     }
 332  
 
 333  
     @Override
 334  
     public void enableValidation(boolean enableValidation) {
 335  0
             this.isValidationEnabled = enableValidation;
 336  0
     }
 337  
 
 338  
     @Override
 339  
     public boolean isValidationEnabled() {
 340  0
                 return isValidationEnabled;
 341  
         }
 342  
 
 343  
         @Override
 344  
     public void updateModel(DataModel model){
 345  0
         SectionBinding.INSTANCE.setModelValue(this, model, "");
 346  0
     }
 347  
 
 348  
     @Override
 349  
     public void updateWidgetData(DataModel model) {
 350  0
         SectionBinding.INSTANCE.setWidgetValue(this, model, "");
 351  0
     }
 352  
 
 353  
     @Override
 354  
     public void resetFieldInteractionFlags() {
 355  0
             this.isDirty = false;
 356  0
         for(FieldDescriptor f: fields){
 357  0
             f.setDirty(false);
 358  0
             f.setHasHadFocus(false);
 359  
         }
 360  
 
 361  0
         for(Section s: sections){
 362  0
             s.resetFieldInteractionFlags();
 363  
         }
 364  
 
 365  0
     }
 366  
 
 367  
     @Override
 368  
     public void resetDirtyFlags() {
 369  0
             this.isDirty = false;
 370  0
         for(FieldDescriptor f: fields){
 371  0
             f.setDirty(false);
 372  
         }
 373  
 
 374  0
         for(Section s: sections){
 375  0
             s.resetDirtyFlags();
 376  
         }
 377  0
     }
 378  
 
 379  
 
 380  
         @Override
 381  
         public String addSection(String key, Section section) {
 382  0
         sections.add(section);
 383  0
         section.getLayout().setKey(key);
 384  0
         String rkey = layout.addLayout(section.getLayout());
 385  0
         return rkey;
 386  
         }
 387  
 
 388  
         @Override
 389  
         public FieldDescriptor getField(String fieldKey) {
 390  0
                 for(FieldDescriptor f: fields){
 391  0
                         if(f.getFieldKey().equals(fieldKey)){
 392  0
                                 return f;
 393  
                         }
 394  
                 }
 395  0
                 return null;
 396  
         }
 397  
 
 398  
         @Override
 399  
         public FieldLayout getLayout() {
 400  0
                 return layout;
 401  
         }
 402  
 
 403  
         @Override
 404  
         public Section getSection(String sectionKey) {
 405  0
                 for(Section s: sections){
 406  0
                         if(s.getLayout().getKey().equals(sectionKey)){
 407  0
                                 return s;
 408  
                         }
 409  
                 }
 410  0
                 return null;
 411  
         }
 412  
 
 413  
         @Override
 414  
         public void removeField(String fieldKey) {
 415  0
                 int index = 0;
 416  0
                 boolean found = false;
 417  0
                 for(FieldDescriptor f: fields){
 418  0
                         if(f.getFieldKey().equals(fieldKey)){
 419  0
                                 index = fields.indexOf(f);
 420  0
                                 found = true;
 421  0
                                 break;
 422  
                         }
 423  
                 }
 424  0
                 if(found){
 425  0
                         fields.remove(index);
 426  
                 }
 427  0
                 layout.removeLayoutElement(fieldKey);
 428  
 
 429  0
         }
 430  
 
 431  
         @Override
 432  
         public void removeField(FieldDescriptor field) {
 433  
 
 434  0
                 fields.remove(field);
 435  0
                 layout.removeLayoutElement(field.getFieldKey());
 436  
 
 437  0
         }
 438  
 
 439  
         @Override
 440  
         public void removeSection(String sectionKey) {
 441  0
                 int index = 0;
 442  0
                 boolean found = false;
 443  0
                 for(Section s: sections){
 444  0
                         if(s.getLayout().getKey().equals(sectionKey)){
 445  0
                                 index = sections.indexOf(s);
 446  0
                                 found = true;
 447  0
                                 break;
 448  
                         }
 449  
                 }
 450  0
                 if(found){
 451  0
                         sections.remove(index);
 452  
                 }
 453  0
                 layout.removeLayoutElement(sectionKey);
 454  
 
 455  0
         }
 456  
 
 457  
         @Override
 458  
         public void removeWidget(Widget widget) {
 459  0
                 layout.removeLayoutElement(widget);
 460  
 
 461  0
         }
 462  
 
 463  
         @Override
 464  
         public void removeWidget(String key) {
 465  0
                 layout.removeLayoutElement(key);
 466  
 
 467  0
         }
 468  
 
 469  
         public boolean isDirty(){
 470  0
                 if(!this.isDirty){
 471  
                         //Check child sections for dirtyness
 472  0
                         for(Section s: sections){
 473  0
                                 if(s.isDirty()){
 474  0
                                         setIsDirty(true);
 475  0
                                         break;
 476  
                                 }
 477  
                         }
 478  
                 }
 479  0
                 return isDirty;
 480  
         }
 481  
 
 482  
     public void setIsDirty(boolean state) {
 483  
                 //Should this trust layoutController to be already set?
 484  0
             if (layoutController == null){
 485  0
                     layoutController = LayoutController.findParentLayout(layout);
 486  
             }
 487  0
             if (isDirty != state){
 488  0
                 isDirty = state;
 489  0
                     if (layoutController != null && isDirty){
 490  0
                             layoutController.fireApplicationEvent(new ContentDirtyEvent());
 491  
                     }
 492  
             }
 493  0
     }
 494  
 
 495  
         /**
 496  
          * Do not use this method for adding sections, fields, or widgets to sections
 497  
          */
 498  
         @Override
 499  
         public void add(Widget w) {
 500  0
                 super.add(w);
 501  0
         }
 502  
 
 503  
         @Override
 504  
         public void addStyleName(String style) {
 505  0
                 layout.addStyleName(style);
 506  0
         }
 507  
 
 508  
         @Override
 509  
         public void setStyleName(String style) {
 510  0
                 layout.setStyleName(style);
 511  0
         }
 512  
 
 513  
         public void setInstructions(String html){
 514  0
                 layout.setInstructions(html);
 515  0
         }
 516  
 
 517  
         public void setHelp(String html){
 518  0
                 layout.setHelp(html);
 519  0
         }
 520  
 }