Coverage Report - org.kuali.student.lum.program.client.ProgramController
 
Classes in this File Line Coverage Branch Coverage Complexity
ProgramController
0%
0/155
0%
0/62
2.184
ProgramController$1
0%
0/11
0%
0/6
2.184
ProgramController$1$1
0%
0/15
0%
0/4
2.184
ProgramController$2
0%
0/5
0%
0/4
2.184
ProgramController$3
0%
0/9
N/A
2.184
ProgramController$4
0%
0/7
0%
0/2
2.184
ProgramController$5
0%
0/10
N/A
2.184
ProgramController$6
0%
0/3
N/A
2.184
ProgramController$7
0%
0/1
N/A
2.184
 
 1  
 package org.kuali.student.lum.program.client;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.HashMap;
 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.Metadata;
 10  
 import org.kuali.student.common.dto.DtoConstants;
 11  
 import org.kuali.student.common.rice.authorization.PermissionType;
 12  
 import org.kuali.student.common.ui.client.application.ViewContext;
 13  
 import org.kuali.student.common.ui.client.configurable.mvc.layouts.MenuSectionController;
 14  
 import org.kuali.student.common.ui.client.configurable.mvc.sections.Section;
 15  
 import org.kuali.student.common.ui.client.configurable.mvc.views.SectionView;
 16  
 import org.kuali.student.common.ui.client.mvc.Callback;
 17  
 import org.kuali.student.common.ui.client.mvc.DataModel;
 18  
 import org.kuali.student.common.ui.client.mvc.DataModelDefinition;
 19  
 import org.kuali.student.common.ui.client.mvc.ModelProvider;
 20  
 import org.kuali.student.common.ui.client.mvc.ModelRequestCallback;
 21  
 import org.kuali.student.common.ui.client.mvc.View;
 22  
 import org.kuali.student.common.ui.client.mvc.dto.ReferenceModel;
 23  
 import org.kuali.student.common.ui.client.mvc.history.HistoryManager;
 24  
 import org.kuali.student.common.ui.client.util.ExportElement;
 25  
 import org.kuali.student.common.ui.client.util.ExportUtils;
 26  
 import org.kuali.student.common.ui.client.widgets.KSButton;
 27  
 import org.kuali.student.common.ui.client.widgets.KSButtonAbstract;
 28  
 import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonEnumerations;
 29  
 import org.kuali.student.common.ui.client.widgets.dialog.ButtonMessageDialog;
 30  
 import org.kuali.student.common.ui.client.widgets.field.layout.button.ButtonGroup;
 31  
 import org.kuali.student.common.ui.client.widgets.field.layout.button.YesNoCancelGroup;
 32  
 import org.kuali.student.common.ui.shared.IdAttributes;
 33  
 import org.kuali.student.common.ui.shared.IdAttributes.IdType;
 34  
 import org.kuali.student.core.comments.ui.client.widgets.commenttool.CommentTool;
 35  
 import org.kuali.student.lum.common.client.helpers.RecentlyViewedHelper;
 36  
 import org.kuali.student.lum.common.client.widgets.AppLocations;
 37  
 import org.kuali.student.lum.program.client.events.ModelLoadedEvent;
 38  
 import org.kuali.student.lum.program.client.events.UpdateEvent;
 39  
 import org.kuali.student.lum.program.client.properties.ProgramProperties;
 40  
 import org.kuali.student.lum.program.client.rpc.AbstractCallback;
 41  
 import org.kuali.student.lum.program.client.rpc.MajorDisciplineRpcService;
 42  
 import org.kuali.student.lum.program.client.rpc.MajorDisciplineRpcServiceAsync;
 43  
 import org.kuali.student.lum.program.client.widgets.ProgramSideBar;
 44  
 
 45  
 import com.google.gwt.core.client.GWT;
 46  
 import com.google.gwt.event.dom.client.ClickEvent;
 47  
 import com.google.gwt.event.dom.client.ClickHandler;
 48  
 import com.google.gwt.event.shared.HandlerManager;
 49  
 import com.google.gwt.user.client.ui.Label;
 50  
 import com.google.gwt.user.client.ui.Widget;
 51  
 
 52  
 /**
 53  
  * @author Igor
 54  
  */
 55  0
 public abstract class ProgramController extends MenuSectionController {
 56  
 
 57  
     protected MajorDisciplineRpcServiceAsync programRemoteService;
 58  
 
 59  0
     protected boolean initialized = false;
 60  
 
 61  
     protected DataModel programModel;
 62  
 
 63  
     protected AbstractProgramConfigurer configurer;
 64  
 
 65  
     protected HandlerManager eventBus;
 66  
 
 67  0
     protected Label statusLabel = new Label();
 68  
 
 69  
     protected ProgramSideBar sideBar;
 70  
 
 71  0
     private boolean needToLoadOldModel = false;
 72  
 
 73  
     private ProgramStatus lastLoadedStatus;
 74  
 
 75  0
     protected boolean reloadMetadata = false;
 76  
 
 77  0
     protected boolean processBeforeShow = true;
 78  
 
 79  
     /**
 80  
      * Constructor.
 81  
      *
 82  
      * @param programModel
 83  
      */
 84  
     public ProgramController(String name, DataModel programModel, ViewContext viewContext, HandlerManager eventBus) {
 85  0
         super();
 86  0
         programRemoteService = createProgramRemoteService();
 87  0
         this.eventBus = eventBus;
 88  0
         this.programModel = programModel;
 89  0
         setViewContext(viewContext);
 90  0
         initializeModel();
 91  0
     }
 92  
 
 93  
 
 94  
     /**
 95  
      * Create a ProgramRpcServiceAsync appropriate for this Controller
 96  
      */
 97  
     protected MajorDisciplineRpcServiceAsync createProgramRemoteService() {
 98  0
         return GWT.create(MajorDisciplineRpcService.class);
 99  
     }
 100  
 
 101  
     @Override
 102  
     public void beforeViewChange(Enum<?> viewChangingTo, final Callback<Boolean> okToChange) {
 103  0
         if (processBeforeShow) {
 104  0
             super.beforeViewChange(viewChangingTo, new Callback<Boolean>() {
 105  
 
 106  
                 @Override
 107  
                 public void exec(Boolean result) {
 108  0
                     if (result) {
 109  0
                         if (getCurrentView() instanceof SectionView && ((SectionView) getCurrentView()).isDirty()) {
 110  0
                             ButtonGroup<ButtonEnumerations.YesNoCancelEnum> buttonGroup = new YesNoCancelGroup();
 111  0
                             final ButtonMessageDialog<ButtonEnumerations.YesNoCancelEnum> dialog = new ButtonMessageDialog<ButtonEnumerations.YesNoCancelEnum>("Warning", "You may have unsaved changes.  Save changes?", buttonGroup);
 112  0
                             buttonGroup.addCallback(new Callback<ButtonEnumerations.YesNoCancelEnum>() {
 113  
 
 114  
                                 @Override
 115  
                                 public void exec(ButtonEnumerations.YesNoCancelEnum result) {
 116  0
                                     switch (result) {
 117  
                                         case YES:
 118  0
                                             dialog.hide();
 119  0
                                             fireUpdateEvent(okToChange);
 120  0
                                             break;
 121  
                                         case NO:
 122  0
                                             dialog.hide();
 123  0
                                             resetModel();
 124  0
                                             needToLoadOldModel = true;
 125  0
                                             resetFieldInteractionFlag();
 126  0
                                             okToChange.exec(true);
 127  0
                                             break;
 128  
                                         case CANCEL:
 129  0
                                             okToChange.exec(false);
 130  0
                                             dialog.hide();
 131  
                                             // Because this event fires after the history change event we need to "undo" the history events. 
 132  0
                                             HistoryManager.logHistoryChange();  
 133  
                                             break;
 134  
                                     }
 135  0
                                 }
 136  
                             });
 137  0
                             dialog.show();
 138  0
                         } else {
 139  0
                             okToChange.exec(true);
 140  
                         }
 141  
                     } else {
 142  0
                         okToChange.exec(false);
 143  
                     }
 144  0
                 }
 145  
             });
 146  
         } else {
 147  0
             processBeforeShow = true;
 148  
         }
 149  0
     }
 150  
 
 151  
     protected void fireUpdateEvent(final Callback<Boolean> okToChange) {
 152  0
         eventBus.fireEvent(new UpdateEvent(okToChange));
 153  0
     }
 154  
 
 155  
     protected void resetModel() {
 156  0
         programModel.resetRoot();
 157  0
     }
 158  
 
 159  
     protected void resetFieldInteractionFlag() {
 160  0
         View currentView = getCurrentView();
 161  0
         if (currentView instanceof Section) {
 162  0
             ((Section) currentView).resetFieldInteractionFlags();
 163  
         }
 164  0
     }
 165  
 
 166  
     /**
 167  
      * Initialized model of the controller.
 168  
      */
 169  
     private void initializeModel() {
 170  0
         setDefaultModelId(ProgramConstants.PROGRAM_MODEL_ID);
 171  0
         registerModel(ProgramConstants.PROGRAM_MODEL_ID, new ModelProvider<DataModel>() {
 172  
             @Override
 173  
             public void requestModel(final ModelRequestCallback<DataModel> callback) {
 174  0
                 if (programModel.getRoot() == null || programModel.getRoot().size() == 0) {
 175  0
                     loadModel(callback);
 176  
                 } else {
 177  0
                     callback.onModelReady(programModel);
 178  
                 }
 179  0
             }
 180  
         });
 181  0
     }
 182  
 
 183  
 
 184  
     @Override
 185  
     public void requestModel(Class modelType, ModelRequestCallback callback) {
 186  0
         if (modelType == ReferenceModel.class) {
 187  0
             ReferenceModel referenceModel = new ReferenceModel();
 188  0
             referenceModel.setReferenceId(ProgramUtils.getProgramId(programModel));
 189  0
             referenceModel.setReferenceTypeKey(ProgramConstants.MAJOR_REFERENCE_TYPE_ID);
 190  0
             referenceModel.setReferenceType(ProgramConstants.MAJOR_LU_TYPE_ID);
 191  0
             Map<String, String> attributes = new HashMap<String, String>();
 192  0
             attributes.put("name", getStringProperty("name"));
 193  0
             referenceModel.setReferenceAttributes(attributes);
 194  0
             callback.onModelReady(referenceModel);
 195  0
         } else {
 196  0
             super.requestModel(modelType, callback);
 197  
         }
 198  0
     }
 199  
 
 200  
 
 201  
     /**
 202  
      * Loads data model from the server.
 203  
      *
 204  
      * @param callback we have to invoke this callback when model is loaded or failed.
 205  
      */
 206  
     protected void loadModel(final ModelRequestCallback<DataModel> callback) {
 207  0
         programRemoteService.getData(getViewContext().getId(), new AbstractCallback<Data>(ProgramProperties.get().common_retrievingData()) {
 208  
 
 209  
             @Override
 210  
             public void onFailure(Throwable caught) {
 211  0
                 super.onFailure(caught);
 212  0
                 callback.onRequestFail(caught);
 213  0
             }
 214  
 
 215  
             @Override
 216  
             public void onSuccess(Data result) {
 217  0
                 super.onSuccess(result);
 218  0
                 programModel.setRoot(result);
 219  0
                 setHeaderTitle();
 220  0
                 callback.onModelReady(programModel);
 221  0
             }
 222  
         });
 223  0
     }
 224  
 
 225  
     private void setModelData() {
 226  0
         setHeaderTitle();
 227  0
         setStatus();
 228  0
         configurer.applyPermissions();
 229  
         //We don't want to throw ModelLoadedEvent when we just want to rollback the model
 230  0
         if (needToLoadOldModel) {
 231  0
             needToLoadOldModel = false;
 232  
         } else {
 233  0
             String id = ProgramUtils.getProgramId(programModel);
 234  0
             if (null != id) {
 235  
                 // add to recently viewed
 236  0
                 ViewContext docContext = new ViewContext();
 237  0
                 docContext.setId(id);
 238  0
                 docContext.setIdType(IdType.OBJECT_ID);
 239  0
                 String pgmType = getStringProperty(ProgramConstants.TYPE);
 240  0
                 docContext.setAttribute(ProgramConstants.TYPE, pgmType + '/' + ProgramSections.PROGRAM_DETAILS_VIEW);
 241  0
                 RecentlyViewedHelper.addDocument(getProgramName(),
 242  
                         HistoryManager.appendContext(getProgramViewLocation(pgmType), docContext));
 243  
             }
 244  0
             eventBus.fireEvent(new ModelLoadedEvent(programModel));
 245  0
             onModelLoadedEvent();
 246  
         }
 247  0
     }
 248  
 
 249  
     private String getProgramViewLocation(String pgmType) {
 250  0
         if (ProgramClientConstants.MAJOR_PROGRAM.equals(pgmType)) {
 251  0
             return AppLocations.Locations.VIEW_PROGRAM.getLocation();
 252  0
         } else if (ProgramClientConstants.CORE_PROGRAM.equals(pgmType)) {
 253  0
             return AppLocations.Locations.VIEW_CORE_PROGRAM.getLocation();
 254  0
         } else if (ProgramClientConstants.CREDENTIAL_PROGRAM_TYPES.contains(pgmType)) {
 255  0
             return AppLocations.Locations.VIEW_BACC_PROGRAM.getLocation();
 256  
         }
 257  0
         return null;
 258  
     }
 259  
 
 260  
     protected void setStatus() {
 261  0
         statusLabel.setText(ProgramProperties.get().common_status(getStringProperty(ProgramConstants.STATE)));
 262  0
     }
 263  
 
 264  
     public String getProgramName() {
 265  0
         String name = getStringProperty(ProgramConstants.LONG_TITLE);
 266  0
         if (name == null) {
 267  0
             name = ProgramProperties.get().common_newProgram();
 268  
         }
 269  0
         return name;
 270  
     }
 271  
 
 272  
     /**
 273  
      * Got invoked by framework before showing the view of the controller.
 274  
      *
 275  
      * @param onReadyCallback
 276  
      */
 277  
     @Override
 278  
     public void beforeShow(final Callback<Boolean> onReadyCallback) {
 279  0
         if (programModel.getRoot() == null) {
 280  0
             loadModel(new ModelRequestCallback<DataModel>() {
 281  
                 @Override
 282  
                 public void onModelReady(DataModel model) {
 283  0
                     if (loadMetadataCondition()) {
 284  0
                         loadMetadata(onReadyCallback);
 285  
                     } else {
 286  
 
 287  0
                         onReadyCallback.exec(true);
 288  
                     }
 289  0
                 }
 290  
 
 291  
                 @Override
 292  
                 public void onRequestFail(Throwable cause) {
 293  0
                     GWT.log(cause.getMessage());
 294  0
                 }
 295  
             });
 296  
         } else {
 297  0
             afterMetadataLoaded(onReadyCallback);
 298  
         }
 299  0
     }
 300  
 
 301  
     /**
 302  
      * We should only load metadata if the status of model is changed.
 303  
      *
 304  
      * @return
 305  
      */
 306  
     protected boolean loadMetadataCondition() {
 307  0
         return lastLoadedStatus == null || ProgramStatus.of(programModel) != lastLoadedStatus;
 308  
     }
 309  
 
 310  
     /**
 311  
      * Loads metadata from the server.
 312  
      *
 313  
      * @param onReadyCallback
 314  
      */
 315  
     protected void loadMetadata(final Callback<Boolean> onReadyCallback) {
 316  0
         Map<String, String> idAttributes = new HashMap<String, String>();
 317  0
         ViewContext viewContext = getViewContext();
 318  0
         IdType idType = viewContext.getIdType();
 319  0
         String viewContextId = null;
 320  0
         if (idType != null) {
 321  0
             idAttributes.put(IdAttributes.ID_TYPE, idType.toString());
 322  0
             viewContextId = viewContext.getId();
 323  0
             if (idType == IdType.COPY_OF_OBJECT_ID) {
 324  0
                 viewContextId = null;
 325  
             }
 326  
         }
 327  0
         if (programModel.getRoot() != null) {
 328  0
             ProgramStatus programStatus = ProgramStatus.of(programModel);
 329  0
             idAttributes.put(DtoConstants.DTO_STATE, programStatus.getValue());
 330  0
             if (programStatus.getNextStatus() != null) {
 331  0
                 idAttributes.put(DtoConstants.DTO_NEXT_STATE, programStatus.getNextStatus().getValue());
 332  
             }
 333  
         }
 334  0
         programRemoteService.getMetadata(viewContextId, idAttributes, new AbstractCallback<Metadata>() {
 335  
 
 336  
             @Override
 337  
             public void onSuccess(Metadata result) {
 338  0
                 super.onSuccess(result);
 339  0
                 DataModelDefinition def = new DataModelDefinition(result);
 340  0
                 programModel.setDefinition(def);
 341  0
                 lastLoadedStatus = ProgramStatus.of(programModel);
 342  0
                 afterMetadataLoaded(onReadyCallback);
 343  0
             }
 344  
 
 345  
             @Override
 346  
             public void onFailure(Throwable caught) {
 347  0
                 super.onFailure(caught);
 348  0
                 onReadyCallback.exec(false);
 349  0
             }
 350  
         });
 351  0
     }
 352  
 
 353  
     protected void configureView() {
 354  0
         addStyleName("programController");
 355  0
         configurer.setModelDefinition(programModel.getDefinition());
 356  0
         configurer.configure(this);
 357  0
         addContentWidget(statusLabel);
 358  0
         setSideBarWidget(sideBar);
 359  0
     }
 360  
 
 361  
     @Override
 362  
     public void setViewContext(ViewContext viewContext) {
 363  0
         super.setViewContext(viewContext);
 364  0
         if (viewContext.getId() != null && !viewContext.getId().isEmpty()) {
 365  0
             viewContext.setPermissionType(PermissionType.OPEN);
 366  
         } else {
 367  0
             viewContext.setPermissionType(PermissionType.INITIATE);
 368  
         }
 369  0
     }
 370  
 
 371  
     /**
 372  
      * Called when metadata is loaded.
 373  
      *
 374  
      * @param onReadyCallback
 375  
      */
 376  
     protected void afterMetadataLoaded(Callback<Boolean> onReadyCallback) {
 377  0
         if (!reloadMetadata) {
 378  0
             configureView();
 379  0
             onReadyCallback.exec(true);
 380  0
             reloadMetadata = true;
 381  
         } else {
 382  0
             onReadyCallback.exec(true);
 383  0
             ProgramUtils.syncMetadata(configurer, programModel.getDefinition());
 384  
         }
 385  0
         if (programModel.getRoot() != null) {
 386  0
             setModelData();
 387  
         }
 388  0
     }
 389  
 
 390  
     protected void setHeaderTitle() {
 391  0
         String title = getProgramName();
 392  0
         this.setContentTitle(title);
 393  0
         this.setName(title);
 394  0
     }
 395  
 
 396  
     protected Widget createCommentPanel() {
 397  0
         final CommentTool commentTool = new CommentTool(ProgramSections.COMMENTS, "Comments", "kuali.comment.type.generalRemarks", "Program Comments");
 398  0
         commentTool.setController(this);
 399  0
         KSButton commentsButton = new KSButton(ProgramProperties.get().comments_button(), KSButtonAbstract.ButtonStyle.DEFAULT_ANCHOR, new ClickHandler() {
 400  
 
 401  
             @Override
 402  
             public void onClick(ClickEvent event) {
 403  0
                 commentTool.show();
 404  0
             }
 405  
         });
 406  0
         return commentsButton;
 407  
     }
 408  
 
 409  
     protected void doSave() {
 410  0
     }
 411  
 
 412  
     public DataModel getProgramModel() {
 413  0
         return programModel;
 414  
     }
 415  
 
 416  
     public void onModelLoadedEvent() {
 417  0
     }
 418  
 
 419  
     protected String getStringProperty(String key) {
 420  0
         return programModel.get(key);
 421  
     }
 422  
 
 423  
     protected Data getDataProperty(String key) {
 424  0
         return programModel.get(key);
 425  
     }
 426  
     
 427  
     public boolean isExportButtonActive() {
 428  0
         if (this.getCurrentViewEnum() != null) {
 429  0
             if (this.getCurrentViewEnum().equals(ProgramSections.SUMMARY) 
 430  
                     || this.getCurrentViewEnum().equals(ProgramSections.VIEW_ALL)) {
 431  0
                 return true;            
 432  
             } else {
 433  0
                 return false;
 434  
             }
 435  
             
 436  
         } else {
 437  0
             return false;
 438  
         }
 439  
     }
 440  
     
 441  
     @Override
 442  
     public ArrayList<ExportElement> getExportElementsFromView() {
 443  
 
 444  0
         String viewName = null;
 445  0
         String sectionTitle = null;
 446  0
         View currentView = this.getCurrentView();
 447  0
         if (currentView != null) {
 448  
             
 449  0
             ArrayList<ExportElement> exportElements = new ArrayList<ExportElement>();
 450  0
             if (currentView != null && currentView instanceof Section) {
 451  0
                 Section currentSection = (Section) currentView;
 452  0
                 List<Section> nestedSections = currentSection.getSections();
 453  0
                 for (int i = 0; i < nestedSections.size(); i++) {
 454  0
                     ExportElement sectionExportItem = new ExportElement();
 455  0
                     ArrayList<ExportElement> subList = null;
 456  0
                     Section nestedSection = nestedSections.get(i);
 457  0
                     if (nestedSection != null && nestedSection instanceof SectionView) {
 458  0
                         SectionView nestedSectionView = (SectionView) nestedSection;
 459  0
                         viewName =  nestedSectionView.getName();
 460  0
                         sectionTitle = nestedSectionView.getTitle();
 461  0
                         sectionExportItem.setSectionName(sectionTitle + " " + i + " - " + viewName);
 462  0
                         sectionExportItem.setViewName(sectionTitle + " " + i + " - " + viewName);
 463  0
                         subList = ExportUtils.getExportElementsFromView(nestedSectionView, subList, viewName, sectionTitle);
 464  0
                         if (subList != null && subList.size()> 0) {
 465  0
                             sectionExportItem.setSubset(subList);
 466  0
                             exportElements.add(sectionExportItem);
 467  
                         }
 468  
                     }                    
 469  
                 }
 470  
             }
 471  0
             return exportElements;
 472  
             
 473  
         } else {
 474  
 //            logger.warn("ExportUtils.getExportElementsFromView controller currentView is null :" + this.getClass().getName());
 475  
         }
 476  0
         return null;
 477  
     
 478  
     }
 479  
 }