001 /** 002 * Copyright 2005-2013 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.document.rule; 017 018 import org.apache.commons.lang.StringUtils; 019 import org.apache.log4j.Logger; 020 import org.kuali.rice.core.api.uif.RemotableAttributeError; 021 import org.kuali.rice.kim.api.KimConstants; 022 import org.kuali.rice.kim.api.type.KimTypeAttribute; 023 import org.kuali.rice.kim.bo.ui.KimDocumentAttributeDataBusinessObjectBase; 024 import org.kuali.rice.kim.impl.common.attribute.KimAttributeBo; 025 import org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo; 026 import org.kuali.rice.krad.service.BusinessObjectService; 027 import org.kuali.rice.krad.service.KRADServiceLocator; 028 import org.kuali.rice.krad.util.GlobalVariables; 029 import org.kuali.rice.krad.util.KRADConstants; 030 import org.kuali.rice.krad.util.KRADPropertyConstants; 031 032 import java.util.ArrayList; 033 import java.util.HashMap; 034 import java.util.List; 035 import java.util.Map; 036 037 /** 038 * This is a description of what this class does - wliang don't forget to fill this in. 039 * 040 * @author Kuali Rice Team (rice.collab@kuali.org) 041 * 042 */ 043 public class AttributeValidationHelper { 044 private static final Logger LOG = Logger.getLogger(AttributeValidationHelper.class); 045 046 private static final String DOCUMENT_PROPERTY_PREFIX = KRADConstants.DOCUMENT_PROPERTY_NAME + "."; 047 048 protected BusinessObjectService businessObjectService; 049 protected Map<String,KimAttributeBo> attributeDefinitionMap = new HashMap<String,KimAttributeBo>(); 050 051 protected KimAttributeBo getAttributeDefinition( String id ) { 052 KimAttributeBo attributeImpl = attributeDefinitionMap.get( id ); 053 054 if ( attributeImpl == null ) { 055 Map<String,String> criteria = new HashMap<String,String>(); 056 criteria.put( KimConstants.PrimaryKeyConstants.KIM_ATTRIBUTE_ID, id ); 057 attributeImpl = (KimAttributeBo)getBusinessObjectService().findByPrimaryKey( KimAttributeBo.class, criteria ); 058 attributeDefinitionMap.put( id, attributeImpl ); 059 } 060 return attributeImpl; 061 } 062 063 public Map<String, String> convertAttributesToMap(List<? extends KimAttributeDataBo> attributes) { 064 Map<String, String> m = new HashMap<String, String>(); 065 for(KimAttributeDataBo data: attributes) { 066 KimAttributeBo attrib = getAttributeDefinition(data.getKimAttributeId()); 067 if(attrib != null){ 068 m.put(attrib.getAttributeName(), data.getAttributeValue()); 069 } else { 070 LOG.error("Unable to get attribute name for ID:" + data.getKimAttributeId()); 071 } 072 } 073 return m; 074 } 075 076 public Map<String, String> convertQualifiersToMap( List<? extends KimDocumentAttributeDataBusinessObjectBase> qualifiers ) { 077 Map<String, String> m = new HashMap<String, String>(); 078 for ( KimDocumentAttributeDataBusinessObjectBase data : qualifiers ) { 079 KimAttributeBo attrib = getAttributeDefinition( data.getKimAttrDefnId() ); 080 if ( attrib != null ) { 081 m.put( attrib.getAttributeName(), data.getAttrVal() ); 082 } else { 083 LOG.error("Unable to get attribute name for ID:" + data.getKimAttrDefnId() ); 084 } 085 } 086 return m; 087 } 088 089 public Map<String, String> getBlankValueQualifiersMap(List<KimTypeAttribute> attributes) { 090 Map<String, String> m = new HashMap<String, String>(); 091 for(KimTypeAttribute attribute: attributes){ 092 KimAttributeBo attrib = getAttributeDefinition(attribute.getKimAttribute().getId()); 093 if ( attrib != null ) { 094 m.put( attrib.getAttributeName(), "" ); 095 } else { 096 LOG.error("Unable to get attribute name for ID:" + attribute.getId()); 097 } 098 } 099 return m; 100 } 101 102 public Map<String, String> convertQualifiersToAttrIdxMap( List<? extends KimDocumentAttributeDataBusinessObjectBase> qualifiers ) { 103 Map<String, String> m = new HashMap<String, String>(); 104 int i = 0; 105 for ( KimDocumentAttributeDataBusinessObjectBase data : qualifiers ) { 106 KimAttributeBo attrib = getAttributeDefinition( data.getKimAttrDefnId() ); 107 if ( attrib != null ) { 108 m.put( attrib.getAttributeName(), Integer.toString(i) ); 109 } else { 110 LOG.error("Unable to get attribute name for ID:" + data.getKimAttrDefnId() ); 111 } 112 i++; 113 } 114 return m; 115 } 116 117 public BusinessObjectService getBusinessObjectService() { 118 if(businessObjectService == null){ 119 businessObjectService = KRADServiceLocator.getBusinessObjectService(); 120 } 121 return businessObjectService; 122 } 123 124 public void moveValidationErrorsToErrorMap(List<RemotableAttributeError> validationErrors) { 125 // FIXME: the above code would overwrite messages on the same attributes (namespaceCode) but on different rows 126 for ( RemotableAttributeError error : validationErrors) { 127 for (String errMsg : error.getErrors()) { 128 String[] splitMsg = StringUtils.split(errMsg, ":"); 129 130 // if the property name starts with "document." then don't prefix with the error path 131 if (error.getAttributeName().startsWith(DOCUMENT_PROPERTY_PREFIX)) { 132 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath( error.getAttributeName(), splitMsg[0], splitMsg.length > 1 ? StringUtils.split(splitMsg[1], ";") : new String[] {} ); 133 } else { 134 GlobalVariables.getMessageMap().putError( error.getAttributeName(), splitMsg[0], splitMsg.length > 1 ? StringUtils.split(splitMsg[1], ";") : new String[] {} ); 135 } 136 } 137 } 138 } 139 140 public List<RemotableAttributeError> convertErrorsForMappedFields(String errorPath, List<RemotableAttributeError> localErrors) { 141 List<RemotableAttributeError> errors = new ArrayList<RemotableAttributeError>(); 142 if (errorPath == null) { 143 errorPath = KRADConstants.EMPTY_STRING; 144 } 145 else if (StringUtils.isNotEmpty(errorPath)) { 146 errorPath = errorPath + "."; 147 } 148 for ( RemotableAttributeError error : localErrors) { 149 Map<String,String> criteria = new HashMap<String,String>(); 150 criteria.put(KRADPropertyConstants.ATTRIBUTE_NAME, error.getAttributeName()); 151 KimAttributeBo attribute = getBusinessObjectService().findByPrimaryKey(KimAttributeBo.class, criteria); 152 String attributeDefnId = attribute==null?"":attribute.getId(); 153 errors.add(RemotableAttributeError.Builder.create(errorPath+"qualifier("+attributeDefnId+").attrVal", error.getErrors()).build()); 154 } 155 return errors; 156 } 157 158 public List<RemotableAttributeError> convertErrors(String errorPath, Map<String, String> attrIdxMap, List<RemotableAttributeError> localErrors) { 159 List<RemotableAttributeError> errors = new ArrayList<RemotableAttributeError>(); 160 if (errorPath == null) { 161 errorPath = KRADConstants.EMPTY_STRING; 162 } 163 else if (StringUtils.isNotEmpty(errorPath)) { 164 errorPath = errorPath + "."; 165 } 166 for ( RemotableAttributeError error : localErrors ) { 167 for (String errMsg : error.getErrors()) { 168 errors.add(RemotableAttributeError.Builder.create(errorPath+"qualifiers["+attrIdxMap.get(error.getAttributeName())+"].attrVal", errMsg).build()); 169 } 170 } 171 return errors; 172 } 173 }