Search in sources :

Example 1 with JDBCFetchPlan

use of org.apache.openjpa.persistence.jdbc.JDBCFetchPlan in project syncope by apache.

the class OpenJpaDialect method beginTransaction.

@Override
public Object beginTransaction(final EntityManager entityManager, final TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException {
    OpenJPAEntityManager openJpaEntityManager = getOpenJPAEntityManager(entityManager);
    if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
        // Pass custom isolation level on to OpenJPA's JDBCFetchPlan configuration
        FetchPlan fetchPlan = openJpaEntityManager.getFetchPlan();
        if (fetchPlan instanceof JDBCFetchPlan) {
            IsolationLevel isolation = IsolationLevel.fromConnectionConstant(definition.getIsolationLevel());
            ((JDBCFetchPlan) fetchPlan).setIsolation(isolation);
        }
    }
    entityManager.getTransaction().begin();
    if (!definition.isReadOnly()) {
        // Like with EclipseLink, make sure to start the logic transaction early so that other
        // participants using the connection (such as JdbcTemplate) run in a transaction.
        openJpaEntityManager.beginStore();
    }
    // Custom implementation for OpenJPA savepoint handling
    return new OpenJpaTransactionData(openJpaEntityManager);
}
Also used : OpenJPAEntityManager(org.apache.openjpa.persistence.OpenJPAEntityManager) IsolationLevel(org.apache.openjpa.persistence.jdbc.IsolationLevel) JDBCFetchPlan(org.apache.openjpa.persistence.jdbc.JDBCFetchPlan) FetchPlan(org.apache.openjpa.persistence.FetchPlan) JDBCFetchPlan(org.apache.openjpa.persistence.jdbc.JDBCFetchPlan)

Example 2 with JDBCFetchPlan

use of org.apache.openjpa.persistence.jdbc.JDBCFetchPlan in project oozie by apache.

the class WorkflowsJobGetJPAExecutor method execute.

/* (non-Javadoc)
     * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
     */
