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.kim.lookup;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.kew.api.KewApiConstants;
020import org.kuali.rice.kim.api.KimConstants;
021import org.kuali.rice.kim.api.permission.PermissionService;
022import org.kuali.rice.kim.api.services.KimApiServiceLocator;
023import org.kuali.rice.kim.util.KimCommonUtilsInternal;
024import org.kuali.rice.kns.lookup.KualiLookupableImpl;
025import org.kuali.rice.krad.util.GlobalVariables;
026import org.kuali.rice.krad.util.KRADConstants;
027import org.kuali.rice.krad.util.UrlFactory;
028
029import java.util.Collections;
030import java.util.Properties;
031
032/**
033 * This is a description of what this class does - shyu don't forget to fill this in. 
034 * 
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 *
037 */
038public class PersonLookupableImpl extends KualiLookupableImpl {
039
040        private static final long serialVersionUID = 1707861010746829601L;
041
042        @Override
043        public String getCreateNewUrl() {
044                String url = "";
045                if((getLookupableHelperService()).allowsNewOrCopyAction(KimConstants.KimUIConstants.KIM_PERSON_DOCUMENT_TYPE_NAME)
046                                && canModifyEntity(GlobalVariables.getUserSession().getPrincipalId(), null)){
047                Properties parameters = new Properties();
048                parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.DOC_HANDLER_METHOD);
049                parameters.put(KRADConstants.PARAMETER_COMMAND, KewApiConstants.INITIATE_COMMAND);
050                parameters.put(KRADConstants.DOCUMENT_TYPE_NAME, KimConstants.KimUIConstants.KIM_PERSON_DOCUMENT_TYPE_NAME);
051                if (StringUtils.isNotBlank(getReturnLocation())) {
052                        parameters.put(KRADConstants.RETURN_LOCATION_PARAMETER, getReturnLocation());
053                        }
054                url = getCreateNewUrl(UrlFactory.parameterizeUrl(
055                                KimCommonUtilsInternal.getKimBasePath()+ KimConstants.KimUIConstants.KIM_PERSON_DOCUMENT_ACTION, parameters));
056                //String url = "lookup.do?businessObjectClassName=org.kuali.rice.kim.bo.types.impl.KimTypeImpl&returnLocation=portal.do&docFormKey="+KimApiConstants.KimUIConstants.KIM_ROLE_DOCUMENT_SHORT_KEY;
057                //url = "../kim/identityManagementPersonDocument.do?methodToCall=docHandler&command=initiate&docTypeName=IdentityManagementPersonDocument";
058                //url = "<a title=\"Create a new record\" href=\"" + url + "\"><img src=\"images/tinybutton-createnew.gif\" alt=\"create new\" width=\"70\" height=\"15\"/></a>";
059                }
060                return url;
061        }
062
063
064    // TODO: this is a copy from the UIDocumentService.  They should be combined outside of UIDocumentService at some point
065    // as to not have duplicate code
066    // we can't have the UIDocumentService here, because that is not available when in Kim embedded mode
067    private boolean canModifyEntity( String currentUserPrincipalId, String toModifyPrincipalId ){
068        return (StringUtils.isNotBlank(currentUserPrincipalId) && StringUtils.isNotBlank(toModifyPrincipalId) &&
069                currentUserPrincipalId.equals(toModifyPrincipalId)) ||
070                KimApiServiceLocator.getPermissionService().isAuthorized(
071                        currentUserPrincipalId,
072                        KimConstants.NAMESPACE_CODE,
073                        KimConstants.PermissionNames.MODIFY_ENTITY,
074                        Collections.singletonMap(KimConstants.AttributeConstants.PRINCIPAL_ID, currentUserPrincipalId));
075    }
076
077}