@Test public void queryWithParameterActionTest() throws Exception { WorkItemManager manager = new TestWorkItemManager(); String DESC = "Cheese"; Product p1 = new Product("Bread", 2f); Product p2 = new Product("Milk", 3f); Product p3 = new Product(DESC, 5f); create(p1); create(p2); create(p3); WorkItemImpl workItem = new WorkItemImpl(); workItem.setParameter(JPAWorkItemHandler.P_ACTION, JPAWorkItemHandler.QUERY_ACTION); Map<String, Object> params = new HashMap<>(); params.put("desc", DESC); workItem.setParameter(JPAWorkItemHandler.P_QUERY, "SELECT p FROM Product p where p.description = :desc"); workItem.setParameter(JPAWorkItemHandler.P_QUERY_PARAMS, params); UserTransaction ut = getUserTransaction(); ut.begin(); handler.executeWorkItem(workItem, manager); ut.commit(); Map<String, Object> results = ((TestWorkItemManager) manager).getResults(workItem.getId()); @SuppressWarnings("unchecked") List<Product> products = (List<Product>) results.get(JPAWorkItemHandler.P_QUERY_RESULTS); assertEquals(1, products.size()); products = getAllProducts(); assertEquals(3, products.size()); for (Product product : products) { removeProduct(product); } products = getAllProducts(); assertEquals(0, products.size()); }
public void executeWorkItem(WorkItem wi, WorkItemManager wim) { Object actionParam = wi.getParameter(P_ACTION); Object entity = wi.getParameter(P_ENTITY); Object id = wi.getParameter(P_ID); Object type = wi.getParameter(P_TYPE); Object queryName = wi.getParameter(P_QUERY); Object queryParams = wi.getParameter(P_QUERY_PARAMS); Map<String, Object> params = new HashMap<String, Object>(); List<Object> queryResults = Collections.emptyList(); String action; if (entity == null && P_ACTION.equals(QUERY_ACTION)) { throw new IllegalArgumentException( "An entity is required. Use the 'entity' parameter"); } action = String.valueOf(actionParam).trim().toUpperCase(); logger.debug("Action {} on {}", action, entity); EntityManager em = emf.createEntityManager(); try { RequiredParameterValidator.validate(this.getClass(), wi); em.joinTransaction(); switch (action) { case DELETE_ACTION: doDelete(em, entity, id); break; case GET_ACTION: if (id == null || type == null) { throw new IllegalArgumentException( "Id or type can't be null when getting an entity"); } entity = doGet(em, type.toString(), Long.parseLong(id.toString())); break; case UPDATE_ACTION: doUpdate(em, entity); break; case CREATE_ACTION: em.persist(entity); break; case QUERY_ACTION: if (queryName == null) { throw new IllegalArgumentException("You must provide a '" + P_QUERY + "' parameter to run named queries."); } queryResults = doQuery(em, String.valueOf(queryName), queryParams); break; default: throw new IllegalArgumentException( "Action " + action + " not recognized. Use 'delete', 'create', 'update', query, or 'get'"); } params.put(P_RESULT, entity); params.put(P_QUERY_RESULTS, queryResults); wim.completeWorkItem(wi.getId(), params); } catch (Exception e) { logger.debug("Error performing JPA action ", e); handleException(e); } finally { em.close(); } }
JPAWorkItemHandler extends AbstractLogOrThrowWorkItemHandler implements Cacheable { public void executeWorkItem(WorkItem wi, WorkItemManager wim) { Object actionParam = wi.getParameter(P_ACTION); Object entity = wi.getParameter(P_ENTITY); Object id = wi.getParameter(P_ID); Object type = wi.getParameter(P_TYPE); Object queryName = wi.getParameter(P_QUERY); Object queryParams = wi.getParameter(P_QUERY_PARAMS); Map<String, Object> params = new HashMap<String, Object>(); List<Object> queryResults = Collections.emptyList(); String action; if (entity == null && P_ACTION.equals(QUERY_ACTION)) { throw new IllegalArgumentException( "An entity is required. Use the 'entity' parameter"); } action = String.valueOf(actionParam).trim().toUpperCase(); logger.debug("Action {} on {}", action, entity); EntityManager em = emf.createEntityManager(); try { RequiredParameterValidator.validate(this.getClass(), wi); em.joinTransaction(); switch (action) { case DELETE_ACTION: doDelete(em, entity, id); break; case GET_ACTION: if (id == null || type == null) { throw new IllegalArgumentException( "Id or type can't be null when getting an entity"); } entity = doGet(em, type.toString(), Long.parseLong(id.toString())); break; case UPDATE_ACTION: doUpdate(em, entity); break; case CREATE_ACTION: em.persist(entity); break; case QUERY_ACTION: if (queryName == null) { throw new IllegalArgumentException("You must provide a '" + P_QUERY + "' parameter to run named queries."); } queryResults = doQuery(em, String.valueOf(queryName), queryParams); break; default: throw new IllegalArgumentException( "Action " + action + " not recognized. Use 'delete', 'create', 'update', query, or 'get'"); } params.put(P_RESULT, entity); params.put(P_QUERY_RESULTS, queryResults); wim.completeWorkItem(wi.getId(), params); } catch (Exception e) { logger.debug("Error performing JPA action ", e); handleException(e); } finally { em.close(); } } }
JPAWorkItemHandler extends AbstractLogOrThrowWorkItemHandler implements Cacheable { public void executeWorkItem(WorkItem wi, WorkItemManager wim) { Object actionParam = wi.getParameter(P_ACTION); Object entity = wi.getParameter(P_ENTITY); Object id = wi.getParameter(P_ID); Object type = wi.getParameter(P_TYPE); Object queryName = wi.getParameter(P_QUERY); Object queryParams = wi.getParameter(P_QUERY_PARAMS); Map<String, Object> params = new HashMap<String, Object>(); List<Object> queryResults = Collections.emptyList(); String action; if (entity == null && P_ACTION.equals(QUERY_ACTION)) { throw new IllegalArgumentException( "An entity is required. Use the 'entity' parameter"); } action = String.valueOf(actionParam).trim().toUpperCase(); logger.debug("Action {} on {}", action, entity); EntityManager em = emf.createEntityManager(); try { RequiredParameterValidator.validate(this.getClass(), wi); em.joinTransaction(); switch (action) { case DELETE_ACTION: doDelete(em, entity, id); break; case GET_ACTION: if (id == null || type == null) { throw new IllegalArgumentException( "Id or type can't be null when getting an entity"); } entity = doGet(em, type.toString(), Long.parseLong(id.toString())); break; case UPDATE_ACTION: doUpdate(em, entity); break; case CREATE_ACTION: em.persist(entity); break; case QUERY_ACTION: if (queryName == null) { throw new IllegalArgumentException("You must provide a '" + P_QUERY + "' parameter to run named queries."); } queryResults = doQuery(em, String.valueOf(queryName), queryParams); break; default: throw new IllegalArgumentException( "Action " + action + " not recognized. Use 'delete', 'create', 'update', query, or 'get'"); } params.put(P_RESULT, entity); params.put(P_QUERY_RESULTS, queryResults); wim.completeWorkItem(wi.getId(), params); } catch (Exception e) { logger.debug("Error performing JPA action ", e); handleException(e); } finally { em.close(); } } JPAWorkItemHandler(String persistenceUnit);  JPAWorkItemHandler(String persistenceUnit,
                              ClassLoader classloader); }
