<commit_before>package ch.puzzle.itc.mobiliar.business.property.control;

import ch.puzzle.itc.mobiliar.business.auditview.control.GenericAuditHandler;
import ch.puzzle.itc.mobiliar.business.auditview.entity.AuditViewEntry;
import ch.puzzle.itc.mobiliar.business.auditview.entity.AuditViewEntryContainer;
import ch.puzzle.itc.mobiliar.business.resourcegroup.entity.ResourceContextEntity;
import org.hibernate.envers.AuditReader;
import org.hibernate.envers.AuditReaderFactory;
import org.hibernate.envers.query.AuditEntity;

import javax.ejb.Stateless;
import javax.inject.Named;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import java.math.BigDecimal;

@Stateless
@Named("propertyEntityAuditviewHandler")
public class PropertyEntityAuditviewHandler extends GenericAuditHandler {
    private static final String SELECT_FOR_RESOURCE = "SELECT TAMW_RESOURCECONTEXT_ID " +
            "FROM TAMW_RESOURCECTX_PROP " +
            "WHERE PROPERTIES_ID = :propertyId";
    private static final String SELECT_FOR_RESOURCE_FROM_AUDIT = "SELECT TAMW_RESOURCECONTEXT_ID " +
            "FROM TAMW_RESOURCECTX_PROP_AUD " +
            "WHERE rev >= :rev " +
            "AND PROPERTIES_ID = :propertyId";
    private static final String SELECT_FOR_PROP_ON_RESOURCE = String.format("%s UNION %s", SELECT_FOR_RESOURCE, SELECT_FOR_RESOURCE_FROM_AUDIT);

    @Override
    public AuditViewEntry createAuditViewEntry(AuditViewEntryContainer auditViewEntryContainer) {
        ResourceContextEntity resourceContextEntity = getResourceContextEntityForPropertyOnMasterResource(auditViewEntryContainer);
        boolean isPropertyOnMasterResource = resourceContextEntity != null;
        if (isPropertyOnMasterResource) {
            auditViewEntryContainer.setEditContextId(resourceContextEntity.getId());
        } else {
            // property is on related resource (provided/consumed)
            setRelationNameAndContextForPropertyOfRelatedResource(auditViewEntryContainer);
        }
        return super.createAuditViewEntry(auditViewEntryContainer);
    }

    private void setRelationNameAndContextForPropertyOfRelatedResource(AuditViewEntryContainer auditViewEntryContainer) {
        try {
            setRelationNameAndContextForPropertyOfConsumedResource(auditViewEntryContainer);
        } catch (NoResultException e) {
            setRelationNameAndContextForPropertyOfProvidedResource(auditViewEntryContainer);
        }
    }

    private void setRelationNameAndContextForPropertyOfConsumedResource(AuditViewEntryContainer container) {
        String selectNameAndContext =
                " SELECT " +
                        "consumed_resource.NAME || " +
                        "  CASE WHEN consumed_resource_relation.IDENTIFIER IS NOT NULL " +
                        "    THEN ' (' || consumed_resource_relation.IDENTIFIER || ')' " +
                        "    ELSE '' " +
                        "  END, " +
                        "resource_relation_context.CONTEXT_ID " +
                        " FROM TAMW_RESOURCE consumed_resource " +
                        " JOIN TAMW_CONSUMEDRESREL consumed_resource_relation " +
                        "     ON consumed_resource_relation.SLAVERESOURCE_ID = consumed_resource.ID " +
                        " JOIN TAMW_RESRELCONTEXT resource_relation_context " +
                        "     ON consumed_resource_relation.ID = resource_relation_context.CONSUMEDRESOURCERELATION_ID " +
                        " JOIN TAMW_RESRELCTX_PROP resource_relation_context_prop " +
                        "     ON resource_relation_context_prop.TAMW_RESRELCONTEXT_ID = resource_relation_context.ID " +
                        " WHERE resource_relation_context_prop.PROPERTIES_ID = :propertyId";
        executeRelatedResourceQueryAndEnrichAuditViewContainer(container, selectNameAndContext, null, AuditViewEntry.RELATION_CONSUMED_RESOURCE);
    }

