Coverage Report - org.kuali.rice.kns.uif.widget.Inquiry
 
Classes in this File Line Coverage Branch Coverage Complexity
Inquiry
0%
0/94
0%
0/30
2.167
 
 1  
 /*
 2  
  * Copyright 2007 The Kuali Foundation Licensed under the Educational Community
 3  
  * License, Version 1.0 (the "License"); you may not use this file except in
 4  
  * compliance with the License. You may obtain a copy of the License at
 5  
  * http://www.opensource.org/licenses/ecl1.php Unless required by applicable law
 6  
  * or agreed to in writing, software distributed under the License is
 7  
  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 8  
  * KIND, either express or implied. See the License for the specific language
 9  
  * governing permissions and limitations under the License.
 10  
  */
 11  
 package org.kuali.rice.kns.uif.widget;
 12  
 
 13  
 import java.security.GeneralSecurityException;
 14  
 import java.util.HashMap;
 15  
 import java.util.List;
 16  
 import java.util.Map;
 17  
 import java.util.Map.Entry;
 18  
 import java.util.Properties;
 19  
 
 20  
 import org.apache.commons.lang.StringUtils;
 21  
 import org.kuali.rice.core.api.services.CoreApiServiceLocator;
 22  
 import org.kuali.rice.core.web.format.Formatter;
 23  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 24  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 25  
 import org.kuali.rice.kns.uif.UifConstants;
 26  
 import org.kuali.rice.kns.uif.UifParameters;
 27  
 import org.kuali.rice.kns.uif.container.View;
 28  
 import org.kuali.rice.kns.uif.core.Component;
 29  
 import org.kuali.rice.kns.uif.field.AttributeField;
 30  
 import org.kuali.rice.kns.uif.field.LinkField;
 31  
 import org.kuali.rice.kns.uif.util.LookupInquiryUtils;
 32  
 import org.kuali.rice.kns.uif.util.ObjectPropertyUtils;
 33  
 import org.kuali.rice.kns.uif.util.ViewModelUtils;
 34  
 import org.kuali.rice.kns.util.UrlFactory;
 35  
 
 36  
 /**
 37  
  * Widget for rendering an Inquiry link on a field's value
 38  
  * 
 39  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 40  
  */
 41  
 public class Inquiry extends WidgetBase {
 42  
     private static final long serialVersionUID = -2154388007867302901L;
 43  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(Inquiry.class);
 44  
 
 45  
     public static final String INQUIRY_TITLE_PREFIX = "title.inquiry.url.value.prependtext";
 46  
 
 47  
     private String baseInquiryUrl;
 48  
 
 49  
     private String dataObjectClassName;
 50  
     private String viewName;
 51  
 
 52  
     private Map<String, String> inquiryParameters;
 53  
 
 54  
     private boolean forceInquiry;
 55  
 
 56  
     private LinkField inquiryLinkField;
 57  
 
 58  
     public Inquiry() {
 59  0
         super();
 60  
 
 61  0
         forceInquiry = false;
 62  0
         inquiryParameters = new HashMap<String, String>();
 63  0
     }
 64  
 
 65  
     /**
 66  
      * @see org.kuali.rice.kns.uif.widget.WidgetBase#performFinalize(org.kuali.rice.kns.uif.container.View,
 67  
      *      java.lang.Object, org.kuali.rice.kns.uif.core.Component)
 68  
      */
 69  
     @Override
 70  
     public void performFinalize(View view, Object model, Component parent) {
 71  0
         super.performFinalize(view, model, parent);
 72  
 
 73  
         // only set inquiry if enabled
 74  0
         if (!isRender()) {
 75  0
             return;
 76  
         }
 77  
 
 78  
         // set render to false until we find an inquiry class
 79  0
         setRender(false);
 80  
 
 81  0
         AttributeField field = (AttributeField) parent;
 82  
 
 83  
         // check if field value is null, if so no inquiry
 84  0
         Object propertyValue = ObjectPropertyUtils.getPropertyValue(model, field.getBindingInfo().getBindingPath());
 85  0
         if ((propertyValue == null) || StringUtils.isBlank(propertyValue.toString())) {
 86  0
             return;
 87  
         }
 88  
 
 89  
         // get parent object for inquiry
 90  0
         Object parentObject = ViewModelUtils.getParentObjectForMetadata(view, model, field);
 91  0
         String propertyName = field.getBindingInfo().getBindingName();
 92  
 
 93  
         // if class and parameters configured, build link from those
 94  0
         if (StringUtils.isNotBlank(dataObjectClassName) && (inquiryParameters != null) && !inquiryParameters.isEmpty()) {
 95  0
             Class<?> inquiryObjectClass = null;
 96  
             try {
 97  0
                 inquiryObjectClass = Class.forName(dataObjectClassName);
 98  
             }
 99  0
             catch (ClassNotFoundException e) {
 100  0
                 LOG.error("Unable to get class for: " + dataObjectClassName);
 101  0
                 throw new RuntimeException(e);
 102  0
             }
 103  
 
 104  0
             buildInquiryLink(parentObject, propertyName, inquiryObjectClass, inquiryParameters);
 105  0
         }
 106  
         // get inquiry class and parameters from view helper
 107  
         else {
 108  0
             view.getViewHelperService().buildInquiryLink(parentObject, propertyName, this);
 109  
         }
 110  0
     }
 111  
 
 112  
     /**
 113  
      * Builds the inquiry link based on the given inquiry class and parameters
 114  
      * 
 115  
      * @param dataObject
 116  
      *            - parent object that contains the data (used to pull inquiry
 117  
      *            parameters)
 118  
      * @param propertyName
 119  
      *            - name of the property the inquiry is set on
 120  
      * @param inquiryObjectClass
 121  
      *            - class of the object the inquiry should point to
 122  
      * @param inquiryParms
 123  
      *            - map of key field mappings for the inquiry
 124  
      */
 125  
     public void buildInquiryLink(Object dataObject, String propertyName, Class<?> inquiryObjectClass,
 126  
             Map<String, String> inquiryParms) {
 127  0
         Properties urlParameters = new Properties();
 128  
 
 129  0
         urlParameters.put(UifParameters.DATA_OBJECT_CLASS_NAME, inquiryObjectClass.getName());
 130  0
         urlParameters.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.START);
 131  
 
 132  
         // get inquiry parameter values
 133  0
         for (Entry<String, String> inquiryParameter : inquiryParms.entrySet()) {
 134  0
             String parameterName = inquiryParameter.getKey();
 135  
 
 136  0
             Object parameterValue = ObjectPropertyUtils.getPropertyValue(dataObject, parameterName);
 137  
 
 138  
             // TODO: need general format util that uses spring
 139  0
             if (parameterValue == null) {
 140  0
                 parameterValue = "";
 141  
             }
 142  0
             else if (parameterValue instanceof java.sql.Date) {
 143  0
                 if (Formatter.findFormatter(parameterValue.getClass()) != null) {
 144  0
                     Formatter formatter = Formatter.getFormatter(parameterValue.getClass());
 145  0
                     parameterValue = formatter.format(parameterValue);
 146  0
                 }
 147  
             }
 148  
             else {
 149  0
                 parameterValue = parameterValue.toString();
 150  
             }
 151  
 
 152  
             // Encrypt value if it is a field that has restriction that prevents
 153  
             // a value from being shown to user, because we don't want the
 154  
             // browser history to store the restricted
 155  
             // attribute's value in the URL
 156  0
             if (KNSServiceLocatorWeb.getBusinessObjectAuthorizationService()
 157  
                     .attributeValueNeedsToBeEncryptedOnFormsAndLinks(inquiryObjectClass, inquiryParameter.getValue())) {
 158  
                 try {
 159  0
                     parameterValue = CoreApiServiceLocator.getEncryptionService().encrypt(parameterValue);
 160  
                 }
 161  0
                 catch (GeneralSecurityException e) {
 162  0
                     LOG.error("Exception while trying to encrypted value for inquiry framework.", e);
 163  0
                     throw new RuntimeException(e);
 164  0
                 }
 165  
             }
 166  
 
 167  
             // add inquiry parameter to URL
 168  0
             urlParameters.put(inquiryParameter.getValue(), parameterValue);
 169  0
         }
 170  
 
 171  0
         String inquiryUrl = UrlFactory.parameterizeUrl(baseInquiryUrl, urlParameters);
 172  0
         inquiryLinkField.setHrefText(inquiryUrl);
 173  
 
 174  
         // get inquiry link text
 175  
         // TODO: should we really put the link label here or just wrap the
 176  
         // written value?
 177  0
         Object fieldValue = ObjectPropertyUtils.getPropertyValue(dataObject, propertyName);
 178  0
         if (fieldValue != null) {
 179  0
             inquiryLinkField.setLinkLabel(fieldValue.toString());
 180  
         }
 181  
 
 182  
         // set inquiry title
 183  0
         String linkTitle = createTitleText(inquiryObjectClass);
 184  0
         linkTitle = LookupInquiryUtils.getTitleText(linkTitle, inquiryObjectClass, inquiryParameters);
 185  0
         inquiryLinkField.setTitle(linkTitle);
 186  
 
 187  0
         setRender(true);
 188  0
     }
 189  
 
 190  
     /**
 191  
      * Gets text to prepend to the inquiry link title
 192  
      * 
 193  
      * @param dataObjectClass
 194  
      *            - data object class being inquired into
 195  
      * @return String title prepend text
 196  
      */
 197  
     public String createTitleText(Class<?> dataObjectClass) {
 198  0
         String titleText = "";
 199  
 
 200  0
         String titlePrefixProp = KNSServiceLocator.getKualiConfigurationService().getPropertyString(
 201  
                 INQUIRY_TITLE_PREFIX);
 202  0
         if (StringUtils.isNotBlank(titlePrefixProp)) {
 203  0
             titleText += titlePrefixProp + " ";
 204  
         }
 205  
 
 206  0
         String objectLabel = KNSServiceLocatorWeb.getDataDictionaryService().getDataDictionary()
 207  
                 .getDataObjectEntry(dataObjectClass.getName()).getObjectLabel();
 208  0
         if (StringUtils.isNotBlank(objectLabel)) {
 209  0
             titleText += objectLabel + " ";
 210  
         }
 211  
 
 212  0
         return titleText;
 213  
     }
 214  
     
 215  
     /**
 216  
      * @see org.kuali.rice.kns.uif.core.ComponentBase#getNestedComponents()
 217  
      */
 218  
     @Override
 219  
     public List<Component> getNestedComponents() {
 220  0
         List<Component> components = super.getNestedComponents();
 221  
 
 222  0
         components.add(inquiryLinkField);
 223  
 
 224  0
         return components;
 225  
     }
 226  
 
 227  
     public String getBaseInquiryUrl() {
 228  0
         return this.baseInquiryUrl;
 229  
     }
 230  
 
 231  
     public void setBaseInquiryUrl(String baseInquiryUrl) {
 232  0
         this.baseInquiryUrl = baseInquiryUrl;
 233  0
     }
 234  
 
 235  
     public String getDataObjectClassName() {
 236  0
         return this.dataObjectClassName;
 237  
     }
 238  
 
 239  
     public void setDataObjectClassName(String dataObjectClassName) {
 240  0
         this.dataObjectClassName = dataObjectClassName;
 241  0
     }
 242  
 
 243  
     public String getViewName() {
 244  0
         return this.viewName;
 245  
     }
 246  
 
 247  
     public void setViewName(String viewName) {
 248  0
         this.viewName = viewName;
 249  0
     }
 250  
 
 251  
     public boolean isForceInquiry() {
 252  0
         return this.forceInquiry;
 253  
     }
 254  
 
 255  
     public void setForceInquiry(boolean forceInquiry) {
 256  0
         this.forceInquiry = forceInquiry;
 257  0
     }
 258  
 
 259  
     public Map<String, String> getInquiryParameters() {
 260  0
         return this.inquiryParameters;
 261  
     }
 262  
 
 263  
     public void setInquiryParameters(Map<String, String> inquiryParameters) {
 264  0
         this.inquiryParameters = inquiryParameters;
 265  0
     }
 266  
 
 267  
     public void setInquiryParameters(String inquiryParameterString) {
 268  0
         Map<String, String> inquiryParms = new HashMap<String, String>();
 269  
 
 270  0
         String[] mappings = StringUtils.split(inquiryParameterString, ",");
 271  0
         for (int i = 0; i < mappings.length; i++) {
 272  0
             String[] mapping = StringUtils.split(mappings[i], ":");
 273  0
             inquiryParms.put(mapping[0], mapping[1]);
 274  
         }
 275  
 
 276  0
         this.inquiryParameters = inquiryParms;
 277  0
     }
 278  
 
 279  
     public LinkField getInquiryLinkField() {
 280  0
         return this.inquiryLinkField;
 281  
     }
 282  
 
 283  
     public void setInquiryLinkField(LinkField inquiryLinkField) {
 284  0
         this.inquiryLinkField = inquiryLinkField;
 285  0
     }
 286  
 
 287  
 }