@SuppressWarnings("unchecked")
@Override
public WorkflowsInfo execute(EntityManager em) throws JPAExecutorException {
    List<String> orArray = new ArrayList<String>();
    List<String> colArray = new ArrayList<String>();
    List<Object> valArray = new ArrayList<Object>();
    StringBuilder sb = new StringBuilder("");
    String orderBy = DEFAULT_ORDER_BY;
    boolean isStatus = false;
    boolean isAppName = false;
    boolean isUser = false;
    boolean isEnabled = false;
    boolean isId = false;
    int index = 0;
    for (Map.Entry<String, List<String>> entry : filter.entrySet()) {
        String colName = null;
        String colVar = null;
        if (entry.getKey().equals(OozieClient.FILTER_GROUP)) {
            XLog.getLog(getClass()).warn("Filter by 'group' is not supported anymore");
        } else {
            if (entry.getKey().equals(OozieClient.FILTER_STATUS)) {
                List<String> values = filter.get(OozieClient.FILTER_STATUS);
                colName = "status";
                for (int i = 0; i < values.size(); i++) {
                    colVar = "status";
                    colVar = colVar + index;
                    if (!isEnabled && !isStatus) {
                        sb.append(seletStr).append(" where w.statusStr IN (:status" + index);
                        isStatus = true;
                        isEnabled = true;
                    } else {
                        if (isEnabled && !isStatus) {
                            sb.append(" and w.statusStr IN (:status" + index);
                            isStatus = true;
                        } else {
                            if (isStatus) {
                                sb.append(", :status" + index);
                            }
                        }
                    }
                    if (i == values.size() - 1) {
                        sb.append(")");
                    }
                    index++;
                    valArray.add(values.get(i));
                    orArray.add(colName);
                    colArray.add(colVar);
                }
            } else {
                if (entry.getKey().equals(OozieClient.FILTER_NAME)) {
                    List<String> values = filter.get(OozieClient.FILTER_NAME);
                    colName = "appName";
                    for (int i = 0; i < values.size(); i++) {
                        colVar = "appName";
                        colVar = colVar + index;
                        if (!isEnabled && !isAppName) {
                            sb.append(seletStr).append(" where w.appName IN (:appName" + index);
                            isAppName = true;
                            isEnabled = true;
                        } else {
                            if (isEnabled && !isAppName) {
                                sb.append(" and w.appName IN (:appName" + index);
                                isAppName = true;
                            } else {
                                if (isAppName) {
                                    sb.append(", :appName" + index);
                                }
                            }
                        }
                        if (i == values.size() - 1) {
                            sb.append(")");
                        }
                        index++;
                        valArray.add(values.get(i));
                        orArray.add(colName);
                        colArray.add(colVar);
                    }
                } else {
                    if (entry.getKey().equals(OozieClient.FILTER_USER)) {
                        List<String> values = filter.get(OozieClient.FILTER_USER);
                        colName = "user";
                        for (int i = 0; i < values.size(); i++) {
                            colVar = "user";
                            colVar = colVar + index;
                            if (!isEnabled && !isUser) {
                                sb.append(seletStr).append(" where w.user IN (:user" + index);
                                isUser = true;
                                isEnabled = true;
                            } else {
                                if (isEnabled && !isUser) {
                                    sb.append(" and w.user IN (:user" + index);
                                    isUser = true;
                                } else {
                                    if (isUser) {
                                        sb.append(", :user" + index);
                                    }
                                }
                            }
                            if (i == values.size() - 1) {
                                sb.append(")");
                            }
                            index++;
                            valArray.add(values.get(i));
                            orArray.add(colName);
                            colArray.add(colVar);
                        }
                    }
                }
                if (entry.getKey().equals(OozieClient.FILTER_ID)) {
                    List<String> values = filter.get(OozieClient.FILTER_ID);
                    colName = "id";
                    for (int i = 0; i < values.size(); i++) {
                        colVar = "id";
                        colVar = colVar + index;
                        if (!isEnabled && !isId) {
                            sb.append(seletStr).append(" where w.id IN (:id" + index);
                            isId = true;
                            isEnabled = true;
                        } else {
                            if (isEnabled && !isId) {
                                sb.append(" and w.id IN (:id" + index);
                                isId = true;
                            } else {
                                if (isId) {
                                    sb.append(", :id" + index);
                                }
                            }
                        }
                        if (i == values.size() - 1) {
                            sb.append(")");
                        }
                        index++;
                        valArray.add(values.get(i));
                        orArray.add(colName);
                        colArray.add(colVar);
                    }
                } else if (entry.getKey().equalsIgnoreCase(OozieClient.FILTER_CREATED_TIME_START)) {
                    List<String> values = filter.get(OozieClient.FILTER_CREATED_TIME_START);
                    colName = "createdTimestampStart";
                    if (values.size() > 1) {
                        throw new JPAExecutorException(ErrorCode.E0302, "cannot specify multiple startcreatedtime");
                    }
                    colVar = colName;
                    colVar = colVar + index;
                    if (!isEnabled) {
                        sb.append(seletStr).append(" where w.createdTimestamp >= :" + colVar);
                        isEnabled = true;
                    } else {
                        sb.append(" and w.createdTimestamp >= :" + colVar);
                    }
                    index++;
                    Date createdTime = null;
                    try {
                        createdTime = parseCreatedTimeString(values.get(0));
                    } catch (Exception e) {
                        throw new JPAExecutorException(ErrorCode.E0302, e.getMessage());
                    }
                    Timestamp createdTimeStamp = new Timestamp(createdTime.getTime());
                    valArray.add(createdTimeStamp);
                    orArray.add(colName);
                    colArray.add(colVar);
                } else if (entry.getKey().equalsIgnoreCase(OozieClient.FILTER_CREATED_TIME_END)) {
                    List<String> values = filter.get(OozieClient.FILTER_CREATED_TIME_END);
                    colName = "createdTimestampEnd";
                    if (values.size() > 1) {
                        throw new JPAExecutorException(ErrorCode.E0302, "cannot specify multiple endcreatedtime");
                    }
                    colVar = colName;
                    colVar = colVar + index;
                    if (!isEnabled) {
                        sb.append(seletStr).append(" where w.createdTimestamp <= :" + colVar);
                        isEnabled = true;
                    } else {
                        sb.append(" and w.createdTimestamp <= :" + colVar);
                    }
                    index++;
                    Date createdTime = null;
                    try {
                        createdTime = parseCreatedTimeString(values.get(0));
                    } catch (Exception e) {
                        throw new JPAExecutorException(ErrorCode.E0302, e.getMessage());
                    }
                    Timestamp createdTimeStamp = new Timestamp(createdTime.getTime());
                    valArray.add(createdTimeStamp);
                    orArray.add(colName);
                    colArray.add(colVar);
                } else // w.id = text || w.appName.contains(text) || w.user.contains(text)
                if (entry.getKey().equalsIgnoreCase(OozieClient.FILTER_TEXT)) {
                    StoreStatusFilter.filterJobsUsingText(filter, sb, isEnabled, seletStr, valArray, orArray, colArray);
                    isEnabled = true;
                }
            }
        }
    }
    orderBy = StoreStatusFilter.getSortBy(filter, orderBy);
    int realLen = 0;
    Query q = null;
    Query qTotal = null;
    if (orArray.size() == 0 && orderBy.equals(DEFAULT_ORDER_BY)) {
        q = em.createNamedQuery("GET_WORKFLOWS_COLUMNS");
        q.setFirstResult(start - 1);
        q.setMaxResults(len);
        qTotal = em.createNamedQuery("GET_WORKFLOWS_COUNT");
    } else {
        sb = sb.toString().trim().length() == 0 ? sb.append(seletStr) : sb;
        String sbTotal = sb.toString();
        sb.append(orderBy);
        q = em.createQuery(sb.toString());
        q.setFirstResult(start - 1);
        q.setMaxResults(len);
        qTotal = em.createQuery(sbTotal.replace(seletStr, countStr));
        for (int i = 0; i < orArray.size(); i++) {
            q.setParameter(colArray.get(i), valArray.get(i));
            qTotal.setParameter(colArray.get(i), valArray.get(i));
        }
    }
    OpenJPAQuery kq = OpenJPAPersistence.cast(q);
    JDBCFetchPlan fetch = (JDBCFetchPlan) kq.getFetchPlan();
    fetch.setFetchBatchSize(20);
    fetch.setResultSetType(ResultSetType.SCROLL_INSENSITIVE);
    fetch.setFetchDirection(FetchDirection.FORWARD);
    fetch.setLRSSizeAlgorithm(LRSSizeAlgorithm.LAST);
    List<?> resultList = q.getResultList();
    List<Object[]> objectArrList = (List<Object[]>) resultList;
    List<WorkflowJobBean> wfBeansList = new ArrayList<WorkflowJobBean>();
    for (Object[] arr : objectArrList) {
        WorkflowJobBean ww = getBeanForWorkflowFromArray(arr);
        wfBeansList.add(ww);
    }
    realLen = ((Long) qTotal.getSingleResult()).intValue();
    return new WorkflowsInfo(wfBeansList, start, len, realLen);
}
Also used : Query(javax.persistence.Query) OpenJPAQuery(org.apache.openjpa.persistence.OpenJPAQuery) JDBCFetchPlan(org.apache.openjpa.persistence.jdbc.JDBCFetchPlan) ArrayList(java.util.ArrayList) Timestamp(java.sql.Timestamp) WorkflowJobBean(org.apache.oozie.WorkflowJobBean) Date(java.util.Date) WorkflowsInfo(org.apache.oozie.WorkflowsInfo) OpenJPAQuery(org.apache.openjpa.persistence.OpenJPAQuery) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 3 with JDBCFetchPlan