    private void setRelationNameAndContextForPropertyOfProvidedResource(AuditViewEntryContainer container) {
        String selectNameAndContext =
                " SELECT provided_resource.NAME, resource_relation_context.CONTEXT_ID " +
                        " FROM TAMW_RESOURCE provided_resource " +
                        " JOIN TAMW_PROVIDEDRESREL provided_resource_relation " +
                        "     ON provided_resource_relation.SLAVERESOURCE_ID = provided_resource.ID " +
                        " JOIN TAMW_RESRELCONTEXT resource_relation_context " +
                        "     ON provided_resource_relation.ID = resource_relation_context.PROVIDEDRESOURCERELATION_ID " +
                        " JOIN TAMW_RESRELCTX_PROP resource_relation_context_prop " +
                        "     ON resource_relation_context_prop.TAMW_RESRELCONTEXT_ID = resource_relation_context.ID " +
                        " WHERE resource_relation_context_prop.PROPERTIES_ID = :propertyId";
//        String selectNameAndContextInAuditLog =
//                " SELECT provided_resource.NAME, resource_relation_context.CONTEXT_ID " +
//                        " FROM TAMW_RESOURCE_AUD provided_resource " +
//                        " JOIN TAMW_PROVIDEDRESREL_AUD provided_resource_relation " +
//                        "     ON provided_resource_relation.SLAVERESOURCE_ID = provided_resource.ID " +
//                        " JOIN TAMW_RESRELCONTEXT_AUD resource_relation_context " +
//                        "     ON provided_resource_relation.ID = resource_relation_context.PROVIDEDRESOURCERELATION_ID " +
//                        " JOIN TAMW_RESRELCTX_PROP_AUD resource_relation_context_prop " +
//                        "     ON resource_relation_context_prop.TAMW_RESRELCONTEXT_ID = resource_relation_context.ID " +
//                        " WHERE resource_relation_context_prop.PROPERTIES_ID = :propertyId " +
//                        " AND ROWNUM = 1" +
//                        " ORDER BY provided_resource_relation.REV DESC";
        executeRelatedResourceQueryAndEnrichAuditViewContainer(container, selectNameAndContext, selectNameAndContext, AuditViewEntry.RELATION_PROVIDED_RESOURCE);
    }

    private void executeRelatedResourceQueryAndEnrichAuditViewContainer(AuditViewEntryContainer container, String select, String selectInAuditLog, String relationName) {
        Object[] nameAndId;
        try {
            nameAndId = (Object[]) entityManager
                    .createNativeQuery(select)
                    .setParameter("propertyId", container.getEntityForRevision().getId())
                    .getSingleResult();
        } catch (NoResultException e) {
            nameAndId = (Object[]) entityManager
                    .createNativeQuery(selectInAuditLog)
                    .setParameter("propertyId", container.getEntityForRevision().getId())
                    .getSingleResult();
        }
        String name = (String) nameAndId[0];
        int resourceContextId = ((BigDecimal) nameAndId[1]).intValue();
        container.setRelationName(String.format("%s: %s", relationName, name));
        container.setEditContextId(resourceContextId);
    }

