예제 #1
0
 public List<GenericValue> findByOr(
     final String entityName, final List expressions, final List orderBy)
     throws DataAccessException {
   try {
     if (orderBy != null) {
       return delegatorInterface.findByOr(entityName, expressions, orderBy);
     } else {
       return delegatorInterface.findByOr(entityName, expressions);
     }
   } catch (final GenericEntityException e) {
     throw new DataAccessException(e);
   }
 }
예제 #2
0
  public GenericValue createValue(final String entityName, final Map<String, Object> fields) {
    try {
      final Map<String, Object> params =
          (fields == null) ? new HashMap<String, Object>(2) : new HashMap<String, Object>(fields);
      if (params.get("id") == null) {
        final Long id = delegatorInterface.getNextSeqId(entityName);
        params.put("id", id);
      }

      final GenericValue v = delegatorInterface.makeValue(entityName, params);
      v.create();
      return v;
    } catch (final GenericEntityException ex) {
      throw new DataAccessException(ex);
    }
  }
예제 #3
0
 public void store(final GenericValue gv) throws DataAccessException {
   try {
     delegatorInterface.store(gv);
   } catch (final GenericEntityException e) {
     throw new DataAccessException(e);
   }
 }
예제 #4
0
 public List<GenericValue> findAll(final String s) {
   try {
     return delegatorInterface.findAll(s);
   } catch (final GenericEntityException e) {
     throw new DataAccessException(e);
   }
 }
예제 #5
0
 public int removeValue(final GenericValue value) throws DataAccessException {
   try {
     return delegatorInterface.removeValue(value);
   } catch (final GenericEntityException e) {
     throw new DataAccessException(e);
   }
 }
예제 #6
0
 public int removeByAnd(final String entityName, final Map map) throws DataAccessException {
   try {
     return delegatorInterface.removeByAnd(entityName, map);
   } catch (final GenericEntityException e) {
     throw new DataAccessException(e);
   }
 }
예제 #7
0
 public List<GenericValue> getRelated(final String relationName, final GenericValue gv) {
   try {
     return delegatorInterface.getRelated(relationName, gv);
   } catch (final GenericEntityException e) {
     throw new DataAccessException(e);
   }
 }
예제 #8
0
 public void removeAll(final List<GenericValue> genericValues) throws DataAccessException {
   try {
     delegatorInterface.removeAll(genericValues, false);
   } catch (final GenericEntityException e) {
     throw new DataAccessException(e);
   }
 }
  @Test
  public void testGetJiraTablesExecption() throws Exception {
    DelegatorInterface delegatorInterface = mock(DelegatorInterface.class);
    OfBizConnectionFactory factory = mock(OfBizConnectionFactory.class);
    ModelReader reader = mock(ModelReader.class);

    when(delegatorInterface.getModelReader()).thenReturn(reader);
    when(reader.getEntityNames()).thenReturn(Lists.<String>newArrayList("one", "two", "three"));

    when(reader.getModelEntity("one")).thenReturn(tableEntity("one"));
    when(reader.getModelEntity("two")).thenReturn(tableEntity("two"));
    when(reader.getModelEntity("three")).thenThrow(new GenericEntityException());

    JRA24857Check check = new JRA24857Check(factory, delegatorInterface);
    assertTrue(check.getJiraTables().isEmpty());
  }
예제 #10
0
 @Override
 public boolean removeRelated(String relationName, GenericValue schemeGv) {
   try {
     return delegatorInterface.removeRelated(relationName, schemeGv) > 0;
   } catch (GenericEntityException e) {
     throw new DataAccessException(e);
   }
 }
  @Test
  public void testGetJiraTables() throws Exception {
    DelegatorInterface delegatorInterface = mock(DelegatorInterface.class);
    OfBizConnectionFactory factory = mock(OfBizConnectionFactory.class);
    ModelReader reader = mock(ModelReader.class);

    when(delegatorInterface.getModelReader()).thenReturn(reader);
    when(reader.getEntityNames()).thenReturn(Lists.<String>newArrayList("one", "two", "three"));

    when(reader.getModelEntity("one")).thenReturn(tableEntity("one"));
    when(reader.getModelEntity("two")).thenReturn(tableEntity("two"));
    ModelViewEntity three = viewEntity("three");
    when(reader.getModelEntity("three")).thenReturn(three);

    JRA24857Check check = new JRA24857Check(factory, delegatorInterface);
    assertEquals(Sets.<String>newHashSet("one", "two"), check.getJiraTables());
  }
예제 #12
0
 @Override
 public int removeByCondition(String entityName, EntityCondition condition)
     throws DataAccessException {
   try {
     return delegatorInterface.removeByCondition(entityName, condition, false);
   } catch (final GenericEntityException e) {
     throw new DataAccessException(e);
   }
 }