use of org.apache.openjpa.persistence.jdbc.JDBCFetchPlan in project oozie by apache.

the class BundleJobInfoGetJPAExecutor method execute.

/* (non-Javadoc)
     * @see org.apache.oozie.executor.jpa.JPAExecutor#execute(javax.persistence.EntityManager)
     */
@Override
@SuppressWarnings("unchecked")
public BundleJobInfo execute(EntityManager em) throws JPAExecutorException {
    List<String> orArray = new ArrayList<String>();
    List<String> colArray = new ArrayList<String>();
    List<Object> valArray = new ArrayList<Object>();
    StringBuilder sb = new StringBuilder("");
    String orderBy = DEFAULT_ORDER_BY;
    StoreStatusFilter.filter(filter, orArray, colArray, valArray, sb, StoreStatusFilter.bundleSeletStr, StoreStatusFilter.bundleCountStr);
    orderBy = StoreStatusFilter.getSortBy(filter, orderBy);
    int realLen = 0;
    Query q = null;
    Query qTotal = null;
    if (orArray.size() == 0 && orderBy.equals(DEFAULT_ORDER_BY)) {
        q = em.createNamedQuery("GET_BUNDLE_JOBS_COLUMNS");
        q.setFirstResult(start - 1);
        q.setMaxResults(len);
        qTotal = em.createNamedQuery("GET_BUNDLE_JOBS_COUNT");
    } else {
        sb = sb.toString().trim().length() == 0 ? sb.append(StoreStatusFilter.bundleSeletStr) : sb;
        String sbTotal = sb.toString();
        sb.append(orderBy);
        q = em.createQuery(sb.toString());
        q.setFirstResult(start - 1);
        q.setMaxResults(len);
        qTotal = em.createQuery(sbTotal.replace(StoreStatusFilter.bundleSeletStr, StoreStatusFilter.bundleCountStr));
    }
    for (int i = 0; i < orArray.size(); i++) {
        q.setParameter(colArray.get(i), valArray.get(i));
        qTotal.setParameter(colArray.get(i), valArray.get(i));
    }
    OpenJPAQuery kq = OpenJPAPersistence.cast(q);
    JDBCFetchPlan fetch = (JDBCFetchPlan) kq.getFetchPlan();
    fetch.setFetchBatchSize(20);
    fetch.setResultSetType(ResultSetType.SCROLL_INSENSITIVE);
    fetch.setFetchDirection(FetchDirection.FORWARD);
    fetch.setLRSSizeAlgorithm(LRSSizeAlgorithm.LAST);
    List<?> resultList = q.getResultList();
    List<Object[]> objectArrList = (List<Object[]>) resultList;
    List<BundleJobBean> bundleBeansList = new ArrayList<BundleJobBean>();
    for (Object[] arr : objectArrList) {
        BundleJobBean bean = getBeanForBundleJobFromArray(arr);
        bundleBeansList.add(bean);
    }
    realLen = ((Long) qTotal.getSingleResult()).intValue();
    return new BundleJobInfo(bundleBeansList, start, len, realLen);
}
Also used : Query(javax.persistence.Query) OpenJPAQuery(org.apache.openjpa.persistence.OpenJPAQuery) JDBCFetchPlan(org.apache.openjpa.persistence.jdbc.JDBCFetchPlan) ArrayList(java.util.ArrayList) BundleJobInfo(org.apache.oozie.BundleJobInfo) OpenJPAQuery(org.apache.openjpa.persistence.OpenJPAQuery) BundleJobBean(org.apache.oozie.BundleJobBean) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with JDBCFetchPlan

