001    /**
002     * Copyright 2005-2014 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.kim.web.struts.action;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.apache.log4j.Logger;
020    import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
021    import org.kuali.rice.core.api.util.RiceKeyConstants;
022    import org.kuali.rice.kim.api.identity.principal.Principal;
023    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
024    import org.kuali.rice.kim.api.type.KimAttributeField;
025    import org.kuali.rice.kim.api.type.KimType;
026    import org.kuali.rice.kim.bo.ui.KimDocumentRoleMember;
027    import org.kuali.rice.kim.bo.ui.KimDocumentRoleQualifier;
028    import org.kuali.rice.kim.bo.ui.PersonDocumentRole;
029    import org.kuali.rice.kim.document.IdentityManagementPersonDocument;
030    import org.kuali.rice.kim.framework.type.KimTypeService;
031    import org.kuali.rice.kim.impl.KIMPropertyConstants;
032    import org.kuali.rice.kim.impl.type.KimTypeBo;
033    import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
034    import org.kuali.rice.kim.web.struts.form.IdentityManagementDocumentFormBase;
035    import org.kuali.rice.kim.web.struts.form.IdentityManagementPersonDocumentForm;
036    import org.kuali.rice.krad.util.GlobalVariables;
037    import org.kuali.rice.krad.util.KRADConstants;
038    
039    import javax.servlet.http.HttpServletRequest;
040    import javax.xml.namespace.QName;
041    
042    /**
043     * This is a description of what this class does - jonathan don't forget to fill this in. 
044     * 
045     * @author Kuali Rice Team (rice.collab@kuali.org)
046     *
047     */
048    public class IdentityManagementPersonInquiry extends IdentityManagementBaseInquiryAction {
049        private static final Logger LOG = Logger.getLogger(IdentityManagementPersonInquiry.class);  
050            /**
051             * This overridden method ...
052             * 
053             * @see org.kuali.rice.kim.web.struts.action.IdentityManagementBaseInquiryAction#loadKimObject(javax.servlet.http.HttpServletRequest, org.kuali.rice.kim.web.struts.form.IdentityManagementDocumentFormBase)
054             */
055            @Override
056            protected void loadKimObject(HttpServletRequest request,
057                            IdentityManagementDocumentFormBase form) {
058            IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
059            String principalId = request.getParameter(KIMPropertyConstants.Person.PRINCIPAL_ID);
060            String principalName = request.getParameter(KIMPropertyConstants.Person.PRINCIPAL_NAME);
061            if ( StringUtils.isBlank(principalId) && StringUtils.isNotBlank(principalName) ) {
062                    Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName);
063                    if ( principal != null ) {
064                            principalId = principal.getPrincipalId();
065                    }
066            }
067            if ( principalId != null ) {
068                    Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
069                    if (principal != null) {
070                            personDocumentForm.setPrincipalId(principalId);
071                            getUiDocumentService().loadEntityToPersonDoc(personDocumentForm.getPersonDocument(), personDocumentForm.getPrincipalId() );
072                        personDocumentForm.setCanOverrideEntityPrivacyPreferences(getUiDocumentService().canOverrideEntityPrivacyPreferences(GlobalVariables.getUserSession().getPrincipalId(), personDocumentForm.getPrincipalId()));
073                            populateRoleInformation( personDocumentForm.getPersonDocument() );
074                    } else {
075                            LOG.error("No records found for Person Inquiry.");
076                    GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_INQUIRY);
077                    }
078            }
079            }
080    
081            protected void populateRoleInformation( IdentityManagementPersonDocument personDoc ) {
082                    for (PersonDocumentRole role : personDoc.getRoles()) {
083                    KimTypeService kimTypeService = (KimTypeService) GlobalResourceLoader.getService(QName.valueOf(
084                        getKimTypeServiceName(KimTypeBo.to(role.getKimRoleType()))));
085                    //it is possible that the the kimTypeService is coming from a remote application 
086                    // and therefore it can't be guarenteed that it is up and working, so using a try/catch to catch this possibility.
087                    try {
088                        role.setDefinitions(kimTypeService.getAttributeDefinitions(role.getKimTypeId()));
089                    } catch (Exception ex) {
090                    LOG.warn("Not able to retrieve KimTypeService from remote system for KIM Type Id: " + role.getKimTypeId(), ex);
091                }
092                    // when post again, it will need this during populate
093                role.setNewRolePrncpl(new KimDocumentRoleMember());
094                for (KimAttributeField key : role.getDefinitions()) {
095                    KimDocumentRoleQualifier qualifier = new KimDocumentRoleQualifier();
096                    //qualifier.setQualifierKey(key);
097                            setAttrDefnIdForQualifier(qualifier, key);
098                    role.getNewRolePrncpl().getQualifiers().add(qualifier);
099                }
100                    role.setAttributeEntry( getUiDocumentService().getAttributeEntries( role.getDefinitions() ) );
101                    }
102            }
103            
104        private void setAttrDefnIdForQualifier(KimDocumentRoleQualifier qualifier,KimAttributeField definition) {
105                    qualifier.setKimAttrDefnId(definition.getId());
106                    qualifier.refreshReferenceObject("kimAttribute");
107        }
108            private String getKimTypeServiceName (KimType kimType) {
109            String serviceName = kimType.getServiceName();
110            if (StringUtils.isBlank(serviceName)) {
111                    serviceName = "kimTypeService";
112            }
113            return serviceName;
114    
115            }
116            
117    }