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