use of org.apache.openjpa.persistence.jdbc.JDBCFetchPlan in project oozie by apache.

the class CoordJobInfoGetJPAExecutor method execute.

@Override
@SuppressWarnings("unchecked")
public CoordinatorJobInfo execute(EntityManager em) throws JPAExecutorException {
    List<String> orArray = new ArrayList<String>();
    List<String> colArray = new ArrayList<String>();
    List<Object> valArray = new ArrayList<Object>();
    StringBuilder sb = new StringBuilder("");
    String orderBy = DEFAULT_ORDER_BY;
    StoreStatusFilter.filter(filter, orArray, colArray, valArray, sb, StoreStatusFilter.coordSeletStr, StoreStatusFilter.coordCountStr);
    orderBy = StoreStatusFilter.getSortBy(filter, orderBy);
    int realLen = 0;
    Query q = null;
    Query qTotal = null;
    if (orArray.size() == 0 && orderBy.equals(DEFAULT_ORDER_BY)) {
        q = em.createNamedQuery("GET_COORD_JOBS_COLUMNS");
        q.setFirstResult(start - 1);
        q.setMaxResults(len);
        qTotal = em.createNamedQuery("GET_COORD_JOBS_COUNT");
    } else {
        sb = sb.toString().trim().length() == 0 ? sb.append(StoreStatusFilter.coordSeletStr) : sb;
        String sbTotal = sb.toString();
        sb.append(orderBy);
        q = em.createQuery(sb.toString());
        q.setFirstResult(start - 1);
        q.setMaxResults(len);
        qTotal = em.createQuery(sbTotal.replace(StoreStatusFilter.coordSeletStr, StoreStatusFilter.coordCountStr));
    }
    for (int i = 0; i < orArray.size(); i++) {
        q.setParameter(colArray.get(i), valArray.get(i));
        qTotal.setParameter(colArray.get(i), valArray.get(i));
    }
    OpenJPAQuery kq = OpenJPAPersistence.cast(q);
    JDBCFetchPlan fetch = (JDBCFetchPlan) kq.getFetchPlan();
    fetch.setFetchBatchSize(20);
    fetch.setResultSetType(ResultSetType.SCROLL_INSENSITIVE);
    fetch.setFetchDirection(FetchDirection.FORWARD);
    fetch.setLRSSizeAlgorithm(LRSSizeAlgorithm.LAST);
    List<?> resultList = q.getResultList();
    List<Object[]> objectArrList = (List<Object[]>) resultList;
    List<CoordinatorJobBean> coordBeansList = new ArrayList<CoordinatorJobBean>();
    for (Object[] arr : objectArrList) {
        CoordinatorJobBean ww = getBeanForCoordinatorJobFromArray(arr);
        coordBeansList.add(ww);
    }
    realLen = ((Long) qTotal.getSingleResult()).intValue();
    return new CoordinatorJobInfo(coordBeansList, start, len, realLen);
}
Also used : CoordinatorJobBean(org.apache.oozie.CoordinatorJobBean) Query(javax.persistence.Query) OpenJPAQuery(org.apache.openjpa.persistence.OpenJPAQuery) CoordinatorJobInfo(org.apache.oozie.CoordinatorJobInfo) JDBCFetchPlan(org.apache.openjpa.persistence.jdbc.JDBCFetchPlan) ArrayList(java.util.ArrayList) OpenJPAQuery(org.apache.openjpa.persistence.OpenJPAQuery) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

JDBCFetchPlan (org.apache.openjpa.persistence.jdbc.JDBCFetchPlan)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Query (javax.persistence.Query)3 OpenJPAQuery (org.apache.openjpa.persistence.OpenJPAQuery)3 Timestamp (java.sql.Timestamp)1 Date (java.util.Date)1 Map (java.util.Map)1 BundleJobBean (org.apache.oozie.BundleJobBean)1 BundleJobInfo (org.apache.oozie.BundleJobInfo)1 CoordinatorJobBean (org.apache.oozie.CoordinatorJobBean)1 CoordinatorJobInfo (org.apache.oozie.CoordinatorJobInfo)1 WorkflowJobBean (org.apache.oozie.WorkflowJobBean)1 WorkflowsInfo (org.apache.oozie.WorkflowsInfo)1 FetchPlan (org.apache.openjpa.persistence.FetchPlan)1 OpenJPAEntityManager (org.apache.openjpa.persistence.OpenJPAEntityManager)1 IsolationLevel (org.apache.openjpa.persistence.jdbc.IsolationLevel)1