View Javadoc
1   package org.kuali.student.lum.program.client.credential.view;
2   
3   import com.google.gwt.core.client.GWT;
4   import com.google.gwt.event.dom.client.ChangeEvent;
5   import com.google.gwt.event.dom.client.ChangeHandler;
6   import com.google.gwt.event.shared.HandlerManager;
7   
8   import org.kuali.student.common.ui.client.application.Application;
9   import org.kuali.student.common.ui.client.application.KSAsyncCallback;
10  import org.kuali.student.common.ui.client.application.ViewContext;
11  import org.kuali.student.common.ui.client.mvc.Callback;
12  import org.kuali.student.common.ui.client.mvc.DataModel;
13  import org.kuali.student.common.ui.client.mvc.history.HistoryManager;
14  import org.kuali.student.common.ui.client.security.AuthorizationCallback;
15  import org.kuali.student.common.ui.client.security.RequiresAuthorization;
16  import org.kuali.student.common.ui.shared.IdAttributes.IdType;
17  import org.kuali.student.lum.common.client.lu.LUUIPermissions;
18  import org.kuali.student.lum.common.client.widgets.AppLocations;
19  import org.kuali.student.lum.common.client.widgets.DropdownList;
20  import org.kuali.student.lum.program.client.ProgramConstants;
21  import org.kuali.student.lum.program.client.ProgramRegistry;
22  import org.kuali.student.lum.program.client.ProgramSections;
23  import org.kuali.student.lum.program.client.ProgramStatus;
24  import org.kuali.student.lum.program.client.credential.CredentialController;
25  import org.kuali.student.lum.program.client.events.ModelLoadedEvent;
26  import org.kuali.student.lum.program.client.events.ProgramViewEvent;
27  import org.kuali.student.lum.program.client.major.ActionType;
28  
29  /**
30   * @author Igor
31   */
32  public class CredentialViewController extends CredentialController implements RequiresAuthorization {
33      /**
34       * Initialize the action drop-down with a list of values.  Note that these values
35       * will be changed further down in the code depending on if we are working with the latest 
36       * version of the program.
37       */
38      private final DropdownList actionBox = new DropdownList(ActionType.getValuesForCredentialProgram(false));
39  
40      /**
41       * Constructor.
42       *
43       * @param programModel
44       */
45      public CredentialViewController(DataModel programModel, ViewContext viewContext, HandlerManager eventBus) {
46          super(programModel, viewContext, eventBus);
47          configurer = GWT.create(CredentialViewConfigurer.class);
48          bind();
49      }
50  
51      private void bind() {
52          actionBox.addChangeHandler(new ChangeHandler() {
53              @Override
54              public void onChange(ChangeEvent event) {
55                  ActionType actionType = ActionType.of(actionBox.getSelectedValue());
56                  ViewContext viewContext = getViewContext();
57                  if (actionType == ActionType.MODIFY) {
58                      ProgramRegistry.setSection(ProgramSections.getEditSection(getCurrentViewEnum()));
59                      HistoryManager.navigate(AppLocations.Locations.EDIT_BACC_PROGRAM.getLocation(), getViewContext());
60                  } else if (actionType == ActionType.MODIFY_VERSION) {
61                      String versionIndId = getStringProperty(ProgramConstants.VERSION_IND_ID);
62                      viewContext.setId(versionIndId);
63                      viewContext.setIdType(IdType.COPY_OF_OBJECT_ID);
64                      HistoryManager.navigate(AppLocations.Locations.EDIT_BACC_PROGRAM.getLocation(), viewContext);
65                  }
66              }
67          });
68          eventBus.addHandler(ProgramViewEvent.TYPE, new ProgramViewEvent.Handler() {
69              @Override
70              public void onEvent(ProgramViewEvent event) {
71                  actionBox.setSelectedIndex(0);
72              }
73          });
74          eventBus.addHandler(ModelLoadedEvent.TYPE, new ModelLoadedEvent.Handler() {
75              @Override
76              public void onEvent(ModelLoadedEvent event) {
77                  resetActionList();
78              }
79          });
80      }
81  
82      @Override
83      protected void configureView() {
84          super.configureView();
85          addContentWidget(actionBox);
86          initialized = true;
87      }
88  
89      protected void resetActionList() {
90          //Only allow modify with version option for an active course that id also the latest version
91          ProgramStatus status = ProgramStatus.of(programModel);
92          String versionIndId = getStringProperty(ProgramConstants.VERSION_IND_ID);
93          Long sequenceNumber = programModel.get(ProgramConstants.VERSION_SEQUENCE_NUMBER);
94  
95          actionBox.clear();
96          if (status == ProgramStatus.ACTIVE) {
97              programRemoteService.isLatestVersion(versionIndId, sequenceNumber, new KSAsyncCallback<Boolean>() {
98                  public void onSuccess(Boolean isLatest) {
99                      actionBox.setList(ActionType.getValuesForCredentialProgram(isLatest));
100                 }
101             });
102         } else {
103             actionBox.setList(ActionType.getValuesForCredentialProgram(false));
104         }
105     }
106     
107     @Override
108     public boolean isAuthorizationRequired() {
109         return true;
110     }
111 
112     @Override
113     public void setAuthorizationRequired(boolean required) {
114         throw new UnsupportedOperationException();
115     }
116     
117     @Override
118     public void checkAuthorization(final AuthorizationCallback authCallback) {
119         Application.getApplicationContext().getSecurityContext().checkScreenPermission(LUUIPermissions.USE_VIEW_CREDENTIAL_PROGRAMS_SCREEN, new Callback<Boolean>() {
120             @Override
121             public void exec(Boolean result) {
122 
123                 final boolean isAuthorized = result;
124             
125                 if(isAuthorized){
126                     authCallback.isAuthorized();
127                 }
128                 else
129                     authCallback.isNotAuthorized("User is not authorized: " + LUUIPermissions.USE_VIEW_CREDENTIAL_PROGRAMS_SCREEN);
130             }   
131         });
132     }
133 }