예제 #13
0
 public List<GenericValue> findByLike(
     final String s, final Map<String, ?> map, final List<String> orderBy)
     throws DataAccessException {
   try {
     return delegatorInterface.findByLike(s, map, orderBy);
   } catch (final GenericEntityException e) {
     throw new DataAccessException(e);
   }
 }
예제 #14
0
 public List<GenericValue> findByAnd(
     final String entityName, final List<EntityCondition> expressions) throws DataAccessException {
   // cannot check EntityCondition instances as they don't give us a where condition
   try {
     return delegatorInterface.findByAnd(entityName, expressions);
   } catch (final GenericEntityException e) {
     throw new DataAccessException(e);
   }
 }
예제 #15
0
 public List<GenericValue> findByAnd(final String entityName, final Map<String, ?> fields)
     throws DataAccessException {
   findByValidator.checkAll(entityName, fields.keySet());
   try {
     return delegatorInterface.findByAnd(entityName, fields);
   } catch (final GenericEntityException e) {
     throw new DataAccessException(e);
   }
 }
예제 #16
0
 public OfBizListIterator findListIteratorByCondition(
     final String entityType, final EntityCondition condition) {
   try {
     return new DefaultOfBizListIterator(
         delegatorInterface.findListIteratorByCondition(entityType, condition, null, null));
   } catch (final GenericEntityException e) {
     throw new DataAccessException(e);
   }
 }
예제 #17
0
 @Override
 public List<GenericValue> getRelated(
     final String relationName, final GenericValue gv, final List<String> orderBy)
     throws DataAccessException {
   try {
     return delegatorInterface.getRelatedOrderBy(relationName, orderBy, gv);
   } catch (final GenericEntityException e) {
     throw new DataAccessException(e);
   }
 }
예제 #18
0
 @Override
 public void createValueWithoutId(final String entityName, final Map<String, Object> fields)
     throws DataAccessException {
   try {
     final GenericValue v = delegatorInterface.makeValue(entityName, fields);
     v.create();
   } catch (final GenericEntityException ex) {
     throw new DataAccessException(ex);
   }
 }
예제 #19
0
 public GenericValue findByPrimaryKey(final String entityName, final Map<String, ?> fields) {
   final long start = System.currentTimeMillis();
   try {
     return delegatorInterface.findByPrimaryKey(entityName, fields);
   } catch (final GenericEntityException e) {
     throw new DataAccessException(e);
   } finally {
     ThreadLocalQueryProfiler.store(
         "OfBizDelegator", "findByPrimaryKey", System.currentTimeMillis() - start);
   }
 }
예제 #20
0
 public List<GenericValue> findByCondition(
     final String entityName,
     final EntityCondition entityCondition,
     final Collection<String> fieldsToSelect,
     final List<String> orderBy) {
   try {
     return delegatorInterface.findByCondition(
         entityName, entityCondition, fieldsToSelect, orderBy);
   } catch (final GenericEntityException e) {
     throw new DataAccessException(e);
   }
 }
예제 #21
0
 @Override
 public long getCountByAnd(final String entityName, final Map<String, ?> fields) {
   try {
     final EntityCondition condition = new EntityFieldMap(fields, EntityOperator.AND);
     final GenericValue countGV =
         EntityUtil.getOnly(
             delegatorInterface.findByCondition(
                 entityName + "Count", condition, ImmutableList.of(COUNT_FIELD_NAME), null));
     return countGV.getLong(COUNT_FIELD_NAME);
   } catch (final GenericEntityException e) {
     throw new DataAccessException(e);
   }
 }
예제 #22
0
 @Override
 public List<GenericValue> transform(
     final String entityName,
     final EntityCondition entityCondition,
     final List<String> orderBy,
     final String lockField,
     final Transformation transformation) {
   try {
     return delegatorInterface.transform(
         entityName, entityCondition, orderBy, lockField, transformation);
   } catch (GenericEntityException e) {
     throw new DataAccessException(e);
   }
 }
예제 #23
0
 /**
  * Always close the iterator returned from this method when finished.
  *
  * @return OfBizListIterator
  */
 public OfBizListIterator findListIteratorByCondition(
     final String entityName,
     final EntityCondition whereEntityCondition,
     final EntityCondition havingEntityCondition,
     final Collection fieldsToSelect,
     final List orderBy,
     final EntityFindOptions entityFindOptions) {
   try {
     return new DefaultOfBizListIterator(
         delegatorInterface.findListIteratorByCondition(
             entityName,
             whereEntityCondition,
             havingEntityCondition,
             fieldsToSelect,
             orderBy,
             entityFindOptions));
   } catch (final GenericEntityException e) {
     throw new DataAccessException(e);
   }
 }
예제 #24
0
 public GenericValue makeValue(final String entityName) {
   return delegatorInterface.makeValue(entityName, null);
 }
