/*
(non-Javadoc)

@see
com.impetus.kundera.query.QueryImpl#populateEntities(com.impetus.kundera
.metadata.model.EntityMetadata, com.impetus.kundera.client.Client)
493165
protected List<Object> populateEntities(EntityMetadata m, Client client)
    {
        if (log.isDebugEnabled())
        {
            log.debug("on start of fetching non associated entities");
        }
        List<Object> result = new ArrayList<Object>();

        initializeReader();

        try
        {
            if (MetadataUtils.useSecondryIndex(((ClientBase) client).getClientMetadata()))
            {
                List<String> relations = new ArrayList<String>();
                if (kunderaQuery.isNative())
                {
                    return ((HibernateClient) client).find(getJPAQuery(), relations, m);

                }
                else
                {
                    return ((HibernateClient) client).findByQuery(getJPAQuery(), getParamaters(), firstResult, maxResult);

                }
            }
            else
            {
                return populateUsingLucene(m, client, result, null);
            }
        }
        catch (Exception e)
        {
            log.error("Error during query execution, Caused by: {}.", e);
            throw new QueryHandlerException(e);
        }
    }
1168636
public void populate(Entity entity) {
    // auto date
    generateAutoDateOrDateTime(singletonList(entity), entity.getEntityType().getAttributes());

    // auto id
    Attribute idAttr = entity.getEntityType().getIdAttribute();
    if (idAttr != null
        && idAttr.isAuto()
        && entity.getIdValue() == null
        && (idAttr.getDataType() == STRING)) {
      entity.set(idAttr.getName(), idGenerator.generateId());
    }
  }
812433
private void populateGroupByResponse(Map<String, Object> aggregations, Iterable<Expression> resultOrderIterable,
            List<Object> results, Client client, EntityMetadata m)
    {
        List temp;
        Object entity = null;
        for (String entry : aggregations.keySet())
        {
            entity = null;
            Object obj = aggregations.get(entry);
            temp = new ArrayList<>();
            Iterator<Expression> resultOrder = resultOrderIterable.iterator();
            while (resultOrder.hasNext())
            {
                Expression expression = (Expression) resultOrder.next();

                if (AggregateFunction.class.isAssignableFrom(expression.getClass()))
                {
                    if (((Map) obj).get(expression.toParsedText()) != null)
                    {
                        temp.add(((Map) obj).get(expression.toParsedText()));
                    }
                }
                else
                {
                    if (entity == null)
                    {
                        entity = findUsingLucene(m, client, new Object[] { entry }).get(0);
                    }
                    temp.add(getEntityFieldValue(m, entity, expression.toParsedText()));
                }
            }
            results.add(temp.size() == 1 ? temp.get(0) : temp);
        }
    }
1173457
@Override
    public void process(Class clazz, EntityMetadata metadata)
    {

        if (LOG.isDebugEnabled())
            LOG.debug("Processing @Entity(" + clazz.getName() + ") for Persistence Object.");
        populateMetadata(metadata, clazz, puProperties);
    }
622197
private List<EnhanceEntity> populateEnhanceEntities(EntityMetadata m, List<String> relationNames, Client client,
            String sqlQuery)
    {
        List<EnhanceEntity> ls = null;
        List result = ((HibernateClient) client).find(sqlQuery, relationNames, m);

        if (!result.isEmpty())
        {
            ls = new ArrayList<EnhanceEntity>(result.size());
            for (Object o : result)
            {
                EnhanceEntity entity = null;
                if (!o.getClass().isAssignableFrom(EnhanceEntity.class))
                {
                    entity = new EnhanceEntity(o, getId(o, m), null);
                }
                else
                {
                    entity = (EnhanceEntity) o;
                }
                ls.add(entity);
            }
        }
        return ls;
    }
509330
protected final void populateIdAccessorMethods(EntityMetadata metadata, Class<?> clazz, Field f)
    {
        try
        {
            BeanInfo info = Introspector.getBeanInfo(clazz);

            for (PropertyDescriptor descriptor : info.getPropertyDescriptors())
            {
                if (descriptor.getName().equals(f.getName()))
                {
                    metadata.setReadIdentifierMethod(descriptor.getReadMethod());
                    metadata.setWriteIdentifierMethod(descriptor.getWriteMethod());
                    return;
                }
            }
        }
        catch (IntrospectionException e)
        {
            throw new RuntimeException(e);
        }
    }
149349
protected List fetch()
    {
        EntityMetadata metadata = getEntityMetadata();
        Client client = persistenceDelegeator.getClient(metadata);
        List results = isRelational(metadata) ? recursivelyPopulateEntities(metadata, client) : populateEntities(
                metadata, client);
        return results;
    }
1201066
@Override
    public List<EnhanceEntity> populateRelation(EntityMetadata m, Client client, int maxResults)
    {
        // TODO Auto-generated method stub
        return null;
    }
70549
public List<?> loadDataAndPopulateResults(DataFrame dataFrame, EntityMetadata m, KunderaQuery kunderaQuery)
    {
        if (kunderaQuery != null && kunderaQuery.isAggregated())
        {
            return dataFrame.collectAsList();
        }
        // TODO: handle the case of specific field selection
        else
        {
            return populateEntityObjectsList(dataFrame, m);
        }
    }
157215
public void saveEntities(Set<String> entityIds, boolean clearOnSuccess, Command callback) {

        List<CmsEntity> entities = new ArrayList<CmsEntity>();
        for (String entityId : entityIds) {
            CmsEntity entity = m_entityBackend.getEntity(entityId);
            if (entity != null) {
                entities.add(entity);
            }
        }
        saveEntities(entities, clearOnSuccess, callback);
    }
265417
@SuppressWarnings("unchecked")
    @Override
    protected List<Object> recursivelyPopulateEntities(EntityMetadata m, Client client) {
        List<EnhanceEntity> ls = null;
        ApplicationMetadata appMetadata = kunderaMetadata.getApplicationMetadata();
        externalProperties = ((CassandraClientBase) client).getExternalProperties();
        MetamodelImpl metaModel =
            (MetamodelImpl) kunderaMetadata.getApplicationMetadata().getMetamodel(m.getPersistenceUnit());

        String query = appMetadata.getQuery(getJPAQuery());
        boolean isNative = kunderaQuery.isNative();

        if (isNative) {
            ls = (List<EnhanceEntity>) ((CassandraClientBase) client).executeQuery(m.getEntityClazz(),
                m.getRelationNames(), isNative, query != null ? query : getJPAQuery());
        } else if (!isNative && ((CassandraClientBase) client).isCql3Enabled(m)) {
            // edited
            // check if lucene or indexer are enabled then populate
            if (MetadataUtils.useSecondryIndex(((ClientBase) client).getClientMetadata())) {
                ls = ((CassandraClientBase) client).executeQuery(m.getEntityClazz(), m.getRelationNames(), isNative,

                    onQueryOverCQL3(m, client, metaModel, m.getRelationNames()));
            } else {
                ls = populateUsingLucene(m, client, null, getKunderaQuery().getResult());
            }
        } else {
            // Index in Inverted Index table if applicable
            boolean useInvertedIndex = CassandraIndexHelper.isInvertedIndexingApplicable(m,
                MetadataUtils.useSecondryIndex(((ClientBase) client).getClientMetadata()));
            Map<Boolean, List<IndexClause>> ixClause =
                MetadataUtils.useSecondryIndex(((ClientBase) client).getClientMetadata())
                    ? prepareIndexClause(m, useInvertedIndex) : null;

            if (useInvertedIndex && !getKunderaQuery().getFilterClauseQueue().isEmpty()) {
                ls = ((CassandraEntityReader) getReader()).readFromIndexTable(m, client, ixClause);
            } else {
                ((CassandraEntityReader) getReader()).setConditions(ixClause);
                ls = reader.populateRelation(m, client, isSingleResult ? 1 : this.maxResult);
            }
        }
        return setRelationEntities(ls, client, m);
    }
1331357
@Override
    public CreateUpdateEntitiesResult updateEntities(ITypedReferenceableInstance[] entityInstanceDefinitions) throws AtlasException {
        CreateUpdateEntitiesResult result = repository.updateEntities(entityInstanceDefinitions);
        onEntitiesAddedUpdated(result.getEntityResult());
        return result;
    }
1069887
@Override
    protected List<Object> recursivelyPopulateEntities(EntityMetadata m, Client client)
    {
        List result = populateEntities(m, client);
        return setRelationEntities(result, client, m);
    }
1051807
@Override
    protected List<Object> recursivelyPopulateEntities(EntityMetadata m, Client client)
    {
        List<EnhanceEntity> ls = new ArrayList<EnhanceEntity>();
        RedisQueryInterpreter interpreter = onTranslation(getKunderaQuery().getFilterClauseQueue(), m);
        ls = ((RedisClient) client).onExecuteQuery(interpreter, m.getEntityClazz());
        return setRelationEntities(ls, client, m);
    }
1072653
private void populateId(EntityMetadata entityMetadata, Table schemaTable, Object entity, Row row)
    {
        FieldDef fieldMetadata;
        FieldValue value;
        String idColumnName = ((AbstractAttribute) entityMetadata.getIdAttribute()).getJPAColumnName();

        fieldMetadata = schemaTable.getField(idColumnName);

        value = row.get(idColumnName);

        NoSqlDBUtils.get(fieldMetadata, value, entity, (Field) entityMetadata.getIdAttribute().getJavaMember());
    }
496938
@Override
    protected List populateEntities(EntityMetadata m, Client client)
    {
        if (kunderaQuery.isNative())
        {
            return ((CouchbaseClient) client).executeNativeQuery(getJPAQuery(), m);
        }
        else
        {
            MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
                    .getMetamodel(m.getPersistenceUnit());
            EntityType entityType = metaModel.entity(m.getEntityClazz());
            JPQLExpression jpqlExp = kunderaQuery.getJpqlExpression();
            List<String> selectColumns = KunderaQueryUtils.getSelectColumns(jpqlExp);
            AsPath asPath;
            if (selectColumns.isEmpty())
            {
                asPath = select(CouchbaseConstants.STAR).from(i(m.getSchema()));
            }
            else
            {
                asPath = select(selectColumns.toArray(new String[selectColumns.size()])).from(i(m.getSchema()));
            }
            Statement statement;

            if (KunderaQueryUtils.hasWhereClause(jpqlExp))
            {
                WhereClause whereClause = KunderaQueryUtils.getWhereClause(jpqlExp);
                Expression whereExp = whereClause.getConditionalExpression();

                if (whereExp instanceof ComparisonExpression)
                {
                    String left = ((ComparisonExpression) whereExp).getLeftExpression().toActualText();
                    String right = ((ComparisonExpression) whereExp).getRightExpression().toActualText();
                    if (right.startsWith(CouchbaseConstants.POSITIONAL_PREFIX)
                            || right.startsWith(CouchbaseConstants.PARAMETERIZED_PREFIX))
                    {
                        right = kunderaQuery.getParametersMap().get(right) + "";
                    }
                    Attribute attribute = entityType.getAttribute(left.split(CouchbaseConstants.DOT_REGEX)[1]);

                    statement = addWhereCondition(asPath, ((ComparisonExpression) whereExp).getActualIdentifier(),
                            ((AbstractAttribute) attribute).getJPAColumnName(), right, m.getTableName());
                }

            }
            else
            {
                asPath.where(x(CouchbaseConstants.KUNDERA_ENTITY).eq(x("'" + m.getTableName() + "'")));
            }

            statement = asPath;
            return ((CouchbaseClient) client).executeQuery(statement, m);
        }
    }
315744
public void setEntities(java.util.Collection<InventoryResultEntity> entities) {
        if (entities == null) {
            this.entities = null;
            return;
        }

        this.entities = new com.amazonaws.internal.SdkInternalList<InventoryResultEntity>(entities);
    }