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