Search in sources :

Example 1 with OpenJPAQuery

use of org.apache.openjpa.persistence.OpenJPAQuery in project oozie by apache.

the class WorkflowStore method getWorkflowOnly.

private WorkflowJobBean getWorkflowOnly(final String id, boolean locking) throws SQLException, InterruptedException, StoreException {
    WorkflowJobBean wfBean = null;
    Query q = entityManager.createNamedQuery("GET_WORKFLOW");
    /*
         * if (locking) { // q.setHint("openjpa.FetchPlan.ReadLockMode","READ");
         * OpenJPAQuery oq = OpenJPAPersistence.cast(q); FetchPlan fetch =
         * oq.getFetchPlan(); fetch.setReadLockMode(LockModeType.WRITE);
         * fetch.setLockTimeout(-1); // unlimited }
         */
    q.setParameter("id", id);
    List<WorkflowJobBean> w = q.getResultList();
    if (w.size() > 0) {
        wfBean = w.get(0);
    }
    return wfBean;
// return getBeanForRunningWorkflow(wfBean);
}
Also used : OpenJPAQuery(org.apache.openjpa.persistence.OpenJPAQuery) Query(javax.persistence.Query) WorkflowJobBean(org.apache.oozie.WorkflowJobBean)

Example 2 with OpenJPAQuery

use of org.apache.openjpa.persistence.OpenJPAQuery in project oozie by apache.

the class WorkflowStore method getAction.

/**
 * Load the action data and returns a bean.
 *
 * @param id Action Id
 * @param locking true if the action is to be locked
 * @return Action Bean
 * @throws StoreException If action doesn't exist
 */
public WorkflowActionBean getAction(final String id, final boolean locking) throws StoreException {
    ParamChecker.notEmpty(id, "ActionID");
    WorkflowActionBean action = doOperation("getAction", new Callable<WorkflowActionBean>() {

        public WorkflowActionBean call() throws SQLException, StoreException, WorkflowException, InterruptedException {
            Query q = entityManager.createNamedQuery("GET_ACTION");
            /*
                 * if (locking) { OpenJPAQuery oq = OpenJPAPersistence.cast(q);
                 * FetchPlan fetch = oq.getFetchPlan();
                 * fetch.setReadLockMode(LockModeType.WRITE);
                 * fetch.setLockTimeout(1000); // 1 seconds }
                 */
            WorkflowActionBean action = null;
            q.setParameter("id", id);
            List<WorkflowActionBean> actions = q.getResultList();
            // action = (WorkflowActionBean) q.getSingleResult();
            if (actions.size() > 0) {
                action = actions.get(0);
            } else {
                throw new StoreException(ErrorCode.E0605, id);
            }
            // return action;
            return getBeanForRunningAction(action);
        }
    });
    return action;
}
Also used : OpenJPAQuery(org.apache.openjpa.persistence.OpenJPAQuery) Query(javax.persistence.Query) SQLException(java.sql.SQLException) WorkflowException(org.apache.oozie.workflow.WorkflowException) ArrayList(java.util.ArrayList) List(java.util.List) WorkflowActionBean(org.apache.oozie.WorkflowActionBean)

Example 3 with OpenJPAQuery

use of org.apache.openjpa.persistence.OpenJPAQuery 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 4 with OpenJPAQuery

use of org.apache.openjpa.persistence.OpenJPAQuery 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 5 with OpenJPAQuery

use of org.apache.openjpa.persistence.OpenJPAQuery 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

Query (javax.persistence.Query)5 OpenJPAQuery (org.apache.openjpa.persistence.OpenJPAQuery)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 JDBCFetchPlan (org.apache.openjpa.persistence.jdbc.JDBCFetchPlan)3 WorkflowJobBean (org.apache.oozie.WorkflowJobBean)2 SQLException (java.sql.SQLException)1 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 WorkflowActionBean (org.apache.oozie.WorkflowActionBean)1 WorkflowsInfo (org.apache.oozie.WorkflowsInfo)1 WorkflowException (org.apache.oozie.workflow.WorkflowException)1