예제 #25
0
 @Override
 public void refreshSequencer() {
   delegatorInterface.refreshSequencer();
 }
예제 #26
0
 @Override
 public ModelReader getModelReader() {
   return delegatorInterface.getModelReader();
 }
예제 #27
0
  public int bulkCopyColumnValuesByAnd(
      final String entityName, final Map updateColumns, final Map criteria) {
    int result = 0;

    if ((entityName == null) || (updateColumns == null) || updateColumns.isEmpty()) {
      return 0;
    }

    try {
      final ModelEntity modelEntity = delegatorInterface.getModelEntity(entityName);

      final GenericHelper entityHelper = delegatorInterface.getEntityHelper(entityName);

      final ModelFieldTypeReader modelFieldTypeReader =
          ModelFieldTypeReader.getModelFieldTypeReader(entityHelper.getHelperName());

      final ArrayList<EntityConditionParam> params = new ArrayList<EntityConditionParam>();

      // generate the update sql
      final StringBuilder updateSql = new StringBuilder("UPDATE ");

      updateSql.append(modelEntity.getTableName(entityHelper.getHelperName()));
      updateSql.append(" SET ");

      if (!modelEntity.areFields(updateColumns.keySet())) {
        throw new GenericModelException(
            "At least one of the passed fields for update is not valid: "
                + updateColumns.keySet().toString());
      }

      if (!modelEntity.areFields(updateColumns.values())) {
        throw new GenericModelException(
            "At least one of the passed fields for update is not valid: "
                + updateColumns.values().toString());
      }

      for (final Iterator iterator = updateColumns.keySet().iterator(); iterator.hasNext(); ) {
        final String column = (String) iterator.next();
        updateSql.append(" ");
        final ModelField toModelField = modelEntity.getField(column);
        updateSql.append(toModelField.getColName());
        updateSql.append(" = ");
        final ModelField fromModelField = modelEntity.getField((String) updateColumns.get(column));
        updateSql.append(fromModelField.getColName());
        if (iterator.hasNext()) {
          updateSql.append(", ");
        }
      }

      if ((criteria != null) && !criteria.isEmpty()) {
        if (!modelEntity.areFields(criteria.keySet())) {
          throw new GenericModelException(
              "At least one of the passed fields is not valid: " + criteria.keySet().toString());
        }

        // generate the where clause
        final EntityFieldMap entityCondition = new EntityFieldMap(criteria, EntityOperator.AND);

        final String entityCondWhereString = entityCondition.makeWhereString(modelEntity, params);

        if (entityCondWhereString.length() > 0) {
          updateSql.append(" WHERE ");
          updateSql.append(entityCondWhereString);
        }
      }

      final SQLProcessor processor = new AutoCommitSQLProcessor(entityHelper.getHelperName());
      final String sql = updateSql.toString();

      if (log.isDebugEnabled()) {
        log.debug("Running bulk update SQL: '" + sql + '\'');
      }

      processor.prepareStatement(sql);

      for (final EntityConditionParam conditionParam : params) {
        SqlJdbcUtil.setValue(
            processor,
            conditionParam.getModelField(),
            modelEntity.getEntityName(),
            conditionParam.getFieldValue(),
            modelFieldTypeReader);
      }

      try {
        result = processor.executeUpdate();
      } finally {
        processor.close();
      }
    } catch (final GenericEntityException e) {
      throw new DataAccessException(e);
    } catch (final NoClassDefFoundError e) {
      // under JDK 1.3 unit tests - javax.sql.XADataSource cannot be found.
      // this shouldn't affect runtime - application servers should ship the jar
    }

    return result;
  }
