View Javadoc

1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.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/ecl2.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.kim.web.struts.action;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21  import org.kuali.rice.core.api.util.RiceKeyConstants;
22  import org.kuali.rice.kim.api.identity.principal.Principal;
23  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
24  import org.kuali.rice.kim.api.type.KimAttributeField;
25  import org.kuali.rice.kim.api.type.KimType;
26  import org.kuali.rice.kim.bo.ui.KimDocumentRoleMember;
27  import org.kuali.rice.kim.bo.ui.KimDocumentRoleQualifier;
28  import org.kuali.rice.kim.bo.ui.PersonDocumentRole;
29  import org.kuali.rice.kim.document.IdentityManagementPersonDocument;
30  import org.kuali.rice.kim.framework.type.KimTypeService;
31  import org.kuali.rice.kim.impl.KIMPropertyConstants;
32  import org.kuali.rice.kim.impl.type.KimTypeBo;
33  import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
34  import org.kuali.rice.kim.web.struts.form.IdentityManagementDocumentFormBase;
35  import org.kuali.rice.kim.web.struts.form.IdentityManagementPersonDocumentForm;
36  import org.kuali.rice.krad.util.GlobalVariables;
37  import org.kuali.rice.krad.util.KRADConstants;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.xml.namespace.QName;
41  
42  /**
43   * This is a description of what this class does - jonathan don't forget to fill this in. 
44   * 
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   *
47   */
48  public class IdentityManagementPersonInquiry extends IdentityManagementBaseInquiryAction {
49      private static final Logger LOG = Logger.getLogger(IdentityManagementPersonInquiry.class);	
50  	/**
51  	 * This overridden method ...
52  	 * 
53  	 * @see org.kuali.rice.kim.web.struts.action.IdentityManagementBaseInquiryAction#loadKimObject(javax.servlet.http.HttpServletRequest, org.kuali.rice.kim.web.struts.form.IdentityManagementDocumentFormBase)
54  	 */
55  	@Override
56  	protected void loadKimObject(HttpServletRequest request,
57  			IdentityManagementDocumentFormBase form) {
58          IdentityManagementPersonDocumentForm personDocumentForm = (IdentityManagementPersonDocumentForm) form;
59          String principalId = request.getParameter(KIMPropertyConstants.Person.PRINCIPAL_ID);
60          String principalName = request.getParameter(KIMPropertyConstants.Person.PRINCIPAL_NAME);
61          if ( StringUtils.isBlank(principalId) && StringUtils.isNotBlank(principalName) ) {
62          	Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName);
63          	if ( principal != null ) {
64          		principalId = principal.getPrincipalId();
65          	}
66          }
67          if ( principalId != null ) {
68          	Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
69          	if (principal != null) {
70  	        	personDocumentForm.setPrincipalId(principalId);
71  	        	getUiDocumentService().loadEntityToPersonDoc(personDocumentForm.getPersonDocument(), personDocumentForm.getPrincipalId() );
72  	            personDocumentForm.setCanOverrideEntityPrivacyPreferences(getUiDocumentService().canOverrideEntityPrivacyPreferences(GlobalVariables.getUserSession().getPrincipalId(), personDocumentForm.getPrincipalId()));
73  	        	populateRoleInformation( personDocumentForm.getPersonDocument() );
74          	} else {
75          		LOG.error("No records found for Person Inquiry.");
76                  GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_INQUIRY);
77          	}
78          }
79  	}
80  
81  	protected void populateRoleInformation( IdentityManagementPersonDocument personDoc ) {
82  		for (PersonDocumentRole role : personDoc.getRoles()) {
83  	        KimTypeService kimTypeService = (KimTypeService) GlobalResourceLoader.getService(QName.valueOf(
84                      getKimTypeServiceName(KimTypeBo.to(role.getKimRoleType()))));
85  	        //it is possible that the the kimTypeService is coming from a remote application 
86  	        // and therefore it can't be guarenteed that it is up and working, so using a try/catch to catch this possibility.
87  	        try {
88  	            role.setDefinitions(kimTypeService.getAttributeDefinitions(role.getKimTypeId()));
89  	        } catch (Exception ex) {
90                  LOG.warn("Not able to retrieve KimTypeService from remote system for KIM Type Id: " + role.getKimTypeId(), ex);
91              }
92          	// when post again, it will need this during populate
93              role.setNewRolePrncpl(new KimDocumentRoleMember());
94              for (KimAttributeField key : role.getDefinitions()) {
95              	KimDocumentRoleQualifier qualifier = new KimDocumentRoleQualifier();
96              	//qualifier.setQualifierKey(key);
97  	        	setAttrDefnIdForQualifier(qualifier, key);
98              	role.getNewRolePrncpl().getQualifiers().add(qualifier);
99              }
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 }