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