List of usage examples for org.hibernate EntityMode POJO
EntityMode POJO
To view the source code for org.hibernate EntityMode POJO.
Click Source Link
From source file:org.mifos.framework.components.audit.util.helpers.InterceptHelper.java
License:Open Source License
private void setPrimaryKeyValues(ClassMetadata customMeta, Object obj, String name, String state) { if (state.equalsIgnoreCase(AuditConstants.TRANSACTIONBEGIN)) { if (AuditConfiguration.checkForPropertyName(entityName, name, localeId)) { String value = AuditConfiguration.getValueOfCorrespondingId(entityName, name, customMeta.getIdentifier(obj, EntityMode.POJO), localeId); initialValues.put(name, value); logger.debug("i setPrimaryKeyValues " + name + " value : " + value); } else {/*from w w w. jav a 2 s . c o m*/ initialValues.put(name, customMeta.getIdentifier(obj, EntityMode.POJO)); logger.debug("i setPrimaryKeyValues " + name + " value : " + customMeta.getIdentifier(obj, EntityMode.POJO)); } String columnName = AuditConfiguration.getColumnNameForPropertyName(entityName, name); if (columnName != null && !columnName.equals("")) { columnNames.put(name, columnName); } else { columnNames.put(name, customMeta.getIdentifierPropertyName()); } } else { if (AuditConfiguration.checkForPropertyName(entityName, name, localeId)) { String value = AuditConfiguration.getValueOfCorrespondingId(entityName, name, customMeta.getIdentifier(obj, EntityMode.POJO), localeId); logger.debug("c setPrimaryKeyValues " + name + " value : " + value); changedValues.put(name, value); } else { changedValues.put(name, customMeta.getIdentifier(obj, EntityMode.POJO)); } String columnName = AuditConfiguration.getColumnNameForPropertyName(entityName, name); if (columnName != null && !columnName.equals("")) { columnNames.put(name, columnName); } else { columnNames.put(name, customMeta.getIdentifierPropertyName()); } } }
From source file:org.openbravo.dal.core.OBDynamicTuplizer.java
License:Open Source License
@Override public EntityMode getEntityMode() { return EntityMode.POJO; }
From source file:org.openmrs.module.auditlog.api.db.DAOUtils.java
License:Open Source License
/** * Gets a set of concrete subclasses for the specified class recursively, note that interfaces * and abstract classes are excluded/*from ww w .ja va 2 s. c om*/ * * @param clazz the Super Class * @param foundSubclasses the list of subclasses found in previous recursive calls, should be * null for the first call * @param mappedClasses the ClassMetadata Collection * @return a set of subclasses */ @SuppressWarnings("unchecked") private static Set<Class<?>> getPersistentConcreteSubclassesInternal(Class<?> clazz, Set<Class<?>> foundSubclasses, Collection<ClassMetadata> mappedClasses) { if (foundSubclasses == null) { foundSubclasses = new HashSet<Class<?>>(); } if (mappedClasses == null) { mappedClasses = getSessionFactory().getAllClassMetadata().values(); } if (clazz != null) { for (ClassMetadata cmd : mappedClasses) { Class<?> possibleSubclass = cmd.getMappedClass(EntityMode.POJO); if (!clazz.equals(possibleSubclass) && clazz.isAssignableFrom(possibleSubclass)) { if (!Modifier.isAbstract(possibleSubclass.getModifiers()) && !possibleSubclass.isInterface()) { foundSubclasses.add(possibleSubclass); } foundSubclasses.addAll(getPersistentConcreteSubclassesInternal(possibleSubclass, foundSubclasses, mappedClasses)); } } } return foundSubclasses; }
From source file:org.openmrs.module.auditlog.api.db.hibernate.HibernateAuditLogDAO.java
License:Open Source License
/** * @see org.openmrs.module.auditlog.api.db.AuditLogDAO#getId(Object) * @return/*from ww w . j av a2s . c o m*/ */ @Override public Serializable getId(Object object) { return sessionFactory.getClassMetadata(object.getClass()).getIdentifier(object, EntityMode.POJO); }
From source file:org.openmrs.module.auditlog.api.db.hibernate.interceptor.HibernateAuditLogInterceptor.java
License:Open Source License
/** * @see org.hibernate.EmptyInterceptor#onFlushDirty(Object, java.io.Serializable, Object[], * Object[], String[], org.hibernate.type.Type[]) *//*from w w w . j a v a 2s. co m*/ @Override public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) { if (propertyNames != null && InterceptorUtil.isAudited(entity.getClass())) { if (previousState == null) { //This is a detached object, load the previous state in a separate session Session tmpSession = null; SessionFactory sf = InterceptorUtil.getSessionFactory(); try { tmpSession = SessionFactoryUtils.getNewSession(sf); Object obj = tmpSession.get(entity.getClass(), id); EntityPersister ep = ((SessionImplementor) tmpSession).getEntityPersister(null, obj); previousState = ep.getPropertyValues(obj, EntityMode.POJO); } finally { if (tmpSession != null) { SessionFactoryUtils.closeSession(tmpSession); } } } Map<String, String[]> propertyChangesMap = null;//Map<propertyName, Object[]{currentValue, PreviousValue}> for (int i = 0; i < propertyNames.length; i++) { //we need to ignore dateChanged and changedBy fields in any case they //are actually part of the Auditlog in form of user and dateCreated if (ArrayUtils.contains(IGNORED_PROPERTIES, propertyNames[i])) { continue; } Object previousValue = (previousState != null) ? previousState[i] : null; Object currentValue = (currentState != null) ? currentState[i] : null; if (!types[i].isCollectionType() && !OpenmrsUtil.nullSafeEquals(currentValue, previousValue)) { //For string properties, ignore changes from null to blank and vice versa //TODO This should be user configurable via a module GP if (StringType.class.getName().equals(types[i].getClass().getName()) || TextType.class.getName().equals(types[i].getClass().getName())) { String currentStateString = null; if (currentValue != null && !StringUtils.isBlank(currentValue.toString())) { currentStateString = currentValue.toString(); } String previousValueString = null; if (previousValue != null && !StringUtils.isBlank(previousValue.toString())) { previousValueString = previousValue.toString(); } //TODO Case sensibility here should be configurable via a GP by admin if (OpenmrsUtil.nullSafeEqualsIgnoreCase(previousValueString, currentStateString)) { continue; } } if (propertyChangesMap == null) { propertyChangesMap = new HashMap<String, String[]>(); } String serializedPreviousValue = AuditLogUtil.serializeObject(previousValue); String serializedCurrentValue = AuditLogUtil.serializeObject(currentValue); propertyChangesMap.put(propertyNames[i], new String[] { serializedCurrentValue, serializedPreviousValue }); } } if (MapUtils.isNotEmpty(propertyChangesMap)) { if (log.isDebugEnabled()) { log.debug("Creating log entry for updated object with id:" + id + " of type:" + entity.getClass().getName()); } updates.get().peek().add(entity); objectChangesMap.get().peek().put(entity, propertyChangesMap); } } return false; }
From source file:org.openmrs.module.auditlog.api.db.hibernate.interceptor.HibernateAuditLogInterceptor.java
License:Open Source License
@Override public void onCollectionRemove(Object collection, Serializable key) throws CallbackException { //We need to get all collection elements and link their childlogs to the parent's if (collection != null) { PersistentCollection persistentColl = (PersistentCollection) collection; if (InterceptorUtil.isAudited(persistentColl.getOwner().getClass())) { Object owningObject = persistentColl.getOwner(); String role = persistentColl.getRole(); String propertyName = role.substring(role.lastIndexOf('.') + 1); ClassMetadata cmd = AuditLogUtil.getClassMetadata(AuditLogUtil.getActualType(owningObject)); Object currentCollection = cmd.getPropertyValue(owningObject, propertyName, EntityMode.POJO); //Hibernate calls onCollectionRemove whenever the underlying collection is replaced with a //new instance i.e one calls the collection's setter and passes in a new instance even if the //new collection contains some elements, we want to treat this as regular collection update, //Except if onCollectionRemove is called because the owner got purged from the DB. //I believe hibernate calls onDelete for the owner before onCollectionRemove for all its //collections so we can guarantee that the owner is already in the 'deletes' thread local boolean isOwnerDeleted = OpenmrsUtil.collectionContains(deletes.get().peek(), owningObject); if (Collection.class.isAssignableFrom(collection.getClass())) { Collection coll = (Collection) collection; if (!coll.isEmpty()) { if (isOwnerDeleted) { if (entityRemovedChildrenMap.get().peek().get(owningObject) == null) { entityRemovedChildrenMap.get().peek().put(owningObject, new HashSet<Object>()); }//from www . j a v a 2 s .c o m for (Object removedItem : coll) { entityRemovedChildrenMap.get().peek().get(owningObject).add(removedItem); } } else if (!isOwnerDeleted && currentCollection == null) { Class<?> propertyClass = cmd.getPropertyType(propertyName).getReturnedClass(); if (Set.class.isAssignableFrom(propertyClass)) { currentCollection = Collections.EMPTY_SET; } else if (List.class.isAssignableFrom(propertyClass)) { currentCollection = Collections.EMPTY_LIST; } } } } else if (Map.class.isAssignableFrom(collection.getClass())) { if (!isOwnerDeleted && currentCollection == null) { currentCollection = Collections.EMPTY_MAP; } } else { //TODO: Handle other persistent collections types e.g bags } if (!isOwnerDeleted) { handleUpdatedCollection(currentCollection, collection, owningObject, role); } } } }
From source file:org.openmrs.module.auditlog.api.db.hibernate.interceptor.InterceptorUtil.java
License:Open Source License
/** * Serializes mapped hibernate objects/*from w w w . java2s .c om*/ * * @param object the object to serialize * @return the serialized JSON text */ static String serializePersistentObject(Object object) { //TODO Might be better to use xstream Map<String, Serializable> propertyNameValueMap = null; ClassMetadata cmd = AuditLogUtil.getClassMetadata(AuditLogUtil.getActualType(object)); if (cmd != null) { propertyNameValueMap = new HashMap<String, Serializable>(); propertyNameValueMap.put(cmd.getIdentifierPropertyName(), cmd.getIdentifier(object, EntityMode.POJO)); for (String propertyName : cmd.getPropertyNames()) { String serializedValue = AuditLogUtil .serializeObject(cmd.getPropertyValue(object, propertyName, EntityMode.POJO)); if (serializedValue != null) { propertyNameValueMap.put(propertyName, serializedValue); } } } return AuditLogUtil.serializeToJson(propertyNameValueMap); }
From source file:org.openmrs.module.auditlog.AuditLogHelper.java
License:Open Source License
/** * Gets implicitly audited classes, this are generated as a result of their owning entity types * being marked as audited if they are not explicitly marked as audited themselves, i.e if * Concept is marked as audited, then ConceptName, ConceptDescription, ConceptMapping etc * implicitly get marked as audited// w w w . ja v a 2 s .co m * * @return a set of implicitly audited classes * @should return a set of implicitly audited classes for none except strategy * @should return a set of implicitly audited classes for all except strategy * @should return an empty set for none strategy * @should return an empty set for all strategy */ public Set<Class<?>> getImplicitlyAuditedClasses() { if (implicitlyAuditedTypeCache == null) { implicitlyAuditedTypeCache = new HashSet<Class<?>>(); if (getAuditingStrategy().equals(AuditStrategy.NONE_EXCEPT)) { for (Class<?> auditedClass : getExceptions()) { if (!AuditLogHelper.CORE_EXCEPTIONS.contains(auditedClass)) { addAssociationTypes(auditedClass); } } } else if (getAuditingStrategy().equals(AuditStrategy.ALL_EXCEPT) && getExceptions().size() > 0) { //generate implicitly audited classes so we can track them. The reason behind //this is: Say Concept is marked as audited and strategy is set to All Except //and say ConceptName is for some reason marked as un audited we should still audit //concept names otherwise it poses inconsistencies Collection<ClassMetadata> allClassMetadata = DAOUtils.getSessionFactory().getAllClassMetadata() .values(); for (ClassMetadata classMetadata : allClassMetadata) { Class<?> mappedClass = classMetadata.getMappedClass(EntityMode.POJO); if (!getExceptions().contains(mappedClass)) { if (!AuditLogHelper.CORE_EXCEPTIONS.contains(mappedClass)) { addAssociationTypes(mappedClass); } } } } } return implicitlyAuditedTypeCache; }
From source file:org.openmrs.module.auditlog.util.AuditLogUtil.java
License:Open Source License
/** * Serializes the specified object to a String, typically it returns the object's uuid if it is * an OpenmrsObject, if not it returns the primary key value if it is a persistent object * otherwise to calls the toString method except for Date, Enum and Class objects that are * handled in a special way.//w w w . j a v a 2 s .co m * * @param obj the object to serialize * @return the serialized String form of the object */ public static String serializeObject(Object obj) { String serializedValue = null; if (obj != null) { Class<?> clazz = getActualType(obj); if (Date.class.isAssignableFrom(clazz)) { //TODO We need to handle time zones issues better serializedValue = new SimpleDateFormat(AuditLogConstants.DATE_FORMAT).format(obj); } else if (Enum.class.isAssignableFrom(clazz)) { //Use value.name() over value.toString() to ensure we always get back the enum //constant value and not the value returned by the implementation of value.toString() serializedValue = ((Enum<?>) obj).name(); } else if (Class.class.isAssignableFrom(clazz)) { serializedValue = ((Class<?>) obj).getName(); } else if (Collection.class.isAssignableFrom(clazz)) { serializedValue = serializeCollection((Collection) obj); } else if (Map.class.isAssignableFrom(clazz)) { serializedValue = serializeMap((Map) obj); } if (StringUtils.isBlank(serializedValue)) { ClassMetadata metadata = getClassMetadata(clazz); if (metadata != null) { Serializable id = metadata.getIdentifier(obj, EntityMode.POJO); if (id != null) { serializedValue = id.toString(); } } } if (StringUtils.isBlank(serializedValue)) { serializedValue = obj.toString(); } } return serializedValue; }
From source file:org.openmrs.module.sync.api.db.hibernate.HibernateSyncInterceptor.java
License:Open Source License
/** * Serializes and packages an intercepted change in object state. * <p>//from w w w . j av a 2s.c o m * IMPORTANT serialization notes: * <p> * Transient Properties. Transients are not serialized/journalled. Marking an object property as * transient is the supported way of designating it as something not to be recorded into the * journal. * <p/> * Hibernate Identity property. A property designated in Hibernate as identity (i.e. primary * key) *is* not serialized. This is because sync does not enforce global uniqueness of database * primary keys. Instead, custom uuid property is used. This allows us to continue to use native * types for 'traditional' entity relationships. * * @param entity The object changed. * @param currentState Array containing data for each field in the object as they will be saved. * @param propertyNames Array containing name for each field in the object, corresponding to * currentState. * @param types Array containing Type of the field in the object, corresponding to currentState. * @param state SyncItemState, e.g. NEW, UPDATED, DELETED * @param id Value of the identifier for this entity */ protected void packageObject(OpenmrsObject entity, Object[] currentState, String[] propertyNames, Type[] types, Serializable id, SyncItemState state) throws SyncException { String objectUuid = null; String originalRecordUuid = null; Set<String> transientProps = null; String infoMsg = null; ClassMetadata data = null; String idPropertyName = null; org.hibernate.tuple.IdentifierProperty idPropertyObj = null; // The container of values to be serialized: // Holds tuples of <property-name> -> {<property-type-name>, // <property-value as string>} HashMap<String, PropertyClassValue> values = new HashMap<String, PropertyClassValue>(); try { objectUuid = entity.getUuid(); // pull-out sync-network wide change id for the sync *record* (not the entity itself), // if one was already assigned (i.e. this change is coming from some other server) originalRecordUuid = getSyncRecord().getOriginalUuid(); if (log.isDebugEnabled()) { // build up a starting msg for all logging: StringBuilder sb = new StringBuilder(); sb.append("In PackageObject, entity type:"); sb.append(entity.getClass().getName()); sb.append(", entity uuid:"); sb.append(objectUuid); sb.append(", originalUuid uuid:"); sb.append(originalRecordUuid); log.debug(sb.toString()); } // Transient properties are not serialized. transientProps = new HashSet<String>(); for (Field f : entity.getClass().getDeclaredFields()) { if (Modifier.isTransient(f.getModifiers())) { transientProps.add(f.getName()); if (log.isDebugEnabled()) log.debug("The field " + f.getName() + " is transient - so we won't serialize it"); } } /* * Retrieve metadata for this type; we need to determine what is the * PK field for this type. We need to know this since PK values are * *not* journalled; values of primary keys are assigned where * physical DB records are created. This is so to avoid issues with * id collisions. * * In case of <generator class="assigned" />, the Identifier * property is already assigned value and needs to be journalled. * Also, the prop will *not* be part of currentState,thus we need to * pull it out with reflection/metadata. */ data = getSessionFactory().getClassMetadata(entity.getClass()); if (data.hasIdentifierProperty()) { idPropertyName = data.getIdentifierPropertyName(); idPropertyObj = ((org.hibernate.persister.entity.AbstractEntityPersister) data).getEntityMetamodel() .getIdentifierProperty(); if (id != null && idPropertyObj.getIdentifierGenerator() != null && (idPropertyObj.getIdentifierGenerator() instanceof org.hibernate.id.Assigned // || idPropertyObj.getIdentifierGenerator() instanceof org.openmrs.api.db.hibernate.NativeIfNotAssignedIdentityGenerator )) { // serialize value as string values.put(idPropertyName, new PropertyClassValue(id.getClass().getName(), id.toString())); } } else if (data.getIdentifierType() instanceof EmbeddedComponentType) { // if we have a component identifier type (like AlertRecipient), // make // sure we include those properties EmbeddedComponentType type = (EmbeddedComponentType) data.getIdentifierType(); for (int i = 0; i < type.getPropertyNames().length; i++) { String propertyName = type.getPropertyNames()[i]; Object propertyValue = type.getPropertyValue(entity, i, org.hibernate.EntityMode.POJO); addProperty(values, entity, type.getSubtypes()[i], propertyName, propertyValue, infoMsg); } } /* * Loop through all the properties/values and put in a hash for * duplicate removal */ for (int i = 0; i < types.length; i++) { String typeName = types[i].getName(); if (log.isDebugEnabled()) log.debug("Processing, type: " + typeName + " Field: " + propertyNames[i]); if (propertyNames[i].equals(idPropertyName) && log.isInfoEnabled()) log.debug(infoMsg + ", Id for this class: " + idPropertyName + " , value:" + currentState[i]); if (currentState[i] != null) { // is this the primary key or transient? if so, we don't // want to serialize if (propertyNames[i].equals(idPropertyName) || ("personId".equals(idPropertyName) && "patientId".equals(propertyNames[i])) //|| ("personId".equals(idPropertyName) && "userId".equals(propertyNames[i])) || transientProps.contains(propertyNames[i])) { // if (log.isInfoEnabled()) log.debug("Skipping property (" + propertyNames[i] + ") because it's either the primary key or it's transient."); } else { addProperty(values, entity, types[i], propertyNames[i], currentState[i], infoMsg); } } else { // current state null -- skip if (log.isDebugEnabled()) log.debug("Field Type: " + typeName + " Field Name: " + propertyNames[i] + " is null, skipped"); } } /* * Now serialize the data identified and put in the value-map */ // Setup the serialization data structures to hold the state Package pkg = new Package(); String className = entity.getClass().getName(); Record xml = pkg.createRecordForWrite(className); Item entityItem = xml.getRootItem(); // loop through the map of the properties that need to be serialized for (Map.Entry<String, PropertyClassValue> me : values.entrySet()) { String property = me.getKey(); // if we are processing onDelete event all we need is uuid if ((state == SyncItemState.DELETED) && (!"uuid".equals(property))) { continue; } try { PropertyClassValue pcv = me.getValue(); appendRecord(xml, entity, entityItem, property, pcv.getClazz(), pcv.getValue()); } catch (Exception e) { String msg = "Could not append attribute. Error while processing property: " + property + " - " + e.getMessage(); throw (new SyncException(msg, e)); } } values.clear(); // Be nice to GC if (objectUuid == null) throw new SyncException("uuid is null for: " + className + " with id: " + id); /* * Create SyncItem and store change in SyncRecord kept in * ThreadLocal. */ SyncItem syncItem = new SyncItem(); syncItem.setKey(new SyncItemKey<String>(objectUuid, String.class)); syncItem.setState(state); syncItem.setContent(xml.toStringAsDocumentFragement()); syncItem.setContainedType(entity.getClass()); if (log.isDebugEnabled()) log.debug("Adding SyncItem to SyncRecord"); getSyncRecord().addItem(syncItem); getSyncRecord().addContainedClass(entity.getClass().getName()); // set the originating uuid for the record: do this once per Tx; // else we may end up with empty string if (getSyncRecord().getOriginalUuid() == null || "".equals(getSyncRecord().getOriginalUuid())) { getSyncRecord().setOriginalUuid(originalRecordUuid); } } catch (SyncException ex) { log.error("Journal error\n", ex); throw (ex); } catch (Exception e) { log.error("Journal error\n", e); throw (new SyncException("Error in interceptor, see log messages and callstack.", e)); } return; }