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