Coverage Report - org.kuali.student.common.ui.client.configurable.mvc.views.SectionView
 
Classes in this File Line Coverage Branch Coverage Complexity
SectionView
0%
0/54
0%
0/16
1.333
SectionView$1
0%
0/9
N/A
1.333
SectionView$2
0%
0/2
N/A
1.333
SectionView$3
0%
0/6
N/A
1.333
 
 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.views;
 17  
 
 18  
 import com.google.gwt.user.client.Window;
 19  
 import com.google.gwt.user.client.ui.Widget;
 20  
 
 21  
 import org.kuali.student.common.assembly.data.Metadata;
 22  
 import org.kuali.student.common.assembly.data.ModelDefinition;
 23  
 import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor;
 24  
 import org.kuali.student.common.ui.client.configurable.mvc.LayoutController;
 25  
 import org.kuali.student.common.ui.client.configurable.mvc.sections.BaseSection;
 26  
 import org.kuali.student.common.ui.client.configurable.mvc.sections.Section;
 27  
 import org.kuali.student.common.ui.client.mvc.*;
 28  
 
 29  
 import java.util.ArrayList;
 30  
 import java.util.List;
 31  
 
 32  
 
 33  
 /**
 34  
  * A view implementation of a section.  A section view is used to add sections as views to a controller.
 35  
  * 
 36  
  * @author Kuali Student
 37  
  *
 38  
  */
 39  
 public abstract class SectionView extends BaseSection implements View {
 40  
 
 41  
     protected String modelId;
 42  
     protected DataModel model;
 43  
 
 44  
     private Enum<?> viewEnum;
 45  
     private String viewName;
 46  
 
 47  0
     private List<View> views = new ArrayList<View>();
 48  
 
 49  
     /**
 50  
      * @param viewEnum Enumeration of this view - id used for navigation, history, and showing a view
 51  
      * @param viewName Name of this view - what this view is called in the breadcrumb
 52  
      */
 53  0
     public SectionView(Enum<?> viewEnum, String viewName) {
 54  0
         this.viewEnum = viewEnum;
 55  0
         this.viewName = viewName;
 56  0
     }
 57  
 
 58  
     /**
 59  
      * This method gets view name enumeration
 60  
      *
 61  
      * @return
 62  
      */
 63  
     @Override
 64  
     public Enum<?> getViewEnum() {
 65  0
         return viewEnum;
 66  
     }
 67  
 
 68  
     public void setViewEnum(Enum<?> viewEnum) {
 69  0
         this.viewEnum = viewEnum;
 70  0
     }
 71  
 
 72  
 
 73  
     /**
 74  
      * Called by controller before the view is displayed to allow lazy initialization or any other preparatory work to be
 75  
      * done.
 76  
      * In SectionView, the section is cleared of all validation errors, the model is requested from its parent
 77  
      * controller, the widgets are updated with the latest data, and beforeShow is called on all of its potential child
 78  
      * views.
 79  
      */
 80  
     @Override
 81  
     public void beforeShow(final Callback<Boolean> onReadyCallback) {
 82  
 
 83  0
         super.clearValidation();
 84  0
         if (getController() != null) {
 85  0
             getController().requestModel(modelId, new ModelRequestCallback<DataModel>() {
 86  
 
 87  
                 @Override
 88  
                 public void onRequestFail(Throwable cause) {
 89  0
                     Window.alert("Failed to get model: " + getName());
 90  0
                     onReadyCallback.exec(false);
 91  0
                 }
 92  
 
 93  
                 @Override
 94  
                 public void onModelReady(DataModel m) {
 95  0
                     model = m;
 96  0
                     updateWidgetData(m);
 97  0
                     resetFieldInteractionFlags();
 98  0
                     onReadyCallback.exec(true);
 99  0
                 }
 100  
 
 101  
             });
 102  
         }
 103  
 
 104  0
         for (Section section : sections) {
 105  0
             if (section instanceof SectionView) {
 106  0
                 ((SectionView) section).beforeShow(new Callback<Boolean>() {
 107  
                     @Override
 108  
                     public void exec(Boolean result) {
 109  0
                     }
 110  
                 });
 111  
             }
 112  
         }
 113  0
         for (View view : views) {
 114  0
             view.beforeShow(Controller.NO_OP_CALLBACK);
 115  
         }
 116  
 
 117  0
     }
 118  
 
 119  
     public String getModelId() {
 120  0
         return modelId;
 121  
     }
 122  
 
 123  
     public void setModelId(String modelId) {
 124  0
         this.modelId = modelId;
 125  0
     }
 126  
 
 127  
     /**
 128  
      * Called by the controller before the view is hidden to allow the view to perform cleanup or request confirmation from
 129  
      * the user, etc. Can cancel the action by returning false.
 130  
      *
 131  
      * @return true if the view can be hidden, or false to cancel the action.
 132  
      */
 133  
     @Override
 134  
     public boolean beforeHide() {
 135  0
         return true;
 136  
     }
 137  
 
 138  
     /**
 139  
      * Returns the controller associated with the view
 140  
      *
 141  
      * @see org.kuali.student.common.ui.client.mvc.View#getController()
 142  
      */
 143  
     @Override
 144  
     public Controller getController() {
 145  0
         return super.getLayoutController();
 146  
     }
 147  
 
 148  
     /**
 149  
      * Returns the view's name
 150  
      *
 151  
      * @see org.kuali.student.common.ui.client.mvc.View#getName()
 152  
      */
 153  
     @Override
 154  
     public String getName() {
 155  0
         return viewName;
 156  
     }
 157  
 
 158  
     public void setName(String name) {
 159  0
         this.viewName = name;
 160  0
     }
 161  
 
 162  
     public void setController(Controller controller) {
 163  0
         if (controller instanceof LayoutController) {
 164  0
             super.setLayoutController((LayoutController) controller);
 165  
         } else {
 166  0
             throw new IllegalArgumentException("Configurable UI sections require a LayoutController, not a base MVC controller");
 167  
         }
 168  0
     }
 169  
 
 170  
     /**
 171  
      * Update the fields on the screen with the model received back from requestModel on the parent controller
 172  
      */
 173  
     public void updateView() {
 174  0
         getController().requestModel(modelId, new ModelRequestCallback<DataModel>() {
 175  
             @Override
 176  
             public void onModelReady(DataModel m) {
 177  
                 // TODO review this, shouldn't it assign this.model = m?
 178  0
                 SectionView.this.model = m;
 179  0
                 updateWidgetData(m);
 180  0
             }
 181  
 
 182  
 
 183  
             @Override
 184  
             public void onRequestFail(Throwable cause) {
 185  0
                 Window.alert("Failed to get model");
 186  0
             }
 187  
         });
 188  
 
 189  0
     }
 190  
 
 191  
     /**
 192  
      * Force an update of fields on this section with the model passed in
 193  
      * @param m
 194  
      */
 195  
     public void updateView(DataModel m) {
 196  0
         this.model = m;
 197  0
         updateWidgetData(m);
 198  0
     }
 199  
 
 200  
     /**
 201  
      * @see org.kuali.student.common.ui.client.mvc.View#asWidget()
 202  
      */
 203  
     public Widget asWidget() {
 204  0
         return this.getLayout();
 205  
     }
 206  
 
 207  
     /**
 208  
      * @see org.kuali.student.common.ui.client.mvc.history.HistorySupport#collectHistory(java.lang.String)
 209  
      */
 210  
     @Override
 211  
     public String collectHistory(String historyStack) {
 212  0
         return null;
 213  
     }
 214  
 
 215  
     /**
 216  
      * @see org.kuali.student.common.ui.client.mvc.history.HistorySupport#onHistoryEvent(java.lang.String)
 217  
      */
 218  
     @Override
 219  
     public void onHistoryEvent(String historyStack) {
 220  
 
 221  0
     }
 222  
 
 223  
     /**
 224  
      * @see org.kuali.student.common.ui.client.mvc.breadcrumb.BreadcrumbSupport#collectBreadcrumbNames(java.util.List)
 225  
      */
 226  
     @Override
 227  
     public void collectBreadcrumbNames(List<String> names) {
 228  0
         names.add(this.getName());
 229  0
     }
 230  
 
 231  
     /**
 232  
      * Add a view as a widget to this section
 233  
      * @param view
 234  
      */
 235  
     public void addView(View view) {
 236  0
         views.add(view);
 237  0
         addWidget(view.asWidget());
 238  0
     }
 239  
 
 240  
     public DataModel getModel() {
 241  0
         return model;
 242  
     }
 243  
 
 244  
     public void updateMetadata(ModelDefinition modelDefinition) {
 245  0
         updateMetadata(modelDefinition, this);
 246  0
     }
 247  
 
 248  
     private void updateMetadata(ModelDefinition modelDefinition, Section topSection) {
 249  0
         for (Section section : topSection.getSections()) {
 250  0
             updateMetadata(modelDefinition, section);
 251  
         }
 252  0
         for (FieldDescriptor field : topSection.getFields()) {
 253  0
             Metadata newMetadata = modelDefinition.getMetadata(field.getFieldKey());
 254  0
             if (newMetadata != null) {
 255  0
                 field.setMetadata(newMetadata);
 256  
             }
 257  0
         }
 258  0
     }
 259  
 
 260  
     @Override
 261  
     public String toString() {
 262  0
         return viewName;
 263  
     }
 264  
 }