001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.uif.widget;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.krad.uif.UifConstants;
020    import org.kuali.rice.krad.uif.UifParameters;
021    import org.kuali.rice.krad.uif.field.InputField;
022    import org.kuali.rice.krad.uif.view.View;
023    import org.kuali.rice.krad.uif.component.BindingInfo;
024    import org.kuali.rice.krad.uif.component.Component;
025    import org.kuali.rice.krad.uif.field.ActionField;
026    import org.kuali.rice.krad.util.UrlFactory;
027    
028    import java.util.List;
029    import java.util.Map;
030    import java.util.Map.Entry;
031    import java.util.Properties;
032    
033    /**
034     * Widget for rendering an Direct Inquiry link icon next to a input field
035     * 
036     * @author Kuali Rice Team (rice.collab@kuali.org)
037     */
038    public class DirectInquiry extends Inquiry {
039        private static final long serialVersionUID = -2490979579285984314L;
040        
041        private ActionField directInquiryActionField;
042    
043        private boolean adjustInquiryParameters;
044        private BindingInfo fieldBindingInfo;
045    
046        public DirectInquiry() {
047            super();
048    
049            adjustInquiryParameters = false;
050        }
051    
052        /**
053         * @see org.kuali.rice.krad.uif.widget.WidgetBase#performFinalize(org.kuali.rice.krad.uif.view.View,
054         *      java.lang.Object, org.kuali.rice.krad.uif.component.Component)
055         */
056        @Override
057        public void performFinalize(View view, Object model, Component parent) {
058            super.performFinalize(view, model, parent);
059    
060            // only set inquiry if enabled
061            if (!isRender() || isReadOnly()) {
062                return;
063            }
064    
065            // set render to false until we find an inquiry class
066            setRender(false);
067    
068            InputField field = (InputField) parent;
069    
070            // determine whether inquiry parameters will need adjusted
071            if (StringUtils.isBlank(getDataObjectClassName())
072                    || (getInquiryParameters() == null)
073                    || getInquiryParameters().isEmpty()) {
074                // if inquiry parameters not given, they will not be adjusted by super
075                adjustInquiryParameters = true;
076                fieldBindingInfo = field.getBindingInfo();
077            }
078    
079            setupLink(view, model, field);
080        }
081    
082        /**
083         * Builds the inquiry link and onclick script based on the given inquiry class and parameters
084         * 
085         * @param dataObject
086         *            - parent object that contains the data (used to pull inquiry
087         *            parameters)
088         * @param propertyName
089         *            - name of the property the inquiry is set on
090         * @param inquiryObjectClass
091         *            - class of the object the inquiry should point to
092         * @param inquiryParms
093         *            - map of key field mappings for the inquiry
094         */
095            public void buildInquiryLink(Object dataObject, String propertyName,
096                            Class<?> inquiryObjectClass, Map<String, String> inquiryParms) {
097                    Properties urlParameters = new Properties();
098    
099                    urlParameters.put(UifParameters.DATA_OBJECT_CLASS_NAME,
100                                    inquiryObjectClass.getName());
101                    urlParameters.put(UifParameters.METHOD_TO_CALL,
102                                    UifConstants.MethodToCallNames.START);
103    
104                    // Direct inquiry
105                    String inquiryUrl = UrlFactory.parameterizeUrl(getBaseInquiryUrl(),
106                                    urlParameters);
107                    StringBuilder paramMapString = new StringBuilder();
108    
109                    // Check if lightbox is set. Get lightbox options.
110                    String lightBoxOptions = "";
111                    boolean lightBoxShow = directInquiryActionField.getLightBoxDirectInquiry() != null;
112                    if (lightBoxShow) {
113                            lightBoxOptions = directInquiryActionField.getLightBoxDirectInquiry()
114                                            .getComponentOptionsJSString();
115                    }
116    
117                    // Build parameter string using the actual names of the fields as on the
118                    // html page
119            for (Entry<String, String> inquiryParameter : inquiryParms.entrySet()) {
120                String inquiryParameterFrom = inquiryParameter.getKey();
121                if (adjustInquiryParameters && (fieldBindingInfo != null)) {
122                    inquiryParameterFrom = fieldBindingInfo.getPropertyAdjustedBindingPath(inquiryParameterFrom);
123                }
124                paramMapString.append(inquiryParameterFrom);
125                paramMapString.append(":");
126                paramMapString.append(inquiryParameter.getValue());
127                paramMapString.append(",");
128            }
129                    paramMapString.deleteCharAt(paramMapString.length() - 1);
130    
131                    // Create onlick script to open the inquiry window on the click event
132                    // of the direct inquiry
133                    StringBuilder onClickScript = new StringBuilder("showDirectInquiry(\"");
134                    onClickScript.append(inquiryUrl);
135                    onClickScript.append("\", \"");
136                    onClickScript.append(paramMapString);
137                    onClickScript.append("\", ");
138                    onClickScript.append(lightBoxShow);
139                    onClickScript.append(", ");
140                    onClickScript.append(lightBoxOptions);
141                    onClickScript.append(");");
142    
143                    directInquiryActionField.setBlockValidateDirty(true);
144                    directInquiryActionField.setClientSideJs(onClickScript.toString());
145    
146                    setRender(true);
147            }
148    
149        /**
150         * @see org.kuali.rice.krad.uif.component.ComponentBase#getComponentsForLifecycle()
151         */
152        @Override
153        public List<Component> getComponentsForLifecycle() {
154            List<Component> components = super.getComponentsForLifecycle();
155    
156            components.add(directInquiryActionField);
157    
158            return components;
159        }   
160            
161            /**
162             * @return the directInquiryActionField
163             */
164            public ActionField getDirectInquiryActionField() {
165                    return this.directInquiryActionField;
166            }
167    
168            /**
169             * @param directInquiryActionField the directInquiryActionField to set
170             */
171            public void setDirectInquiryActionField(ActionField directInquiryActionField) {
172                    this.directInquiryActionField = directInquiryActionField;
173            }
174    
175    }