Example usage for org.hibernate.metadata ClassMetadata getPropertyValues

List of usage examples for org.hibernate.metadata ClassMetadata getPropertyValues

Introduction

In this page you can find the example usage for org.hibernate.metadata ClassMetadata getPropertyValues.

Prototype

Object[] getPropertyValues(Object entity) throws HibernateException;

Source Link

Document

Extract the property values from the given entity.

Usage

From source file:org.cgiar.ccafs.marlo.data.HibernateAuditLogListener.java

License:Open Source License

public Set<HashMap<String, Object>> relations(Object[] state, Type[] types, Object id, boolean firstRelations,
        Session session, Object object) {

    Set<HashMap<String, Object>> relations = new HashSet<>();
    int i = 0;//from  w  ww .j a va2s. co m
    String parentId = "";
    try {
        parentId = id.toString();
    } catch (Exception e1) {
        e1.printStackTrace();
        parentId = "";
    }
    /**
     * We load and refresh the object to get the relations updated.
     * Christian Garcia
     */

    for (Type type : types) {
        HashMap<String, Object> objects = new HashMap<>();

        if (type instanceof OneToOneType) {

            IAuditLog audit = (IAuditLog) state[i];
            if (audit != null) {
                String name = audit.getClass().getName();
                Class<?> className;
                try {
                    className = Class.forName(name);

                    Object obj = session.get(className, audit.getId());

                    Set<IAuditLog> listRelation = new HashSet<>();
                    listRelation.add((IAuditLog) obj);
                    IAuditLog auditlog = (IAuditLog) obj;
                    auditlog = (IAuditLog) session.get(auditlog.getClass(), auditlog.getId());
                    // session.refresh(auditlog);
                    Set<HashMap<String, Object>> loadList = this.loadListOfRelations(auditlog, session);
                    for (HashMap<String, Object> hashMap : loadList) {
                        HashSet<IAuditLog> relationAudit = (HashSet<IAuditLog>) hashMap.get(IAuditLog.ENTITY);
                        for (IAuditLog iAuditLog2 : relationAudit) {
                            Set<HashMap<String, Object>> loadListRelations = this
                                    .loadListOfRelations(iAuditLog2, session);

                            relations.addAll(loadListRelations);
                        }
                    }

                    relations.addAll(loadList);

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }

        if (type instanceof OrderedSetType || type instanceof SetType) {

            if (AuditLogContextProvider.getAuditLogContext().getRelationsNames().contains(type.getName())) {
                Set<IAuditLog> listRelation = new HashSet<>();
                try {

                    String fieldSet = type.getName()
                            .replaceAll("java.util.Set\\(" + object.getClass().getName() + ".", "")
                            .replaceAll("\\)", "");
                    Field privateField = object.getClass().getDeclaredField(fieldSet);
                    privateField.setAccessible(true);
                    Set<Object> set = (Set<Object>) privateField.get(object);
                    // Hibernate.initialize(set);
                    if (set != null && !set.isEmpty()) {
                        Object reObject = session.get(
                                AuditLogContextProvider.getAuditLogContext().getEntityCanonicalName(),
                                (Serializable) id);
                        // session.refresh(reObject);
                        ClassMetadata metadata = session.getSessionFactory()
                                .getClassMetadata(reObject.getClass());
                        Object[] values = metadata.getPropertyValues(reObject);
                        set = (Set<Object>) values[i];
                        for (Object iAuditLog : set) {
                            if (iAuditLog instanceof IAuditLog) {
                                IAuditLog audit = (IAuditLog) iAuditLog;
                                if (audit.isActive()) {
                                    try {
                                        String name = audit.getClass().getName();
                                        Class<?> className = Class.forName(name);

                                        /**
                                         * Ensure that lazy loaded collections are at least proxy instances (if not fetched).
                                         * If you are not getting all collections auditLogged it is because the Action classes are
                                         * persisting the detached entity and not copying across the managed collections (e.g. the ones
                                         * in our entities that are generally using Set). To overcome refactor the Action classes to copy
                                         * the List collections, which are full of detached entities, see OutcomeAction save method for an
                                         * example.
                                         * When issue #1124 is solved, this won't be a problem.
                                         */
                                        /**
                                         * We load the object to get the id assigned
                                         * Christian Garcia
                                         */

                                        Object obj = session.get(className, audit.getId());

                                        if (obj == null) {

                                            /**
                                             * This is likely to be that the entity has just been hard deleted (MARLO has a mixture of
                                             * entities
                                             * where some are soft deleted and others are hard deleted).
                                             */
                                            LOG.info("IAuditLog obj with className: " + className + ", and id: "
                                                    + audit.getId() + " can not be found");
                                            // Add the audit from the state object array
                                            listRelation.add(audit);
                                            continue;
                                        } else {

                                            ClassMetadata classMetadataObj = session.getSessionFactory()
                                                    .getClassMetadata(obj.getClass());
                                            String[] propertyNamesRelation = classMetadataObj
                                                    .getPropertyNames();
                                            Phase phaseObject = null;
                                            boolean hasPhase = false;
                                            for (String nameAtrribute : propertyNamesRelation) {
                                                if (nameAtrribute.equals("phase")) {
                                                    phaseObject = (Phase) classMetadataObj.getPropertyValue(obj,
                                                            nameAtrribute);
                                                    if (phaseObject != null) {
                                                        phaseObject = (Phase) session.get(Phase.class,
                                                                phaseObject.getId());
                                                        if (phaseObject != null) {
                                                            hasPhase = true;
                                                        }
                                                    }

                                                }
                                            }
                                            /*
                                             * if have phase and the phase is the current we are checking , we load the info
                                             */
                                            if (hasPhase) {
                                                if (AuditLogContextProvider.getAuditLogContext().getPhase()
                                                        .equals(phaseObject)) {
                                                    listRelation.add((IAuditLog) obj);
                                                    IAuditLog auditlog = (IAuditLog) obj;
                                                    auditlog = (IAuditLog) session.get(auditlog.getClass(),
                                                            auditlog.getId());

                                                    // session.refresh(auditlog);
                                                    Set<HashMap<String, Object>> loadList = this
                                                            .loadListOfRelations(auditlog, session);
                                                    for (HashMap<String, Object> hashMap : loadList) {
                                                        HashSet<IAuditLog> relationAudit = (HashSet<IAuditLog>) hashMap
                                                                .get(IAuditLog.ENTITY);
                                                        for (IAuditLog iAuditLog2 : relationAudit) {
                                                            Set<HashMap<String, Object>> loadListRelations = this
                                                                    .loadListOfRelations(iAuditLog2, session);

                                                            relations.addAll(loadListRelations);
                                                        }
                                                    }

                                                    relations.addAll(loadList);
                                                }

                                            } else {
                                                /*
                                                 * If doesn't have phase we alway load the info
                                                 */
                                                listRelation.add((IAuditLog) obj);
                                                IAuditLog auditlog = (IAuditLog) obj;
                                                auditlog = (IAuditLog) session.get(auditlog.getClass(),
                                                        auditlog.getId());
                                                // session.refresh(auditlog);
                                                Set<HashMap<String, Object>> loadList = this
                                                        .loadListOfRelations(auditlog, session);
                                                for (HashMap<String, Object> hashMap : loadList) {
                                                    HashSet<IAuditLog> relationAudit = (HashSet<IAuditLog>) hashMap
                                                            .get(IAuditLog.ENTITY);
                                                    for (IAuditLog iAuditLog2 : relationAudit) {
                                                        Set<HashMap<String, Object>> loadListRelations = this
                                                                .loadListOfRelations(iAuditLog2, session);

                                                        relations.addAll(loadListRelations);
                                                    }
                                                }

                                                relations.addAll(loadList);
                                            }
                                        }

                                    } catch (ClassNotFoundException e) {

                                        LOG.error(e.getLocalizedMessage());
                                    }
                                }

                            }

                        }
                        if (!listRelation.isEmpty()) {
                            objects.put(IAuditLog.ENTITY, listRelation);
                            objects.put(IAuditLog.PRINCIPAL, "3");
                            objects.put(IAuditLog.RELATION_NAME, type.getName() + ":" + parentId);
                            relations.add(objects);
                        }

                    }
                } catch (HibernateException e) {
                    e.printStackTrace();
                    LOG.info("Can not load lazy relation  " + e.getLocalizedMessage());
                } catch (IllegalArgumentException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IllegalAccessException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (NoSuchFieldException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (SecurityException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }

        }
        i++;
    }

    return relations;
}