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.375
VariationsBinding$1
0%
0/12
0%
0/2
2.375
VariationsBinding$2
0%
0/5
0%
0/2
2.375
 
 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
                         if(variationData.get("id")!=null){
 79  0
                                 viewContext.setAttribute(ProgramConstants.VARIATION_ID, variationData.get("id").toString());
 80  
                         }
 81  0
                         HistoryManager.navigate(url, viewContext);
 82  0
                     }
 83  
                 });
 84  
 
 85  0
                 table.setWidget(row, 0, anchor);
 86  0
                 if (editable) {
 87  0
                     KSButton removeButton = new KSButton(ProgramProperties.get().common_remove());
 88  0
                     table.setWidget(row, 1, removeButton);                                             
 89  0
                     removeButton.addClickHandler(new ClickHandler() {
 90  
 
 91  
                         @Override
 92  
                         public void onClick(ClickEvent event) {
 93  0
                             if (Window.confirm("Are you sure you want to delete specialization?")) {
 94  0
                                 variationMap.remove(new Data.IntegerKey(currentRow));
 95  0
                                 MajorManager.getEventBus().fireEvent(new UpdateEvent());
 96  
                             }
 97  0
                         }
 98  
                     });
 99  0
                     if (configuration != null && configuration.checkPermission(model)) {
 100  0
                         removeButton.setEnabled(false);
 101  
                     }
 102  
 
 103  
                 }
 104  
 
 105  
                 //Set warning message if specialization has warnings
 106  0
                 if (hasWarnings(currentRow)){
 107  0
                     table.getRowFormatter().addStyleName(row, "warning");
 108  0
                     SpanPanel warning = new SpanPanel();
 109  0
                     warning.setHTML("<b> Warning </b> <br/>" + 
 110  
                                     Application.getApplicationContext().getMessage("validation", "validation.variation"));
 111  0
                     table.setWidget(row, 2, warning);
 112  0
                 } else {
 113  0
                         table.getRowFormatter().removeStyleName(row, "warning");
 114  0
                     table.setWidget(row, 2, new KSLabel(""));                        
 115  
                 }
 116  
                 
 117  0
                 row++;
 118  0
             }
 119  
         }
 120  0
     }
 121  
 
 122  
     private String getVariationName(Data data) {
 123  0
         StringBuilder nameBuilder = new StringBuilder();
 124  0
         nameBuilder.append(data.<Object>get(ProgramConstants.LONG_TITLE));
 125  0
         nameBuilder.append(" (").append(data.<Object>get(ProgramConstants.CODE)).append(")");
 126  0
         return nameBuilder.toString();
 127  
     }
 128  
     
 129  
     /**
 130  
      * This checks the validation warnings stored in the app context to see if any warnings exist
 131  
      * on the specialization.
 132  
      * 
 133  
      * @param variationIndex
 134  
      * @return true of variation at variationIndex has any warnings.
 135  
      */
 136  
     private boolean hasWarnings(int variationIndex){
 137  0
             List<ValidationResultInfo> validationWarnings = Application.getApplicationContext().getValidationWarnings();
 138  0
             for (ValidationResultInfo vr:validationWarnings){
 139  0
                     if (vr.getElement().contains(ProgramConstants.VARIATIONS + "/" + variationIndex)){
 140  0
                         return true;
 141  
                     }
 142  
             }
 143  0
             return false;
 144  
     }
 145  
 }