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.inquiry;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.coreservice.impl.namespace.NamespaceBo;
020import org.kuali.rice.kim.api.KimConstants;
021import org.kuali.rice.kim.impl.group.GroupBo;
022import org.kuali.rice.kim.impl.type.KimTypeBo;
023import org.kuali.rice.kim.util.KimCommonUtilsInternal;
024import org.kuali.rice.kns.inquiry.KualiInquirableImpl;
025import org.kuali.rice.kns.lookup.HtmlData;
026import org.kuali.rice.krad.bo.BusinessObject;
027import org.kuali.rice.krad.uif.widget.Inquiry;
028import org.kuali.rice.krad.util.KRADConstants;
029import org.kuali.rice.krad.util.ObjectUtils;
030import org.kuali.rice.krad.util.UrlFactory;
031
032import java.util.ArrayList;
033import java.util.Collections;
034import java.util.HashMap;
035import java.util.List;
036import java.util.Map;
037import java.util.Properties;
038
039/**
040 * This is a description of what this class does - kellerj don't forget to fill this in.
041 *
042 * @author Kuali Rice Team (rice.collab@kuali.org)
043 *
044 */
045public class GroupInquirableImpl extends KualiInquirableImpl {
046
047
048        protected final String GROUP_NAME = "name";
049        protected final String GROUP_ID = "id";
050        protected final String NAMESPACE_CODE = "namespaceCode";
051        
052        @Override
053        public void buildInquirableLink(Object dataObject, String propertyName, Inquiry inquiry){
054                
055                if(GROUP_NAME.equals(propertyName)){
056                        Map<String, String> primaryKeys = new HashMap<String, String>();
057                        primaryKeys.put(GROUP_ID, GROUP_ID);
058                        inquiry.buildInquiryLink(dataObject, propertyName, GroupBo.class, primaryKeys);
059                } else if(NAMESPACE_CODE.equals(propertyName)){
060                        Map<String, String> primaryKeys = new HashMap<String, String>();
061                        primaryKeys.put(propertyName, "code");
062                        inquiry.buildInquiryLink(dataObject, propertyName, NamespaceBo.class, primaryKeys);
063                } else if("kimTypeInfo.name".equals(propertyName)){
064                        Map<String, String> primaryKeys = new HashMap<String, String>();
065                        primaryKeys.put("kimTypeInfo.id", KimConstants.PrimaryKeyConstants.KIM_TYPE_ID);
066                        inquiry.buildInquiryLink(dataObject, propertyName, KimTypeBo.class, primaryKeys);
067        }
068        }
069        
070    @Override
071    public HtmlData getInquiryUrl(BusinessObject businessObject, String attributeName, boolean forceInquiry) {
072                if(GROUP_NAME.equals(attributeName)){
073                        List<String> primaryKeys = new ArrayList<String>();
074                        primaryKeys.add(GROUP_ID);
075                    String href = (getInquiryUrlForPrimaryKeys(GroupBo.class, businessObject, primaryKeys, null)).getHref();
076                    HtmlData.AnchorHtmlData htmlData = new HtmlData.AnchorHtmlData();
077                    htmlData.setHref(getCustomGroupInquiryHref(href));
078                        return htmlData;
079                } else if(NAMESPACE_CODE.equals(attributeName)){
080                        List<String> primaryKeys = new ArrayList<String>();
081                        primaryKeys.add("code");
082                        NamespaceBo parameterNamespace = new NamespaceBo();
083                        parameterNamespace.setCode((String)ObjectUtils.getPropertyValue(businessObject, attributeName));
084                        return getInquiryUrlForPrimaryKeys(NamespaceBo.class, parameterNamespace, primaryKeys, null);
085                } else if("kimTypeInfo.name".equals(attributeName)){
086                        KimTypeBo kimType = new KimTypeBo();
087                        kimType.setId( ((GroupBo)businessObject).getKimTypeId() );
088                        return getInquiryUrlForPrimaryKeys(KimTypeBo.class, kimType, Collections.singletonList( KimConstants.PrimaryKeyConstants.KIM_TYPE_ID ), null);
089        }
090                
091        return super.getInquiryUrl(businessObject, attributeName, forceInquiry);
092    }
093
094        public String getCustomGroupInquiryHref(String href){
095        Properties parameters = new Properties();
096        String hrefPart = "";
097                if (StringUtils.isNotBlank(href) && href.contains("&" + KimConstants.PrimaryKeyConstants.GROUP_ID + "=")) {
098                        int idx1 = href.indexOf("&"+KimConstants.PrimaryKeyConstants.GROUP_ID+"=");
099                    int idx2 = href.indexOf("&", idx1+1);
100                    if (idx2 < 0) {
101                        idx2 = href.length();
102                    }
103                parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.PARAM_MAINTENANCE_VIEW_MODE_INQUIRY);
104                hrefPart = href.substring(idx1, idx2);
105            }
106                return UrlFactory.parameterizeUrl(KimCommonUtilsInternal.getKimBasePath()+
107                                KimConstants.KimUIConstants.KIM_GROUP_INQUIRY_ACTION, parameters)+hrefPart;
108        }
109
110        /**
111         * This overridden method ...
112         *
113         * @see org.kuali.rice.krad.inquiry.KualiInquirableImpl#getBusinessObject(java.util.Map)
114         */
115        @SuppressWarnings("unchecked")
116        @Override
117        public BusinessObject getBusinessObject(Map fieldValues) {
118            BusinessObject bo = super.getBusinessObject(fieldValues);
119            return bo;
120        }
121
122}