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.impl.responsibility;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.commons.beanutils.PropertyUtils;
025import org.kuali.rice.core.api.criteria.QueryByCriteria;
026import org.kuali.rice.coreservice.impl.namespace.NamespaceBo;
027import org.kuali.rice.kim.api.KimConstants;
028import org.kuali.rice.kim.api.role.Role;
029import org.kuali.rice.kim.api.role.RoleService;
030import org.kuali.rice.kim.api.services.KimApiServiceLocator;
031import org.kuali.rice.kim.impl.role.RoleBo;
032import org.kuali.rice.kim.impl.role.RoleResponsibilityBo;
033import org.kuali.rice.kim.inquiry.KimInquirableImpl;
034import org.kuali.rice.kim.lookup.RoleLookupableHelperServiceImpl;
035import org.kuali.rice.kns.lookup.HtmlData;
036import org.kuali.rice.krad.bo.BusinessObject;
037import org.kuali.rice.krad.data.DataObjectWrapper;
038import org.kuali.rice.krad.data.KradDataServiceLocator;
039import org.kuali.rice.krad.uif.widget.Inquiry;
040
041@SuppressWarnings({"unchecked", "deprecation"})
042public class ResponsibilityInquirableImpl extends KimInquirableImpl {
043        private static final long serialVersionUID = 1L;
044        
045        @Override
046        public void buildInquirableLink(Object dataObject, String propertyName, Inquiry inquiry){
047
048                if(NAME.equals(propertyName) || NAME_TO_DISPLAY.equals(propertyName)){
049                        Map<String, String> primaryKeys = new HashMap<String, String>();
050                        primaryKeys.put(ID, ID);
051                        inquiry.buildInquiryLink(dataObject, propertyName, UberResponsibilityBo.class, primaryKeys);
052        } else if(NAMESPACE_CODE.equals(propertyName) || TEMPLATE_NAMESPACE_CODE.equals(propertyName)){
053                Map<String, String> primaryKeys = new HashMap<String, String>();
054                        primaryKeys.put(propertyName, "code");
055                        inquiry.buildInquiryLink(dataObject, propertyName, NamespaceBo.class, primaryKeys);
056        } else if(DETAIL_OBJECTS.equals(propertyName)){
057                super.buildInquirableLink(dataObject, propertyName, inquiry);
058        } else if(ASSIGNED_TO_ROLES.equals(propertyName)){
059                super.buildInquirableLink(dataObject, propertyName, inquiry);
060        }else{
061                super.buildInquirableLink(dataObject, propertyName, inquiry);
062        }
063        }
064
065    @Override
066    public HtmlData getInquiryUrl(BusinessObject businessObject, String attributeName, boolean forceInquiry) {
067        /*
068         *  - responsibility detail values (attribute name and value separated by colon, commas between attributes)
069                 *      - required role qualifiers (attribute name and value separated by colon, commas between attributes)
070                 *      - list of roles assigned: role type, role namespace, role name
071         */
072        if(NAME.equals(attributeName) || NAME_TO_DISPLAY.equals(attributeName)){
073                        return getInquiryUrlForPrimaryKeys(UberResponsibilityBo.class, businessObject, Collections.singletonList(ID), null);
074        } else if(NAMESPACE_CODE.equals(attributeName) || TEMPLATE_NAMESPACE_CODE.equals(attributeName)){
075            String code = (String) KradDataServiceLocator.getDataObjectService().wrap(businessObject).getPropertyValueNullSafe(attributeName);
076            NamespaceBo parameterNamespace = new NamespaceBo();
077                        parameterNamespace.setCode(code);
078                        return getInquiryUrlForPrimaryKeys(NamespaceBo.class, parameterNamespace, Collections.singletonList("code"), null);
079        } else if(DETAIL_OBJECTS.equals(attributeName)){
080                //return getAttributesInquiryUrl(businessObject, DETAIL_OBJECTS);
081        } else if(ASSIGNED_TO_ROLES.equals(attributeName)){
082                return getAssignedRoleInquiryUrl(businessObject);
083        }
084
085        return super.getInquiryUrl(businessObject, attributeName, forceInquiry);
086    }
087
088        protected HtmlData getAttributesInquiryUrl(BusinessObject businessObject, String attributeName){
089        DataObjectWrapper<BusinessObject> wrapper = KradDataServiceLocator.getDataObjectService().wrap(businessObject);
090        List<ResponsibilityAttributeBo> responsibilityAttributeData =
091                (List<ResponsibilityAttributeBo>) wrapper.getPropertyValueNullSafe(attributeName);
092        List<HtmlData.AnchorHtmlData> htmlData = new ArrayList<HtmlData.AnchorHtmlData>();
093                List<String> primaryKeys = new ArrayList<String>();
094                primaryKeys.add(ATTRIBUTE_DATA_ID);
095        for(ResponsibilityAttributeBo responsibilityAttributeDataImpl: responsibilityAttributeData){
096                htmlData.add(getInquiryUrlForPrimaryKeys(ResponsibilityAttributeBo.class, responsibilityAttributeDataImpl, primaryKeys,
097                        getKimAttributeLabelFromDD(responsibilityAttributeDataImpl.getKimAttribute().getAttributeName())+ KimConstants.KimUIConstants.NAME_VALUE_SEPARATOR+
098                        responsibilityAttributeDataImpl.getAttributeValue()));
099        }
100        return new HtmlData.MultipleAnchorHtmlData(htmlData);
101    }
102
103        protected HtmlData getAssignedRoleInquiryUrl(BusinessObject businessObject){
104        UberResponsibilityBo responsibility = (UberResponsibilityBo)businessObject;
105        List<RoleBo> assignedToRoles = responsibility.getAssignedToRoles();
106        List<HtmlData.AnchorHtmlData> htmlData = new ArrayList<HtmlData.AnchorHtmlData>();
107                if(assignedToRoles!=null && !assignedToRoles.isEmpty()){
108                        List<String> primaryKeys = Collections.singletonList(ID);
109                        RoleService roleService = KimApiServiceLocator.getRoleService();
110                        for(RoleBo roleImpl: assignedToRoles){
111                                Role role = roleService.getRole(roleImpl.getId());
112                                HtmlData.AnchorHtmlData inquiryHtmlData = getInquiryUrlForPrimaryKeys(RoleBo.class, role, primaryKeys,
113                                        role.getNamespaceCode()+" "+
114                                        role.getName());
115                                inquiryHtmlData.setHref(RoleLookupableHelperServiceImpl.getCustomRoleInquiryHref(inquiryHtmlData.getHref()));
116                        htmlData.add(inquiryHtmlData);
117                }
118                }
119        return new HtmlData.MultipleAnchorHtmlData(htmlData);
120    }
121
122    @Override
123        public Object retrieveDataObject(@SuppressWarnings("rawtypes") Map fieldValues){
124        return getBusinessObject(fieldValues);
125    }
126
127        @Override
128        public BusinessObject getBusinessObject(@SuppressWarnings("rawtypes") Map fieldValues) {
129        if ( fieldValues.get(ID) == null ) {
130            return null;
131        }       
132                ResponsibilityBo responsibilityImpl = getDataObjectService().find(ResponsibilityBo.class, fieldValues.get(ID).toString());
133                return getResponsibilitiesSearchResultsCopy(responsibilityImpl);
134        }
135
136        private UberResponsibilityBo getResponsibilitiesSearchResultsCopy(ResponsibilityBo responsibilitySearchResult){
137                UberResponsibilityBo responsibilitySearchResultCopy = new UberResponsibilityBo();
138                try{
139                        PropertyUtils.copyProperties(responsibilitySearchResultCopy, responsibilitySearchResult);
140                } catch(Exception ex){
141            throw new RuntimeException(ex);
142                }
143                List<RoleResponsibilityBo> roleResponsibilitys = 
144                        getDataObjectService().findMatching(RoleResponsibilityBo.class, 
145                                QueryByCriteria.Builder.forAttribute("responsibilityId", responsibilitySearchResultCopy.getId()).build()).getResults();
146                List<RoleBo> assignedToRoles = new ArrayList<RoleBo>();
147                for(RoleResponsibilityBo roleResponsibilityImpl: roleResponsibilitys){
148                        assignedToRoles.add( getDataObjectService().find(RoleBo.class, roleResponsibilityImpl.getRoleId()));
149                }
150                responsibilitySearchResultCopy.setAssignedToRoles(assignedToRoles);
151                return responsibilitySearchResultCopy;
152        }
153
154}