List of usage examples for org.hibernate.mapping Map.Entry getKey
public KeyValue getKey()
From source file:org.sparkcommerce.openadmin.server.dao.DynamicEntityDaoImpl.java
License:Apache License
protected void buildPropertiesFromPolymorphicEntities(Class<?>[] entities, ForeignKey foreignField, String[] additionalNonPersistentProperties, ForeignKey[] additionalForeignFields, MergedPropertyType mergedPropertyType, Boolean populateManyToOneFields, String[] includeFields, String[] excludeFields, String configurationKey, String ceilingEntityFullyQualifiedClassname, Map<String, FieldMetadata> mergedProperties, List<Class<?>> parentClasses, String prefix, Boolean isParentExcluded) { for (Class<?> clazz : entities) { String cacheKey = getCacheKey(foreignField, additionalNonPersistentProperties, additionalForeignFields, mergedPropertyType, populateManyToOneFields, clazz, configurationKey, isParentExcluded); Map<String, FieldMetadata> cacheData = null; synchronized (DynamicDaoHelperImpl.LOCK_OBJECT) { if (useCache()) { cacheData = METADATA_CACHE.get(cacheKey); }/* w w w . j a v a2s . c om*/ if (cacheData == null) { Map<String, FieldMetadata> props = getPropertiesForEntityClass(clazz, foreignField, additionalNonPersistentProperties, additionalForeignFields, mergedPropertyType, populateManyToOneFields, includeFields, excludeFields, configurationKey, ceilingEntityFullyQualifiedClassname, parentClasses, prefix, isParentExcluded); //first check all the properties currently in there to see if my entity inherits from them for (Class<?> clazz2 : entities) { if (!clazz2.getName().equals(clazz.getName())) { for (Map.Entry<String, FieldMetadata> entry : props.entrySet()) { FieldMetadata metadata = entry.getValue(); try { if (Class.forName(metadata.getInheritedFromType()).isAssignableFrom(clazz2)) { String[] both = (String[]) ArrayUtils.addAll(metadata.getAvailableToTypes(), new String[] { clazz2.getName() }); metadata.setAvailableToTypes(both); } } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } } } METADATA_CACHE.put(cacheKey, props); cacheData = props; } } //clone the metadata before passing to the system Map<String, FieldMetadata> clonedCache = new HashMap<String, FieldMetadata>(cacheData.size()); for (Map.Entry<String, FieldMetadata> entry : cacheData.entrySet()) { clonedCache.put(entry.getKey(), entry.getValue().cloneFieldMetadata()); } mergedProperties.putAll(clonedCache); } }
From source file:org.sparkcommerce.openadmin.server.dao.DynamicEntityDaoImpl.java
License:Apache License
protected void buildEntityProperties(Map<String, FieldMetadata> fields, ForeignKey foreignField, ForeignKey[] additionalForeignFields, String[] additionalNonPersistentProperties, Boolean populateManyToOneFields, String[] includeFields, String[] excludeFields, String configurationKey, String ceilingEntityFullyQualifiedClassname, String propertyName, Class<?> returnedClass, Class<?> targetClass, List<Class<?>> parentClasses, String prefix, Boolean isParentExcluded) { Class<?>[] polymorphicEntities = getAllPolymorphicEntitiesFromCeiling(returnedClass); List<Class<?>> clonedParentClasses = new ArrayList<Class<?>>(); for (Class<?> parentClass : parentClasses) { clonedParentClasses.add(parentClass); }/*from w w w . j av a2 s . c o m*/ clonedParentClasses.add(targetClass); Map<String, FieldMetadata> newFields = getMergedPropertiesRecursively(ceilingEntityFullyQualifiedClassname, polymorphicEntities, foreignField, additionalNonPersistentProperties, additionalForeignFields, MergedPropertyType.PRIMARY, populateManyToOneFields, includeFields, excludeFields, configurationKey, clonedParentClasses, prefix + propertyName + '.', isParentExcluded); for (FieldMetadata newMetadata : newFields.values()) { newMetadata.setInheritedFromType(targetClass.getName()); newMetadata.setAvailableToTypes(new String[] { targetClass.getName() }); } Map<String, FieldMetadata> convertedFields = new HashMap<String, FieldMetadata>(newFields.size()); for (Map.Entry<String, FieldMetadata> key : newFields.entrySet()) { convertedFields.put(propertyName + '.' + key.getKey(), key.getValue()); if (key.getValue() instanceof BasicFieldMetadata) { for (Map.Entry<String, Map<String, String>> entry : ((BasicFieldMetadata) key.getValue()) .getValidationConfigurations().entrySet()) { Class<?> validatorImpl = null; try { validatorImpl = Class.forName(entry.getKey()); } catch (ClassNotFoundException e) { Object bean = applicationContext.getBean(entry.getKey()); if (bean != null) { validatorImpl = bean.getClass(); } } if (validatorImpl != null && FieldNamePropertyValidator.class.isAssignableFrom(validatorImpl)) { for (Map.Entry<String, String> configs : entry.getValue().entrySet()) { if (newFields.containsKey(configs.getValue())) { configs.setValue(propertyName + "." + configs.getValue()); } } } } } } fields.putAll(convertedFields); }
From source file:org.sparkcommerce.openadmin.server.service.persistence.module.MapStructurePersistenceModule.java
License:Apache License
protected Entity[] getMapRecords(Serializable record, MapStructure mapStructure, Map<String, FieldMetadata> ceilingMergedProperties, Map<String, FieldMetadata> valueMergedProperties, Property symbolicIdProperty)//from w ww . j a v a2s .co m throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, SecurityException, IllegalArgumentException, ClassNotFoundException, NoSuchFieldException { //compile a list of mapKeys that were used as mapFields List<String> mapFieldKeys = new ArrayList<String>(); String mapProperty = mapStructure.getMapProperty(); for (Map.Entry<String, FieldMetadata> entry : ceilingMergedProperties.entrySet()) { if (entry.getKey().startsWith(mapProperty + FieldManager.MAPFIELDSEPARATOR)) { mapFieldKeys.add(entry.getKey().substring(entry.getKey().indexOf(FieldManager.MAPFIELDSEPARATOR) + FieldManager.MAPFIELDSEPARATOR.length(), entry.getKey().length())); } } Collections.sort(mapFieldKeys); FieldManager fieldManager = getFieldManager(); Map map; try { map = (Map) fieldManager.getFieldValue(record, mapProperty); } catch (FieldNotAvailableException e) { throw new IllegalArgumentException(e); } List<Entity> entities = new ArrayList<Entity>(map.size()); for (Object key : map.keySet()) { if (key instanceof String && mapFieldKeys.contains(key)) { continue; } Entity entityItem = new Entity(); entityItem.setType(new String[] { record.getClass().getName() }); entities.add(entityItem); List<Property> props = new ArrayList<Property>(); Property propertyItem = new Property(); propertyItem.setName(mapStructure.getKeyPropertyName()); props.add(propertyItem); String strVal; if (Date.class.isAssignableFrom(key.getClass())) { strVal = getSimpleDateFormatter().format((Date) key); } else if (Timestamp.class.isAssignableFrom(key.getClass())) { strVal = getSimpleDateFormatter().format(new Date(((Timestamp) key).getTime())); } else if (Calendar.class.isAssignableFrom(key.getClass())) { strVal = getSimpleDateFormatter().format(((Calendar) key).getTime()); } else if (Double.class.isAssignableFrom(key.getClass())) { strVal = getDecimalFormatter().format(key); } else if (BigDecimal.class.isAssignableFrom(key.getClass())) { strVal = getDecimalFormatter().format(key); } else { strVal = key.toString(); } propertyItem.setValue(strVal); PersistentClass persistentClass = persistenceManager.getDynamicEntityDao() .getPersistentClass(mapStructure.getValueClassName()); if (persistentClass == null) { Property temp = new Property(); temp.setName(((SimpleValueMapStructure) mapStructure).getValuePropertyName()); temp.setValue(String.valueOf(map.get(key))); props.add(temp); } else { extractPropertiesFromPersistentEntity(valueMergedProperties, (Serializable) map.get(key), props); } if (symbolicIdProperty != null) { props.add(symbolicIdProperty); } Property[] properties = new Property[props.size()]; properties = props.toArray(properties); entityItem.setProperties(properties); } return entities.toArray(new Entity[entities.size()]); }
From source file:org.unitime.commons.hibernate.util.HibernateUtil.java
License:Open Source License
public static void clearCache(Class persistentClass, boolean evictQueries) { _RootDAO dao = new _RootDAO(); org.hibernate.Session hibSession = dao.getSession(); SessionFactory hibSessionFactory = hibSession.getSessionFactory(); if (persistentClass == null) { for (Iterator i = hibSessionFactory.getAllClassMetadata().entrySet().iterator(); i.hasNext();) { Map.Entry entry = (Map.Entry) i.next(); String className = (String) entry.getKey(); ClassMetadata classMetadata = (ClassMetadata) entry.getValue(); try { hibSessionFactory.getCache().evictEntityRegion(Class.forName(className)); for (int j = 0; j < classMetadata.getPropertyNames().length; j++) { if (classMetadata.getPropertyTypes()[j].isCollectionType()) { try { hibSessionFactory.getCache().evictCollectionRegion( className + "." + classMetadata.getPropertyNames()[j]); } catch (MappingException e) { }//w w w. j a va 2 s . co m } } } catch (ClassNotFoundException e) { } } hibSessionFactory.getCache().evictEntityRegions(); hibSessionFactory.getCache().evictCollectionRegions(); } else { ClassMetadata classMetadata = hibSessionFactory.getClassMetadata(persistentClass); hibSessionFactory.getCache().evictEntityRegion(persistentClass); if (classMetadata != null) { for (int j = 0; j < classMetadata.getPropertyNames().length; j++) { if (classMetadata.getPropertyTypes()[j].isCollectionType()) { try { hibSessionFactory.getCache().evictCollectionRegion(persistentClass.getClass().getName() + "." + classMetadata.getPropertyNames()[j]); } catch (MappingException e) { } } } } } if (evictQueries) { hibSessionFactory.getCache().evictQueryRegions(); hibSessionFactory.getCache().evictDefaultQueryRegion(); } }