JPAWorkItemHandler extends AbstractLogOrThrowWorkItemHandler implements Cacheable { public void executeWorkItem(WorkItem wi, WorkItemManager wim) { Object actionParam = wi.getParameter(P_ACTION); Object entity = wi.getParameter(P_ENTITY); Object id = wi.getParameter(P_ID); Object type = wi.getParameter(P_TYPE); Object queryName = wi.getParameter(P_QUERY); Object queryParams = wi.getParameter(P_QUERY_PARAMS); Map<String, Object> params = new HashMap<String, Object>(); List<Object> queryResults = Collections.emptyList(); String action; if (entity == null && P_ACTION.equals(QUERY_ACTION)) { throw new IllegalArgumentException( "An entity is required. Use the 'entity' parameter"); } action = String.valueOf(actionParam).trim().toUpperCase(); logger.debug("Action {} on {}", action, entity); EntityManager em = emf.createEntityManager(); try { RequiredParameterValidator.validate(this.getClass(), wi); em.joinTransaction(); switch (action) { case DELETE_ACTION: doDelete(em, entity, id); break; case GET_ACTION: if (id == null || type == null) { throw new IllegalArgumentException( "Id or type can't be null when getting an entity"); } entity = doGet(em, type.toString(), Long.parseLong(id.toString())); break; case UPDATE_ACTION: doUpdate(em, entity); break; case CREATE_ACTION: em.persist(entity); break; case QUERY_ACTION: if (queryName == null) { throw new IllegalArgumentException("You must provide a '" + P_QUERY + "' parameter to run named queries."); } queryResults = doQuery(em, String.valueOf(queryName), queryParams); break; default: throw new IllegalArgumentException( "Action " + action + " not recognized. Use 'delete', 'create', 'update', query, or 'get'"); } params.put(P_RESULT, entity); params.put(P_QUERY_RESULTS, queryResults); wim.completeWorkItem(wi.getId(), params); } catch (Exception e) { logger.debug("Error performing JPA action ", e); handleException(e); } finally { em.close(); } } JPAWorkItemHandler(String persistenceUnit);  JPAWorkItemHandler(String persistenceUnit,
                              ClassLoader classloader); void executeWorkItem(WorkItem wi,
                                WorkItemManager wim); void close(); void abortWorkItem(WorkItem wi,
                              WorkItemManager wim); }
