| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.krad.datadictionary; |
| 17 | |
|
| 18 | |
import org.apache.commons.lang.StringUtils; |
| 19 | |
import org.apache.commons.logging.Log; |
| 20 | |
import org.apache.commons.logging.LogFactory; |
| 21 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
| 22 | |
import org.kuali.rice.krad.uif.UifConstants; |
| 23 | |
import org.kuali.rice.krad.uif.component.Component; |
| 24 | |
import org.kuali.rice.krad.uif.util.ComponentUtils; |
| 25 | |
import org.kuali.rice.krad.uif.util.ViewModelUtils; |
| 26 | |
import org.kuali.rice.krad.uif.view.View; |
| 27 | |
import org.kuali.rice.krad.uif.service.ViewTypeService; |
| 28 | |
import org.springframework.beans.PropertyValues; |
| 29 | |
import org.springframework.beans.factory.config.BeanDefinition; |
| 30 | |
import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 31 | |
import org.kuali.rice.krad.uif.UifConstants.ViewType; |
| 32 | |
|
| 33 | |
import java.util.ArrayList; |
| 34 | |
import java.util.HashMap; |
| 35 | |
import java.util.List; |
| 36 | |
import java.util.Map; |
| 37 | |
import java.util.Map.Entry; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public class UifDictionaryIndex implements Runnable { |
| 51 | 0 | private static final Log LOG = LogFactory.getLog(UifDictionaryIndex.class); |
| 52 | |
|
| 53 | |
private DefaultListableBeanFactory ddBeans; |
| 54 | |
|
| 55 | |
|
| 56 | |
private Map<String, String> viewBeanEntriesById; |
| 57 | |
|
| 58 | |
|
| 59 | |
private Map<String, ViewTypeDictionaryIndex> viewEntriesByType; |
| 60 | |
|
| 61 | 0 | public UifDictionaryIndex(DefaultListableBeanFactory ddBeans) { |
| 62 | 0 | this.ddBeans = ddBeans; |
| 63 | 0 | } |
| 64 | |
|
| 65 | |
public void run() { |
| 66 | 0 | LOG.info("Starting View Index Building"); |
| 67 | 0 | buildViewIndicies(); |
| 68 | 0 | LOG.info("Completed View Index Building"); |
| 69 | 0 | } |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public View getViewById(String viewId) { |
| 80 | 0 | String beanName = viewBeanEntriesById.get(viewId); |
| 81 | 0 | if (StringUtils.isBlank(beanName)) { |
| 82 | 0 | throw new DataDictionaryException("Unable to find View with id: " + viewId); |
| 83 | |
} |
| 84 | |
|
| 85 | 0 | return ddBeans.getBean(beanName, View.class); |
| 86 | |
} |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
public View getViewByTypeIndex(ViewType viewTypeName, Map<String, String> indexKey) { |
| 100 | 0 | String index = buildTypeIndex(indexKey); |
| 101 | |
|
| 102 | 0 | ViewTypeDictionaryIndex typeIndex = getTypeIndex(viewTypeName); |
| 103 | |
|
| 104 | 0 | String beanName = typeIndex.get(index); |
| 105 | 0 | if (StringUtils.isNotBlank(beanName)) { |
| 106 | 0 | return ddBeans.getBean(beanName, View.class); |
| 107 | |
} |
| 108 | |
|
| 109 | 0 | return null; |
| 110 | |
} |
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
public boolean viewByTypeExist(ViewType viewTypeName, Map<String, String> indexKey) { |
| 121 | 0 | boolean viewExist = false; |
| 122 | |
|
| 123 | 0 | String index = buildTypeIndex(indexKey); |
| 124 | 0 | ViewTypeDictionaryIndex typeIndex = getTypeIndex(viewTypeName); |
| 125 | |
|
| 126 | 0 | String beanName = typeIndex.get(index); |
| 127 | 0 | if (StringUtils.isNotBlank(beanName)) { |
| 128 | 0 | viewExist = true; |
| 129 | |
} |
| 130 | |
|
| 131 | 0 | return viewExist; |
| 132 | |
} |
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public PropertyValues getViewPropertiesById(String viewId) { |
| 146 | 0 | String beanName = viewBeanEntriesById.get(viewId); |
| 147 | 0 | if (StringUtils.isBlank(beanName)) { |
| 148 | 0 | BeanDefinition beanDefinition = ddBeans.getMergedBeanDefinition(beanName); |
| 149 | |
|
| 150 | 0 | return beanDefinition.getPropertyValues(); |
| 151 | |
} |
| 152 | |
|
| 153 | 0 | return null; |
| 154 | |
} |
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
public PropertyValues getViewPropertiesByType(ViewType viewTypeName, Map<String, String> indexKey) { |
| 171 | 0 | String index = buildTypeIndex(indexKey); |
| 172 | |
|
| 173 | 0 | ViewTypeDictionaryIndex typeIndex = getTypeIndex(viewTypeName); |
| 174 | |
|
| 175 | 0 | String beanName = typeIndex.get(index); |
| 176 | 0 | if (StringUtils.isNotBlank(beanName)) { |
| 177 | 0 | BeanDefinition beanDefinition = ddBeans.getMergedBeanDefinition(beanName); |
| 178 | |
|
| 179 | 0 | return beanDefinition.getPropertyValues(); |
| 180 | |
} |
| 181 | |
|
| 182 | 0 | return null; |
| 183 | |
} |
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
public List<View> getViewsForType(ViewType viewTypeName) { |
| 194 | 0 | List<View> typeViews = new ArrayList<View>(); |
| 195 | |
|
| 196 | |
|
| 197 | 0 | if (viewEntriesByType.containsKey(viewTypeName.name())) { |
| 198 | 0 | ViewTypeDictionaryIndex typeIndex = viewEntriesByType.get(viewTypeName.name()); |
| 199 | 0 | for (Entry<String, String> typeEntry : typeIndex.getViewIndex().entrySet()) { |
| 200 | 0 | View typeView = ddBeans.getBean(typeEntry.getValue(), View.class); |
| 201 | 0 | typeViews.add(typeView); |
| 202 | 0 | } |
| 203 | 0 | } else { |
| 204 | 0 | throw new DataDictionaryException("Unable to find view index for type: " + viewTypeName); |
| 205 | |
} |
| 206 | |
|
| 207 | 0 | return typeViews; |
| 208 | |
} |
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
protected void buildViewIndicies() { |
| 216 | 0 | viewBeanEntriesById = new HashMap<String, String>(); |
| 217 | 0 | viewEntriesByType = new HashMap<String, ViewTypeDictionaryIndex>(); |
| 218 | |
|
| 219 | 0 | String[] beanNames = ddBeans.getBeanNamesForType(View.class); |
| 220 | 0 | for (int i = 0; i < beanNames.length; i++) { |
| 221 | 0 | String beanName = beanNames[i]; |
| 222 | 0 | BeanDefinition beanDefinition = ddBeans.getMergedBeanDefinition(beanName); |
| 223 | 0 | PropertyValues propertyValues = beanDefinition.getPropertyValues(); |
| 224 | |
|
| 225 | 0 | String id = ViewModelUtils.getStringValFromPVs(propertyValues, "id"); |
| 226 | 0 | if (StringUtils.isBlank(id)) { |
| 227 | 0 | id = beanName; |
| 228 | |
} |
| 229 | |
|
| 230 | 0 | if (viewBeanEntriesById.containsKey(id)) { |
| 231 | 0 | throw new DataDictionaryException("Two views must not share the same id. Found duplicate id: " + id); |
| 232 | |
} |
| 233 | 0 | viewBeanEntriesById.put(id, beanName); |
| 234 | |
|
| 235 | 0 | indexViewForType(propertyValues, beanName); |
| 236 | |
} |
| 237 | 0 | } |
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
protected void indexViewForType(PropertyValues propertyValues, String beanName) { |
| 249 | 0 | String viewTypeName = ViewModelUtils.getStringValFromPVs(propertyValues, "viewTypeName"); |
| 250 | 0 | if (StringUtils.isBlank(viewTypeName)) { |
| 251 | 0 | return; |
| 252 | |
} |
| 253 | |
|
| 254 | 0 | UifConstants.ViewType viewType = ViewType.valueOf(viewTypeName); |
| 255 | |
|
| 256 | 0 | ViewTypeService typeService = KRADServiceLocatorWeb.getViewService().getViewTypeService(viewType); |
| 257 | 0 | if (typeService == null) { |
| 258 | |
|
| 259 | 0 | return; |
| 260 | |
} |
| 261 | |
|
| 262 | |
|
| 263 | 0 | Map<String, String> typeParameters = typeService.getParametersFromViewConfiguration(propertyValues); |
| 264 | |
|
| 265 | |
|
| 266 | 0 | String index = buildTypeIndex(typeParameters); |
| 267 | |
|
| 268 | |
|
| 269 | 0 | ViewTypeDictionaryIndex typeIndex = getTypeIndex(viewType); |
| 270 | |
|
| 271 | 0 | typeIndex.put(index, beanName); |
| 272 | 0 | } |
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
protected ViewTypeDictionaryIndex getTypeIndex(UifConstants.ViewType viewType) { |
| 283 | 0 | ViewTypeDictionaryIndex typeIndex = null; |
| 284 | |
|
| 285 | 0 | if (viewEntriesByType.containsKey(viewType.name())) { |
| 286 | 0 | typeIndex = viewEntriesByType.get(viewType.name()); |
| 287 | |
} else { |
| 288 | 0 | typeIndex = new ViewTypeDictionaryIndex(); |
| 289 | 0 | viewEntriesByType.put(viewType.name(), typeIndex); |
| 290 | |
} |
| 291 | |
|
| 292 | 0 | return typeIndex; |
| 293 | |
} |
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
protected String buildTypeIndex(Map<String, String> typeParameters) { |
| 302 | 0 | String index = ""; |
| 303 | |
|
| 304 | 0 | for (String parameterName : typeParameters.keySet()) { |
| 305 | 0 | if (StringUtils.isNotBlank(index)) { |
| 306 | 0 | index += "|||"; |
| 307 | |
} |
| 308 | 0 | index += parameterName + "^^" + typeParameters.get(parameterName); |
| 309 | |
} |
| 310 | |
|
| 311 | 0 | return index; |
| 312 | |
} |
| 313 | |
|
| 314 | |
} |