Coverage Report - org.kuali.student.lum.program.client.variation.VariationsBinding
 
Classes in this File Line Coverage Branch Coverage Complexity
VariationsBinding
0%
0/46
0%
0/16
2.25
VariationsBinding$1
0%
0/10
N/A
2.25
VariationsBinding$2
0%
0/5
0%
0/2
2.25
 
 1  
 package org.kuali.student.lum.program.client.variation;
 2  
 
 3  
 import java.util.List;
 4  
 
 5  
 import org.kuali.student.common.assembly.data.Data;
 6  
 import org.kuali.student.common.ui.client.application.Application;
 7  
 import org.kuali.student.common.ui.client.application.ViewContext;
 8  
 import org.kuali.student.common.ui.client.configurable.mvc.binding.ModelWidgetBindingSupport;
 9  
 import org.kuali.student.common.ui.client.mvc.DataModel;
 10  
 import org.kuali.student.common.ui.client.mvc.history.HistoryManager;
 11  
 import org.kuali.student.common.ui.client.widgets.KSButton;
 12  
 import org.kuali.student.common.ui.client.widgets.KSLabel;
 13  
 import org.kuali.student.common.ui.client.widgets.field.layout.element.SpanPanel;
 14  
 import org.kuali.student.common.ui.shared.IdAttributes.IdType;
 15  
 import org.kuali.student.common.validation.dto.ValidationResultInfo;
 16  
 import org.kuali.student.lum.common.client.configuration.Configuration;
 17  
 import org.kuali.student.lum.program.client.ProgramConstants;
 18  
 import org.kuali.student.lum.program.client.ProgramRegistry;
 19  
 import org.kuali.student.lum.program.client.ProgramUtils;
 20  
 import org.kuali.student.lum.program.client.events.UpdateEvent;
 21  
 import org.kuali.student.lum.program.client.major.MajorManager;
 22  
 import org.kuali.student.lum.program.client.properties.ProgramProperties;
 23  
 
 24  
 import com.google.gwt.core.client.GWT;
 25  
 import com.google.gwt.event.dom.client.ClickEvent;
 26  
 import com.google.gwt.event.dom.client.ClickHandler;
 27  
 import com.google.gwt.user.client.Window;
 28  
 import com.google.gwt.user.client.ui.Anchor;
 29  
 import com.google.gwt.user.client.ui.FlexTable;
 30  
 
 31  
 /**
 32  
  * @author Igor
 33  
  */
 34  0
 public class VariationsBinding extends ModelWidgetBindingSupport<FlexTable> {
 35  
 
 36  
     private String url;
 37  
 
 38  
     private boolean editable;
 39  
 
 40  
     private Configuration configuration;
 41  
 
 42  0
     public VariationsBinding(String url, boolean editable) {
 43  0
         this.url = url;
 44  0
         this.editable = editable;
 45  0
     }
 46  
 
 47  
     public VariationsBinding(String url, boolean editable, Configuration configuration) {
 48  0
         this(url, editable);
 49  0
         this.configuration = configuration;
 50  0
     }
 51  
 
 52  
     @Override
 53  
     public void setModelValue(FlexTable table, DataModel model, String path) {
 54  0
         GWT.log("VariationsBinding.setModelValue...", null);
 55  0
     }
 56  
 
 57  
     @Override
 58  
     public void setWidgetValue(final FlexTable table, final DataModel model, String path) {               
 59  0
             table.clear();
 60  0
         final Data variationMap = model.get(path);        
 61  0
         if (variationMap != null) {
 62  0
             int row = 0;
 63  0
             for (final Data.Property property : variationMap) {
 64  0
                 final Data variationData = property.getValue();
 65  0
                 final int currentRow = row;
 66  
                        
 67  0
                 Anchor anchor = new Anchor(getVariationName(variationData));
 68  0
                 anchor.addClickHandler(new ClickHandler() {
 69  
                     @Override
 70  
                     public void onClick(ClickEvent event) {
 71  0
                         ProgramRegistry.setData(variationData);
 72  0
                         ProgramRegistry.setRow(currentRow);
 73  0
                         ProgramUtils.addCredentialProgramDataToVariation(variationData, model);
 74  0
                         String id = (String) model.get("id");
 75  0
                         ViewContext viewContext = new ViewContext();
 76  0
                         viewContext.setId(id);
 77  0
                         viewContext.setIdType(IdType.OBJECT_ID);
 78  0
                         HistoryManager.navigate(url, viewContext);
 79  0
                     }
 80  
                 });
 81  
 
 82  0
                 table.setWidget(row, 0, anchor);
 83  0
                 if (editable) {
 84  0
                     KSButton removeButton = new KSButton(ProgramProperties.get().common_remove());
 85  0
                     table.setWidget(row, 1, removeButton);                                             
 86  0
                     removeButton.addClickHandler(new ClickHandler() {
 87  
 
 88  
                         @Override
 89  
                         public void onClick(ClickEvent event) {
 90  0
                             if (Window.confirm("Are you sure you want to delete specialization?")) {
 91  0
                                 variationMap.remove(new Data.IntegerKey(currentRow));
 92  0
                                 MajorManager.getEventBus().fireEvent(new UpdateEvent());
 93  
                             }
 94  0
                         }
 95  
                     });
 96  0
                     if (configuration != null && configuration.checkPermission(model)) {
 97  0
                         removeButton.setEnabled(false);
 98  
                     }
 99  
 
 100  
                 }
 101  
 
 102  
                 //Set warning message if specialization has warnings
 103  0
                 if (hasWarnings(currentRow)){
 104  0
                     table.getRowFormatter().addStyleName(row, "warning");
 105  0
                     SpanPanel warning = new SpanPanel();
 106  0
                     warning.setHTML("<b> Warning </b> <br/>" + 
 107  
                                     Application.getApplicationContext().getMessage("validation", "validation.variation"));
 108  0
                     table.setWidget(row, 2, warning);
 109  0
                 } else {
 110  0
                         table.getRowFormatter().removeStyleName(row, "warning");
 111  0
                     table.setWidget(row, 2, new KSLabel(""));                        
 112  
                 }
 113  
                 
 114  0
                 row++;
 115  0
             }
 116  
         }
 117  0
     }
 118  
 
 119  
     private String getVariationName(Data data) {
 120  0
         StringBuilder nameBuilder = new StringBuilder();
 121  0
         nameBuilder.append(data.<Object>get(ProgramConstants.LONG_TITLE));
 122  0
         nameBuilder.append(" (").append(data.<Object>get(ProgramConstants.CODE)).append(")");
 123  0
         return nameBuilder.toString();
 124  
     }
 125  
     
 126  
     /**
 127  
      * This checks the validation warnings stored in the app context to see if any warnings exist
 128  
      * on the specialization.
 129  
      * 
 130  
      * @param variationIndex
 131  
      * @return true of variation at variationIndex has any warnings.
 132  
      */
 133  
     private boolean hasWarnings(int variationIndex){
 134  0
             List<ValidationResultInfo> validationWarnings = Application.getApplicationContext().getValidationWarnings();
 135  0
             for (ValidationResultInfo vr:validationWarnings){
 136  0
                     if (vr.getElement().contains(ProgramConstants.VARIATIONS + "/" + variationIndex)){
 137  0
                         return true;
 138  
                     }
 139  
             }
 140  0
             return false;
 141  
     }
 142  
 }