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