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.web.struts.action;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.log4j.Logger;
020import org.apache.struts.action.ActionForm;
021import org.apache.struts.action.ActionForward;
022import org.apache.struts.action.ActionMapping;
023import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
024import org.kuali.rice.core.api.util.RiceConstants;
025import org.kuali.rice.core.api.util.RiceKeyConstants;
026import org.kuali.rice.kim.api.identity.IdentityService;
027import org.kuali.rice.kim.api.responsibility.ResponsibilityService;
028import org.kuali.rice.kim.api.role.Role;
029import org.kuali.rice.kim.api.services.KimApiServiceLocator;
030import org.kuali.rice.kim.api.type.KimType;
031import org.kuali.rice.kim.framework.role.RoleTypeService;
032import org.kuali.rice.kim.framework.type.KimTypeService;
033import org.kuali.rice.kim.impl.type.KimTypeLookupableHelperServiceImpl;
034import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
035import org.kuali.rice.kim.service.UiDocumentService;
036import org.kuali.rice.kim.util.KimCommonUtilsInternal;
037import org.kuali.rice.kim.web.struts.form.IdentityManagementDocumentFormBase;
038import org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase;
039import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
040import org.kuali.rice.kns.web.struts.form.KualiTableRenderFormMetadata;
041import org.kuali.rice.krad.util.GlobalVariables;
042import org.kuali.rice.krad.util.KRADConstants;
043
044import javax.servlet.http.HttpServletRequest;
045import javax.servlet.http.HttpServletResponse;
046import javax.xml.namespace.QName;
047
048/**
049 * 
050 * @author Kuali Rice Team (rice.collab@kuali.org)
051 *
052 */
053abstract public class IdentityManagementDocumentActionBase extends KualiTransactionalDocumentActionBase {
054
055    private static final Logger LOG = Logger.getLogger( IdentityManagementDocumentActionBase.class );
056
057        protected static final String CHANGE_MEMBER_TYPE_CODE_METHOD_TO_CALL = "changeMemberTypeCode";
058        protected static final String CHANGE_NAMESPACE_METHOD_TO_CALL = "changeNamespace";
059
060        protected IdentityService identityService;
061        protected ResponsibilityService responsibilityService;
062        protected UiDocumentService uiDocumentService;
063                
064        @Override
065        public ActionForward performLookup(ActionMapping mapping, ActionForm form,
066                        HttpServletRequest request, HttpServletResponse response)
067                        throws Exception {
068                ActionForward forward =  super.performLookup(mapping, form, request, response);
069                String path = forward.getPath();
070                //Making the hack look cleaner!
071                forward.setPath(KimCommonUtilsInternal.getPathWithKimContext(path, getActionName()));
072                return forward;
073        }
074
075        protected abstract String getActionName();
076        
077    protected IdentityService getIdentityService() {
078        if ( identityService == null ) {
079                identityService = KimApiServiceLocator.getIdentityService();
080        }
081                return identityService;
082        }
083
084    protected ResponsibilityService getResponsibilityService() {
085        if ( responsibilityService == null ) {
086                responsibilityService = KimApiServiceLocator.getResponsibilityService();
087        }
088                return responsibilityService;
089        }
090
091    protected UiDocumentService getUiDocumentService() {
092                if ( uiDocumentService == null ) {
093                        uiDocumentService = KIMServiceLocatorInternal.getUiDocumentService();
094                }
095                return uiDocumentService;
096        }
097
098        @Override
099    protected String getReturnLocation(HttpServletRequest request, ActionMapping mapping){
100        String returnLocation = super.getReturnLocation(request, mapping);
101        return KimCommonUtilsInternal.getPathWithKimContext(returnLocation, getActionName());
102    }
103
104        @Override
105    protected ActionForward returnToSender(HttpServletRequest request, ActionMapping mapping, KualiDocumentFormBase form) {
106        ActionForward dest;
107        if (form.isReturnToActionList()) {
108            String workflowBase = getKualiConfigurationService().getPropertyValueAsString(
109                    KRADConstants.WORKFLOW_URL_KEY);
110            String actionListUrl = workflowBase + "/ActionList.do";
111
112            dest = new ActionForward(actionListUrl, true);
113        } else if (StringUtils.isNotBlank(form.getBackLocation())){
114                dest = new ActionForward(form.getBackLocation(), true);
115        } else {
116                dest = mapping.findForward(KRADConstants.MAPPING_PORTAL);
117            ActionForward newDest = new ActionForward();
118            //why is this being done?
119            KimCommonUtilsInternal.copyProperties(newDest, dest);
120            newDest.setPath(getApplicationBaseUrl());
121            return newDest;
122        }
123
124        setupDocumentExit();
125        return dest;
126    }    
127
128        protected ActionForward getBasePathForward(HttpServletRequest request, ActionForward forward){
129                ActionForward newDest = new ActionForward();
130        KimCommonUtilsInternal.copyProperties(newDest, forward);
131        newDest.setPath(getApplicationBaseUrl());
132        return newDest;
133    }
134
135    public ActionForward switchToPage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
136        IdentityManagementDocumentFormBase idmForm = (IdentityManagementDocumentFormBase) form;
137        
138        KualiTableRenderFormMetadata memberTableMetadata = idmForm.getMemberTableMetadata();
139        memberTableMetadata.jumpToPage(memberTableMetadata.getSwitchToPageNumber(), idmForm.getMemberRows().size(), idmForm.getRecordsPerPage());
140        memberTableMetadata.setColumnToSortIndex(memberTableMetadata.getPreviouslySortedColumnIndex());
141        return mapping.findForward(RiceConstants.MAPPING_BASIC);
142    }
143
144    protected void applyPagingAndSortingFromPreviousPageView(IdentityManagementDocumentFormBase idmForm) {
145        KualiTableRenderFormMetadata memberTableMetadata = idmForm.getMemberTableMetadata();
146
147        memberTableMetadata.jumpToPage(memberTableMetadata.getViewedPageNumber(), idmForm.getMemberRows().size(), idmForm.getRecordsPerPage());
148    }
149
150    protected boolean validateRole( String roleId, Role role, String propertyName, String message){
151        if ( role == null ) {
152                GlobalVariables.getMessageMap().putError(propertyName, RiceKeyConstants.ERROR_INVALID_ROLE, roleId );
153                return false;
154        }
155        KimType typeInfo = KimApiServiceLocator.getKimTypeInfoService().getKimType(role.getKimTypeId());
156        
157        if(KimTypeLookupableHelperServiceImpl.hasDerivedRoleTypeService(typeInfo)){
158                GlobalVariables.getMessageMap().putError(propertyName, RiceKeyConstants.ERROR_CANT_ADD_DERIVED_ROLE, message);
159                return false;
160        }
161        return true;
162    }
163 
164    public ActionForward changeNamespace(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
165        return refresh(mapping, form, request, response);
166    }
167
168    protected KimTypeService getKimTypeService( KimType typeInfo ) {
169                String serviceName = typeInfo.getServiceName();
170                if ( StringUtils.isNotBlank(serviceName) ) {
171                        try {
172                                KimTypeService service = (KimTypeService) GlobalResourceLoader.getService(QName.valueOf(serviceName));
173                                if ( service != null && service instanceof RoleTypeService) {
174                                        return service;
175                                } else {
176                                        return (RoleTypeService) KIMServiceLocatorInternal.getService("kimNoMembersRoleTypeService");
177                                }
178                        } catch ( Exception ex ) {
179                                LOG.error( "Unable to find role type service with name: " + serviceName, ex );
180                                return (RoleTypeService) KIMServiceLocatorInternal.getService("kimNoMembersRoleTypeService");
181                        }
182                }
183                return null;
184    }
185
186}