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