    public ResourceContextEntity getResourceContextEntityForPropertyOnMasterResource(AuditViewEntryContainer container) {
        try {

            Query query = entityManager
                    .createNativeQuery(SELECT_FOR_PROP_ON_RESOURCE)
                    .setParameter("rev", container.getRevEntity().getId())
                    .setParameter("propertyId", container.getEntityForRevision().getId());
            BigDecimal resourceContextId = (BigDecimal) query.getSingleResult();
            AuditReader reader = AuditReaderFactory.get(entityManager);
            return (ResourceContextEntity) reader.createQuery()
                    .forRevisionsOfEntity(ResourceContextEntity.class, true, true)
                    .add(AuditEntity.id().eq(resourceContextId.intValue()))
                    .setMaxResults(1)
                    .getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }
}
<commit_msg>improved readability in PropertyEntityAuditviewHandler for properties on provided resource<commit_after>package ch.puzzle.itc.mobiliar.business.property.control;

import ch.puzzle.itc.mobiliar.business.auditview.control.GenericAuditHandler;
import ch.puzzle.itc.mobiliar.business.auditview.entity.AuditViewEntry;
import ch.puzzle.itc.mobiliar.business.auditview.entity.AuditViewEntryContainer;
import ch.puzzle.itc.mobiliar.business.resourcegroup.entity.ResourceContextEntity;
import ch.puzzle.itc.mobiliar.common.util.Tuple;
import org.hibernate.envers.AuditReader;
import org.hibernate.envers.AuditReaderFactory;
import org.hibernate.envers.query.AuditEntity;

import javax.ejb.Stateless;
import javax.inject.Named;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import java.math.BigDecimal;

@Stateless
@Named("propertyEntityAuditviewHandler")
public class PropertyEntityAuditviewHandler extends GenericAuditHandler {
    // Property on Master Resource
    private static final String SELECT_FOR_RESOURCE = "SELECT TAMW_RESOURCECONTEXT_ID " +
            "FROM TAMW_RESOURCECTX_PROP " +
            "WHERE PROPERTIES_ID = :propertyId";
    private static final String SELECT_FOR_RESOURCE_FROM_AUDIT = "SELECT TAMW_RESOURCECONTEXT_ID " +
            "FROM TAMW_RESOURCECTX_PROP_AUD " +
            "WHERE rev >= :rev " +
            "AND PROPERTIES_ID = :propertyId";
    private static final String SELECT_FOR_PROP_ON_RESOURCE = String.format("%s UNION %s", SELECT_FOR_RESOURCE, SELECT_FOR_RESOURCE_FROM_AUDIT);

    // Property on Consumed Resource
    private static final String SELECT_NAME_AND_CONTEXT_FOR_PROPERTY_ON_CONSUMED_RESOURCE =
            " SELECT " +
                    " consumed_resource.NAME || " +
                    "  CASE WHEN consumed_resource_relation.IDENTIFIER IS NOT NULL " +
                    "    THEN ' (' || consumed_resource_relation.IDENTIFIER || ')' " +
                    "    ELSE '' " +
                    "  END, " +
                    " resource_relation_context.CONTEXT_ID " +
                    " FROM TAMW_RESOURCE consumed_resource " +
                    " JOIN TAMW_CONSUMEDRESREL consumed_resource_relation " +
                    "     ON consumed_resource_relation.SLAVERESOURCE_ID = consumed_resource.ID " +
                    " JOIN TAMW_RESRELCONTEXT resource_relation_context " +
                    "     ON consumed_resource_relation.ID = resource_relation_context.CONSUMEDRESOURCERELATION_ID " +
                    " JOIN TAMW_RESRELCTX_PROP resource_relation_context_prop " +
                    "     ON resource_relation_context_prop.TAMW_RESRELCONTEXT_ID = resource_relation_context.ID " +
                    " WHERE resource_relation_context_prop.PROPERTIES_ID = :propertyId";

    private static final String SELECT_NAME_AND_CONTEXT_FOR_PROPERTY_ON_PROVIDED_RESOURCE =
            " SELECT provided_resource.NAME, resource_relation_context.CONTEXT_ID " +
                    " FROM TAMW_RESOURCE provided_resource " +
                    " JOIN TAMW_PROVIDEDRESREL provided_resource_relation " +
                    "     ON provided_resource_relation.SLAVERESOURCE_ID = provided_resource.ID " +
                    " JOIN TAMW_RESRELCONTEXT resource_relation_context " +
                    "     ON provided_resource_relation.ID = resource_relation_context.PROVIDEDRESOURCERELATION_ID " +
                    " JOIN TAMW_RESRELCTX_PROP resource_relation_context_prop " +
                    "     ON resource_relation_context_prop.TAMW_RESRELCONTEXT_ID = resource_relation_context.ID " +
                    " WHERE resource_relation_context_prop.PROPERTIES_ID = :propertyId";

//        String selectNameAndContextInAuditLog =
//                " SELECT provided_resource.NAME, resource_relation_context.CONTEXT_ID " +
//                        " FROM TAMW_RESOURCE_AUD provided_resource " +
//                        " JOIN TAMW_PROVIDEDRESREL_AUD provided_resource_relation " +
//                        "     ON provided_resource_relation.SLAVERESOURCE_ID = provided_resource.ID " +
//                        " JOIN TAMW_RESRELCONTEXT_AUD resource_relation_context " +
//                        "     ON provided_resource_relation.ID = resource_relation_context.PROVIDEDRESOURCERELATION_ID " +
//                        " JOIN TAMW_RESRELCTX_PROP_AUD resource_relation_context_prop " +
//                        "     ON resource_relation_context_prop.TAMW_RESRELCONTEXT_ID = resource_relation_context.ID " +
//                        " WHERE resource_relation_context_prop.PROPERTIES_ID = :propertyId " +
//                        " AND ROWNUM = 1" +
//                        " ORDER BY provided_resource_relation.REV DESC";

