View Javadoc

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