001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.krad.uif.control;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.kim.api.identity.Person;
020import org.kuali.rice.kim.api.identity.PersonService;
021import org.kuali.rice.kim.api.services.KimApiServiceLocator;
022import org.kuali.rice.krad.uif.field.InputField;
023import org.kuali.rice.krad.uif.view.View;
024import org.kuali.rice.krad.uif.component.Component;
025import org.kuali.rice.krad.uif.component.MethodInvokerConfig;
026import org.kuali.rice.krad.uif.field.AttributeQuery;
027import org.kuali.rice.krad.uif.widget.QuickFinder;
028
029/**
030 * Represents a user control, which is a special control to handle
031 * the input of a Person
032 *
033 * @author Kuali Rice Team (rice.collab@kuali.org)
034 */
035public class UserControl extends TextControl {
036    private static final long serialVersionUID = 7468340793076585869L;
037
038    private String principalIdPropertyName;
039    private String personNamePropertyName;
040    private String personObjectPropertyName;
041
042    public UserControl() {
043        super();
044    }
045
046    @Override
047    public void performApplyModel(View view, Object model, Component parent) {
048        super.performApplyModel(view, model, parent);
049
050        if (!(parent instanceof InputField)) {
051            return;
052        }
053
054        InputField field = (InputField) parent;
055        field.getHiddenPropertyNames().add(principalIdPropertyName);
056
057        if (!field.isReadOnly()) {
058            // add information fields
059            if (StringUtils.isNotBlank(personNamePropertyName)) {
060                field.getInformationalDisplayPropertyNames().add(personNamePropertyName);
061            } else {
062                field.getInformationalDisplayPropertyNames().add(personObjectPropertyName + ".name");
063            }
064
065            // setup script to clear id field when name is modified
066            String idPropertyPath = field.getBindingInfo().getPropertyAdjustedBindingPath(principalIdPropertyName);
067            String onChangeScript = "setValue('" + idPropertyPath + "','');";
068
069            if (StringUtils.isNotBlank(field.getOnChangeScript())) {
070                onChangeScript = field.getOnChangeScript() + onChangeScript;
071            }
072            field.setOnChangeScript(onChangeScript);
073        }
074
075        if (field.isReadOnly() && StringUtils.isBlank(field.getAdditionalDisplayPropertyName())) {
076            if (StringUtils.isNotBlank(personNamePropertyName)) {
077                field.setAdditionalDisplayPropertyName(personNamePropertyName);
078            } else {
079                field.setAdditionalDisplayPropertyName(personObjectPropertyName + ".name");
080            }
081        }
082
083        // setup field query for displaying name
084        AttributeQuery attributeQuery = new AttributeQuery();
085        MethodInvokerConfig methodInvokerConfig = new MethodInvokerConfig();
086        PersonService personService = KimApiServiceLocator.getPersonService();
087        methodInvokerConfig.setTargetObject(personService);
088        attributeQuery.setQueryMethodInvokerConfig(methodInvokerConfig);
089        attributeQuery.setQueryMethodToCall("getPersonByPrincipalName");
090        attributeQuery.getQueryMethodArgumentFieldList().add(field.getPropertyName());
091        attributeQuery.getReturnFieldMapping().put("principalId", principalIdPropertyName);
092
093        if (StringUtils.isNotBlank(personNamePropertyName)) {
094            attributeQuery.getReturnFieldMapping().put("name", personNamePropertyName);
095        } else {
096            attributeQuery.getReturnFieldMapping().put("name", personObjectPropertyName + ".name");
097        }
098        field.setFieldAttributeQuery(attributeQuery);
099
100        // setup field lookup
101        QuickFinder quickFinder = field.getFieldLookup();
102        if (quickFinder.isRender()) {
103            if (StringUtils.isBlank(quickFinder.getDataObjectClassName())) {
104                quickFinder.setDataObjectClassName(Person.class.getName());
105            }
106
107            if (quickFinder.getFieldConversions().isEmpty()) {
108                quickFinder.getFieldConversions().put("principalId", principalIdPropertyName);
109
110                if (StringUtils.isNotBlank(personNamePropertyName)) {
111                    quickFinder.getFieldConversions().put("name", personNamePropertyName);
112                } else {
113                    quickFinder.getFieldConversions().put("name", personObjectPropertyName + ".name");
114                }
115
116                quickFinder.getFieldConversions().put("principalName", field.getPropertyName());
117            }
118        }
119    }
120
121    /**
122     * The name of the property on the parent object that holds the principal id
123     *
124     * @return String principalIdPropertyName
125     */
126    public String getPrincipalIdPropertyName() {
127        return principalIdPropertyName;
128    }
129
130    /**
131     * Setter for the name of the property on the parent object that holds the principal id
132     *
133     * @param principalIdPropertyName
134     */
135    public void setPrincipalIdPropertyName(String principalIdPropertyName) {
136        this.principalIdPropertyName = principalIdPropertyName;
137    }
138
139    /**
140     * The name of the property on the parent object that holds the person name
141     *
142     * @return String personNamePropertyName
143     */
144    public String getPersonNamePropertyName() {
145        return personNamePropertyName;
146    }
147
148    /**
149     * Setter for the name of the property on the parent object that holds the person name
150     *
151     * @param personNamePropertyName
152     */
153    public void setPersonNamePropertyName(String personNamePropertyName) {
154        this.personNamePropertyName = personNamePropertyName;
155    }
156
157    /**
158     * The name of the property on the parent object that holds the person object
159     *
160     * @return String personObjectPropertyName
161     */
162    public String getPersonObjectPropertyName() {
163        return personObjectPropertyName;
164    }
165
166    /**
167     * Setter for the name of the property on the parent object that holds the person object
168     *
169     * @param personObjectPropertyName
170     */
171    public void setPersonObjectPropertyName(String personObjectPropertyName) {
172        this.personObjectPropertyName = personObjectPropertyName;
173    }
174}