예제 #28
0
  public int bulkUpdateByPrimaryKey(
      final String entityName, final Map<String, ?> updateValues, final List<Long> keys) {
    int result = 0;

    if ((entityName == null)
        || (updateValues == null)
        || updateValues.isEmpty()
        || (keys == null)
        || keys.isEmpty()) {
      return 0;
    }

    try {
      final GenericHelper entityHelper = delegatorInterface.getEntityHelper(entityName);
      final ModelEntity modelEntity = delegatorInterface.getModelEntity(entityName);

      final List<String> pks = modelEntity.getPkFieldNames();
      if (pks.size() != 1) {
        throw new DataAccessException(
            "BulkUpdateByPrimaryKey only works for single column keys at this moment.");
      }
      final String pkName = pks.get(0);

      final Updater updater =
          new Updater(
              ModelFieldTypeReader.getModelFieldTypeReader(entityHelper.getHelperName()),
              entityName);
      final List<Updater.Value> params = new ArrayList<Updater.Value>();

      final StringBuilder updateSql = new StringBuilder("UPDATE ");

      updateSql.append(modelEntity.getTableName(entityHelper.getHelperName()));
      updateSql.append(" SET ");
      // generate the update sql
      for (final Iterator<String> iterator = updateValues.keySet().iterator();
          iterator.hasNext(); ) {
        final String column = iterator.next();
        updateSql.append(" ");
        final ModelField field = modelEntity.getField(column);
        updateSql.append(field.getColName());
        updateSql.append(" = ");
        params.add(updater.create(field, updateValues.get(column)));
        updateSql.append("? ");
        if (iterator.hasNext()) {
          updateSql.append(", ");
        }
      }

      // generate the where clause
      updateSql.append(" WHERE ");

      // batch the update
      final int batchSize = getQueryBatchSize();

      int currentIndex = 0;

      while (currentIndex < keys.size()) {
        int i = 0;
        final StringBuilder idClause = new StringBuilder();
        final ArrayList<Long> idParams = new ArrayList<Long>();
        for (final Iterator<Long> iterator = keys.subList(currentIndex, keys.size()).iterator();
            iterator.hasNext() && (i < batchSize);
            i++) {
          final Long key = iterator.next();
          idClause.append(" ");
          idClause.append(pkName);
          idClause.append(" = ");
          idParams.add(key);
          idClause.append("? ");

          if (iterator.hasNext() && ((i + 1) < batchSize)) {
            idClause.append(" or ");
          }
        }

        final SQLProcessor processor = new AutoCommitSQLProcessor(entityHelper.getHelperName());
        processor.prepareStatement(updateSql.toString() + idClause.toString());
        for (final Updater.Value param : params) {
          param.setValue(processor);
        }
        for (final Long idParam : idParams) {
          processor.setValue(idParam);
        }

        try {
          result = processor.executeUpdate();
        } finally {
          processor.close();
        }
        currentIndex += i;
      }
    } catch (final GenericEntityException e) {
      throw new DataAccessException(e);
    } catch (final SQLException e) {
      throw new DataAccessException(e);
    } catch (final NoClassDefFoundError e) {
      // under JDK 1.3 unit tests - javax.sql.XADataSource cannot be found.
      // this shouldn't affect runtime - application servers should ship the jar
    }
    return result;
  }
예제 #29
0
  public int removeByOr(final String entityName, final String entityId, final List<Long> ids)
      throws DataAccessException, GenericModelException {
    int result = 0;

    final ModelEntity modelEntity = delegatorInterface.getModelEntity(entityName);

    if (modelEntity == null) {
      throw new GenericModelException("The entityName passed in was not valid: " + entityName);
    } else if (!modelEntity.isField(entityId)) {
      throw new GenericModelException(
          "The entityId passed in was not valid for the given entity: " + entityId);
    }
    final ModelField modelField = modelEntity.getField(entityId);

    try {
      final GenericHelper entityHelper = delegatorInterface.getEntityHelper(entityName);
      // Generate SQL
      final StringBuilder removeSql = new StringBuilder("DELETE FROM ");

      removeSql.append(modelEntity.getTableName(entityHelper.getHelperName()));
      removeSql.append(" WHERE ");
      removeSql.append(modelField.getColName());
      removeSql.append(" IN (");

      final int idsSize = ids.size();
      final ArrayList<Long> idParams = new ArrayList<Long>();
      StringBuilder idClause = new StringBuilder();

      // batch the update
      final int batchSize = getQueryBatchSize();
      int batchIndex = 0;
      for (int i = 0; i < idsSize; i++) {
        idParams.add(ids.get(i));
        idClause.append("?");

        final boolean isEndOfBatch = (batchIndex == batchSize - 1);
        final boolean isEndOfIdList = (i == idsSize - 1);

        if (isEndOfBatch || isEndOfIdList) {
          final SQLProcessor processor = new AutoCommitSQLProcessor(entityHelper.getHelperName());
          // finish batch
          idClause.append(")");
          try {
            processor.prepareStatement(removeSql.toString() + idClause.toString());
            for (final Long idParam : idParams) {
              processor.setValue(idParam);
            }
            // execute update
            result += processor.executeUpdate();

            // clean-up for the next batch
            idParams.clear();
            idClause = new StringBuilder();
            batchIndex = 0;
          } finally {
            try {
              processor.close();
            } catch (final GenericDataSourceException e) {
              log.warn("Could not close the SQLProcessor", e);
            }
          }
        } else {
          // add to this batch
          idClause.append(", ");
          batchIndex++;
        }
      }
    } catch (final GenericEntityException e) {
      throw new DataAccessException(e);
    } catch (final SQLException e) {
      throw new DataAccessException(e);
    }

    return result;
  }
예제 #30
0
 @Override
 public GenericValue makeValue(String entityName, Map<String, Object> fields) {
   return delegatorInterface.makeValue(entityName, fields);
 }