    @Override
    public AuditViewEntry createAuditViewEntry(AuditViewEntryContainer container) {
        ResourceContextEntity resourceContextEntity = getResourceContextEntityForPropertyOnMasterResource(container);
        boolean isPropertyOnMasterResource = resourceContextEntity != null;
        if (isPropertyOnMasterResource) {
            container.setEditContextId(resourceContextEntity.getId());
        } else {
            // property is on related resource (provided/consumed)
            Tuple<String, Integer> nameAndContext = getNameAndContextOfConsumedResource(container);
            boolean isPropertyOnConsumedResource = nameAndContext != null;
            if (isPropertyOnConsumedResource){
                container.setRelationName(String.format("%s: %s", AuditViewEntry.RELATION_CONSUMED_RESOURCE, nameAndContext.getA()));
                container.setEditContextId(nameAndContext.getB());
            } else {
                // property is on provided resource
                nameAndContext = getNameAndContextOfProvidedResource(container);
                container.setRelationName(String.format("%s: %s", AuditViewEntry.RELATION_PROVIDED_RESOURCE, nameAndContext.getA()));
                container.setEditContextId(nameAndContext.getB());
            }
        }
        return super.createAuditViewEntry(container);
    }

    private Tuple<String, Integer> getNameAndContextOfProvidedResource(AuditViewEntryContainer container) {
        try {
            Object[] nameAndId;
            nameAndId = (Object[]) entityManager
                    .createNativeQuery(SELECT_NAME_AND_CONTEXT_FOR_PROPERTY_ON_PROVIDED_RESOURCE)
                    .setParameter("propertyId", container.getEntityForRevision().getId())
                    .getSingleResult();
            String name = (String) nameAndId[0];
            int resourceContextId = ((BigDecimal) nameAndId[1]).intValue();
            return new Tuple<>(name, resourceContextId);
        } catch (NoResultException e) {
            return null;
        }
    }

    /**
     *
     * @param container
     * @return a Tuple&lt;Name of consumed Resource, ContextId&gt; or null
     */
    private Tuple<String, Integer> getNameAndContextOfConsumedResource(AuditViewEntryContainer container) {
        try {
            Object[] nameAndId;
            nameAndId = (Object[]) entityManager
                    .createNativeQuery(SELECT_NAME_AND_CONTEXT_FOR_PROPERTY_ON_CONSUMED_RESOURCE)
                    .setParameter("propertyId", container.getEntityForRevision().getId())
                    .getSingleResult();
            String name = (String) nameAndId[0];
            int resourceContextId = ((BigDecimal) nameAndId[1]).intValue();
            return new Tuple<>(name, resourceContextId);
        } catch (NoResultException e) {
            return null;
        }
    }

    public ResourceContextEntity getResourceContextEntityForPropertyOnMasterResource(AuditViewEntryContainer container) {
        try {
            Query query = entityManager
                    .createNativeQuery(SELECT_FOR_PROP_ON_RESOURCE)
                    .setParameter("rev", container.getRevEntity().getId())
                    .setParameter("propertyId", container.getEntityForRevision().getId());
            BigDecimal resourceContextId = (BigDecimal) query.getSingleResult();
            AuditReader reader = AuditReaderFactory.get(entityManager);
            return (ResourceContextEntity) reader.createQuery()
                    .forRevisionsOfEntity(ResourceContextEntity.class, true, true)
                    .add(AuditEntity.id().eq(resourceContextId.intValue()))
                    .setMaxResults(1)
                    .getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }
}

{'fraction_non_alphanumeric': 0.0738936988936989, 'fraction_numerical': 0.0006012506012506013, 'mean_word_length': 2.5839258780435252, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6351955', 'n_tokens_mistral': 4446, 'n_tokens_neox': 4267, 'n_words': 661}