JPAWorkItemHandler extends AbstractLogOrThrowWorkItemHandler implements Cacheable { public void executeWorkItem(WorkItem wi, WorkItemManager wim) { Object actionParam = wi.getParameter(P_ACTION); Object entity = wi.getParameter(P_ENTITY); Object id = wi.getParameter(P_ID); Object type = wi.getParameter(P_TYPE); Object queryName = wi.getParameter(P_QUERY); Object queryParams = wi.getParameter(P_QUERY_PARAMS); Map<String, Object> params = new HashMap<String, Object>(); List<Object> queryResults = Collections.emptyList(); String action; if (entity == null && P_ACTION.equals(QUERY_ACTION)) { throw new IllegalArgumentException( "An entity is required. Use the 'entity' parameter"); } action = String.valueOf(actionParam).trim().toUpperCase(); logger.debug("Action {} on {}", action, entity); EntityManager em = emf.createEntityManager(); try { RequiredParameterValidator.validate(this.getClass(), wi); em.joinTransaction(); switch (action) { case DELETE_ACTION: doDelete(em, entity, id); break; case GET_ACTION: if (id == null || type == null) { throw new IllegalArgumentException( "Id or type can't be null when getting an entity"); } entity = doGet(em, type.toString(), Long.parseLong(id.toString())); break; case UPDATE_ACTION: doUpdate(em, entity); break; case CREATE_ACTION: em.persist(entity); break; case QUERY_ACTION: if (queryName == null) { throw new IllegalArgumentException("You must provide a '" + P_QUERY + "' parameter to run named queries."); } queryResults = doQuery(em, String.valueOf(queryName), queryParams); break; default: throw new IllegalArgumentException( "Action " + action + " not recognized. Use 'delete', 'create', 'update', query, or 'get'"); } params.put(P_RESULT, entity); params.put(P_QUERY_RESULTS, queryResults); wim.completeWorkItem(wi.getId(), params); } catch (Exception e) { logger.debug("Error performing JPA action ", e); handleException(e); } finally { em.close(); } } JPAWorkItemHandler(String persistenceUnit);  JPAWorkItemHandler(String persistenceUnit,
                              ClassLoader classloader); void executeWorkItem(WorkItem wi,
                                WorkItemManager wim); void close(); void abortWorkItem(WorkItem wi,
                              WorkItemManager wim); static final String P_RESULT; static final String P_TYPE; static final String P_ID; static final String P_ENTITY; static final String P_ACTION; static final String P_QUERY; static final String P_QUERY_PARAMS; static final String P_QUERY_RESULTS; static final String CREATE_ACTION; static final String UPDATE_ACTION; static final String GET_ACTION; static final String DELETE_ACTION; static final String QUERY_ACTION; }