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