Coverage Report - org.kuali.student.lum.program.client.widgets.ProgramSideBar
 
Classes in this File Line Coverage Branch Coverage Complexity
ProgramSideBar
0%
0/95
0%
0/30
1.85
ProgramSideBar$1
0%
0/3
N/A
1.85
ProgramSideBar$2
0%
0/5
N/A
1.85
ProgramSideBar$3
0%
0/3
N/A
1.85
ProgramSideBar$4
0%
0/6
0%
0/4
1.85
ProgramSideBar$5
0%
0/1
N/A
1.85
ProgramSideBar$State
0%
0/3
N/A
1.85
ProgramSideBar$Type
0%
0/4
N/A
1.85
 
 1  
 package org.kuali.student.lum.program.client.widgets;
 2  
 
 3  
 import java.util.Date;
 4  
 import java.util.HashMap;
 5  
 import java.util.Map;
 6  
 
 7  
 import org.kuali.student.common.assembly.data.Metadata;
 8  
 import org.kuali.student.common.assembly.data.QueryPath;
 9  
 import org.kuali.student.common.ui.client.application.Application;
 10  
 import org.kuali.student.common.ui.client.application.ViewContext;
 11  
 import org.kuali.student.common.ui.client.configurable.mvc.DefaultWidgetFactory;
 12  
 import org.kuali.student.common.ui.client.configurable.mvc.binding.HasDataValueBinding;
 13  
 import org.kuali.student.common.ui.client.mvc.DataModel;
 14  
 import org.kuali.student.common.ui.client.mvc.HasDataValue;
 15  
 import org.kuali.student.common.ui.client.mvc.history.HistoryManager;
 16  
 import org.kuali.student.lum.common.client.widgets.AppLocations;
 17  
 import org.kuali.student.lum.program.client.ProgramConstants;
 18  
 import org.kuali.student.lum.program.client.ProgramMsgConstants;
 19  
 import org.kuali.student.lum.program.client.ProgramUtils;
 20  
 import org.kuali.student.lum.program.client.events.AfterSaveEvent;
 21  
 import org.kuali.student.lum.program.client.events.ModelLoadedEvent;
 22  
 import org.kuali.student.lum.program.client.major.MajorController;
 23  
 
 24  
 import com.google.gwt.event.dom.client.ClickEvent;
 25  
 import com.google.gwt.event.dom.client.ClickHandler;
 26  
 import com.google.gwt.event.shared.HandlerManager;
 27  
 import com.google.gwt.user.client.ui.Anchor;
 28  
 import com.google.gwt.user.client.ui.Composite;
 29  
 import com.google.gwt.user.client.ui.HorizontalPanel;
 30  
 import com.google.gwt.user.client.ui.Label;
 31  
 import com.google.gwt.user.client.ui.SimplePanel;
 32  
 import com.google.gwt.user.client.ui.VerticalPanel;
 33  
 import com.google.gwt.user.client.ui.Widget;
 34  
 
 35  
 /**
 36  
  * @author Igor
 37  
  */
 38  0
 public class ProgramSideBar extends Composite {
 39  
 
 40  0
     private final VerticalPanel content = new VerticalPanel();
 41  
 
 42  0
     private State state = State.VIEW;
 43  
 
 44  
     private Type type;
 45  
 
 46  
     private Label versionLabel;
 47  
     private Anchor viewVersion;
 48  0
     private Label historyLabel = new Label(getLabel(ProgramMsgConstants.SIDEBAR_HISTORY));
 49  0
     private Label lastUpdatedDate = new Label();
 50  0
     private SimplePanel scheduledReviewDate = new SimplePanel();
 51  0
     private Label lastReviewDate = new Label();
 52  
     private final HandlerManager eventBus;
 53  
 
 54  
     private ViewContext viewContext;
 55  
 
 56  
     private final SideBarDialogManager dialogManager;
 57  
 
 58  0
     public ProgramSideBar(HandlerManager eventBus, Type type) {
 59  0
         this.eventBus = eventBus;
 60  0
         this.type = type;
 61  0
         dialogManager = new SideBarDialogManager(eventBus);
 62  0
         initWidget(content);
 63  0
         setStyles();
 64  0
         buildLayout();
 65  0
         bind();
 66  0
     }
 67  
 
 68  
     public void initialize(MajorController controller) {
 69  0
         DataModel model = controller.getProgramModel();
 70  0
         dialogManager.configureView(model.getDefinition(), controller);
 71  0
         viewContext = controller.getViewContext();
 72  0
         updateFields(model);
 73  0
     }
 74  
 
 75  
     private void bind() {
 76  0
         eventBus.addHandler(ModelLoadedEvent.TYPE, new ModelLoadedEvent.Handler() {
 77  
             @Override
 78  
             public void onEvent(ModelLoadedEvent event) {
 79  0
                 updateFields(event.getModel());
 80  0
             }
 81  
         });
 82  0
         eventBus.addHandler(AfterSaveEvent.TYPE, new AfterSaveEvent.Handler() {
 83  
             @Override
 84  
             public void onEvent(AfterSaveEvent event) {
 85  0
                 DataModel model = event.getModel();
 86  0
                 dialogManager.configureView(model.getDefinition(), event.getController());
 87  0
                 updateFields(event.getModel());
 88  0
             }
 89  
         });
 90  0
     }
 91  
 
 92  
     private void updateFields(DataModel model) {
 93  0
             String programType = model.get(ProgramConstants.TYPE);
 94  
 
 95  
             //The sidebar fields should only be updated if the model that was changed/loaded is associated with the sidebar
 96  0
             boolean doUpdate =        (type == ProgramSideBar.Type.CORE && ProgramConstants.CORE_LU_TYPE_ID.equals(programType)) ||
 97  
                                                     (type == ProgramSideBar.Type.CREDENTIAL && programType.startsWith(ProgramConstants.CRED_LU_TYPE_PREFIX)) ||
 98  
                                                     (type == ProgramSideBar.Type.MAJOR && ProgramConstants.MAJOR_LU_TYPE_ID.equals(programType)); 
 99  
             
 100  0
             if (doUpdate){
 101  0
                     setVersion((Long)model.get(ProgramConstants.VERSION), versionLabel);
 102  0
                 setDate((Date) model.get(ProgramConstants.LAST_UPDATED_DATE), lastUpdatedDate);
 103  0
                 lastReviewDate.setText((String) model.get(ProgramConstants.LAST_REVIEW_DATE));
 104  0
                 setWidget(ProgramConstants.SCHEDULED_REVIEW_DATE, scheduledReviewDate, model);
 105  
             }
 106  0
     }
 107  
 
 108  
     private void setDate(Date updatedDate, Label lastUpdatedDate) {
 109  0
         if (updatedDate != null) {
 110  0
             lastUpdatedDate.setText(ProgramUtils.df.format(updatedDate));
 111  
         } else {
 112  0
             lastUpdatedDate.setText("");
 113  
         }
 114  0
     }
 115  
 
 116  
     private void setVersion(Long version, Label versionaLabel){
 117  0
             if (version != null){
 118  0
                        viewVersion.setVisible(version >= 1);
 119  0
                     versionLabel.setText(getLabel(ProgramMsgConstants.SIDEBAR_VERSION, String.valueOf(version)));
 120  
             } else {
 121  0
                     versionLabel.setText(getLabel(ProgramMsgConstants.SIDEBAR_VERSION, ""));
 122  0
                     viewVersion.setVisible(false);
 123  
             }
 124  0
     }
 125  
 
 126  
     private void setWidget(String path, SimplePanel container, DataModel model) {
 127  0
         Metadata mData = model.getMetadata(QueryPath.parse(path));
 128  
 
 129  
         // CoreProgram and CredentialProgram don't have SCHEDULED_REVIEW_DATE
 130  0
         if (null == mData && ProgramConstants.SCHEDULED_REVIEW_DATE.equals(path)) {
 131  0
             return;
 132  
         }
 133  0
         Widget widget = DefaultWidgetFactory.getInstance().getReadOnlyWidget(model.getMetadata(QueryPath.parse(path)));
 134  0
         HasDataValueBinding.INSTANCE.setWidgetValue((HasDataValue) widget, model, path);
 135  0
         container.setWidget(widget);
 136  0
     }
 137  
 
 138  
 
 139  
     private void buildLayout() {
 140  0
         content.clear();
 141  0
         Label history = new Label(getLabel(ProgramMsgConstants.SIDEBAR_HISTORY));
 142  0
         history.addStyleName("KS-Program-History");
 143  0
         content.add(history);
 144  
 
 145  0
         content.add(createVersionPanel());
 146  0
         content.add(createDatePanel(getLabel(ProgramMsgConstants.SIDEBAR_PROGRAMLASTUPDATED), lastUpdatedDate, false));
 147  0
         if (type == Type.MAJOR) {
 148  0
             content.add(createDatePanel(getLabel(ProgramMsgConstants.SIDEBAR_SCHEDULEDREVIEWDATE), scheduledReviewDate, true));
 149  0
             content.add(createDatePanel(getLabel(ProgramMsgConstants.SIDEBAR_LASTREVIEWDATE), lastReviewDate, true));
 150  
         }
 151  0
         content.add(createVersionHistoryPanel());
 152  0
     }
 153  
 
 154  
     private Widget createDatePanel(String title, Widget widget, boolean showEdit) {
 155  0
         VerticalPanel verticalPanel = new VerticalPanel();
 156  0
         HorizontalPanel datePanel = new HorizontalPanel();
 157  0
         datePanel.addStyleName("datePanel");
 158  0
         datePanel.add(widget);
 159  0
         if (state == State.EDIT && showEdit) {
 160  0
             Anchor edit = new Anchor(getLabel(ProgramMsgConstants.COMMON_EDIT));
 161  0
             edit.addClickHandler(new ClickHandler() {
 162  
 
 163  
                 @Override
 164  
                 public void onClick(ClickEvent event) {
 165  0
                     dialogManager.show();
 166  0
                 }
 167  
             });
 168  0
             datePanel.add(edit);
 169  
         }
 170  0
         verticalPanel.add(new Label(title));
 171  0
         verticalPanel.add(datePanel);
 172  0
         return verticalPanel;
 173  
     }
 174  
 
 175  
     private Widget createHistoryPanel(){
 176  0
             VerticalPanel  verticalPanel = new VerticalPanel();
 177  0
             versionLabel = new Label(getLabel(ProgramMsgConstants.SIDEBAR_VERSION, ""));
 178  0
             verticalPanel.add(versionLabel);
 179  0
             return verticalPanel;
 180  
     }
 181  
 
 182  
     private Widget createVersionPanel(){
 183  0
             VerticalPanel  verticalPanel = new VerticalPanel();
 184  0
             versionLabel = new Label(getLabel(ProgramMsgConstants.SIDEBAR_VERSION, ""));
 185  0
             verticalPanel.add(versionLabel);
 186  0
             return verticalPanel;
 187  
     }
 188  
 
 189  
     private Widget createVersionHistoryPanel(){
 190  0
             VerticalPanel  verticalPanel = new VerticalPanel();
 191  0
             viewVersion = new Anchor(getLabel(ProgramMsgConstants.SIDEBAR_VIEWHISTORY));
 192  0
             viewVersion.addClickHandler(new ClickHandler(){
 193  
                         @Override
 194  
                         public void onClick(ClickEvent event) {
 195  0
                                 switch (type){
 196  0
                                         case MAJOR: HistoryManager.navigate(AppLocations.Locations.VIEW_PROGRAM_VERSIONS.getLocation()); break;
 197  0
                                         case CORE: HistoryManager.navigate(AppLocations.Locations.VIEW_CORE_VERSIONS.getLocation()); break;
 198  0
                                         case CREDENTIAL: HistoryManager.navigate(AppLocations.Locations.VIEW_BACC_VERSIONS.getLocation()); break;
 199  
                                 }
 200  
 
 201  
 
 202  0
                         }
 203  
 
 204  
             });
 205  
             //viewVersion.setVisible(false);
 206  
 
 207  0
             verticalPanel.add(viewVersion);
 208  
 
 209  0
             return verticalPanel;
 210  
     }
 211  
 
 212  
     public void setState(State state) {
 213  0
         this.state = state;
 214  0
         buildLayout();
 215  0
     }
 216  
 
 217  
     private void setStyles() {
 218  0
         content.addStyleName("sideBar");
 219  0
         content.addStyleName("KS-Program-History-Sidebar");
 220  0
         historyLabel.addStyleName("history");
 221  0
     }
 222  
 
 223  0
     public static enum State {
 224  0
         EDIT,
 225  0
         VIEW
 226  
     }
 227  
 
 228  0
     public static enum Type {
 229  0
         CREDENTIAL,
 230  0
         CORE,
 231  0
         MAJOR
 232  
     }
 233  
     
 234  
     private String getLabel(String messageKey) {
 235  0
         return Application.getApplicationContext().getUILabel(ProgramMsgConstants.PROGRAM_MSG_GROUP, messageKey);
 236  
     }
 237  
     
 238  
     private String getLabel(String messageKey, String parameter) {
 239  0
         Map<String, Object> parameters = new HashMap<String, Object>();
 240  0
         parameters.put("0", parameter);
 241  0
         return Application.getApplicationContext().getUILabel(ProgramMsgConstants.PROGRAM_MSG_GROUP, messageKey, parameters);
 242  
     } 
 243  
 }