@Override public void updateCustomFields(final Iterable<CustomFieldModelDao> customFieldIds, final InternalCallContext context) throws CustomFieldApiException { transactionalSqlDao.execute(false, CustomFieldApiException.class, new EntitySqlDaoTransactionWrapper<Void>() { private void validateCustomField(final CustomFieldModelDao input, @Nullable CustomFieldModelDao existing) throws CustomFieldApiException { if (existing == null) { throw new CustomFieldApiException(ErrorCode.CUSTOM_FIELD_DOES_NOT_EXISTS_FOR_ID, input.getId()); } if (input.getObjectId() != null & !input.getObjectId().equals(existing.getObjectId())) { throw new CustomFieldApiException(ErrorCode.CUSTOM_FIELD_INVALID_UPDATE, input.getId(), input.getObjectId(), "ObjectId"); } if (input.getObjectType() != null && input.getObjectType() != existing.getObjectType()) { throw new CustomFieldApiException(ErrorCode.CUSTOM_FIELD_INVALID_UPDATE, input.getId(), input.getObjectType(), "ObjectType"); } if (input.getFieldName() != null && !input.getFieldName().equals(existing.getFieldName())) { throw new CustomFieldApiException(ErrorCode.CUSTOM_FIELD_INVALID_UPDATE, input.getId(), input.getFieldName(), "FieldName"); } } @Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception { final CustomFieldSqlDao sqlDao = entitySqlDaoWrapperFactory.become(CustomFieldSqlDao.class); for (final CustomFieldModelDao cur : customFieldIds) { final CustomFieldModelDao customField = sqlDao.getById(cur.getId().toString(), context); validateCustomField(cur, customField); sqlDao.updateValue(cur.getId().toString(), cur.getFieldValue(), context); postBusEventFromTransaction(customField, customField, ChangeType.UPDATE, entitySqlDaoWrapperFactory, context); } return null; } }); }
@Test(groups = "slow") public void testCustomFieldUpdate() throws Exception { final CustomField customField1 = new StringCustomField("gtqre", "value1", ObjectType.ACCOUNT, accountId, callContext.getCreatedDate()); eventsListener.pushExpectedEvents(NextEvent.CUSTOM_FIELD); customFieldUserApi.addCustomFields(ImmutableList.<CustomField>of(customField1), callContext); assertListenerStatus(); final CustomField update1 = new StringCustomField(customField1.getId(), customField1.getFieldName(), "value2", customField1.getObjectType(), customField1.getObjectId(), callContext.getCreatedDate()); customFieldUserApi.updateCustomFields(ImmutableList.of(update1), callContext); List<CustomField> all = customFieldUserApi.getCustomFieldsForAccount(accountId, callContext); Assert.assertEquals(all.size(), 1); Assert.assertEquals(all.get(0).getId(), update1.getId()); Assert.assertEquals(all.get(0).getObjectType(), update1.getObjectType()); Assert.assertEquals(all.get(0).getObjectId(), update1.getObjectId()); Assert.assertEquals(all.get(0).getFieldName(), update1.getFieldName()); Assert.assertEquals(all.get(0).getFieldValue(), "value2"); try { customFieldUserApi.updateCustomFields(ImmutableList.<CustomField>of(new StringCustomField("gtqre", "value1", ObjectType.ACCOUNT, accountId, callContext.getCreatedDate())), callContext); Assert.fail("Updating custom field should fail"); } catch (final CustomFieldApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CUSTOM_FIELD_DOES_NOT_EXISTS_FOR_ID.getCode()); } try { customFieldUserApi.updateCustomFields(ImmutableList.<CustomField>of(new StringCustomField(customField1.getId(), "wrongName", "value2", customField1.getObjectType(), customField1.getObjectId(), callContext.getCreatedDate())), callContext); Assert.fail("Updating custom field should fail"); } catch (final CustomFieldApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CUSTOM_FIELD_INVALID_UPDATE.getCode()); } }
customFieldUserApi.addCustomFields(ImmutableList.<CustomField>of(customField3, customField1), callContext); } catch (CustomFieldApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CUSTOM_FIELD_ALREADY_EXISTS.getCode());
@Override protected CustomFieldApiException generateAlreadyExistsException(final CustomFieldModelDao entity, final InternalCallContext context) { return new CustomFieldApiException(ErrorCode.CUSTOM_FIELD_ALREADY_EXISTS, entity.getId()); }
customFieldUserApi.addCustomFields(ImmutableList.<CustomField>of(customField3, customField1), callContext); } catch (CustomFieldApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CUSTOM_FIELD_ALREADY_EXISTS.getCode());
private void validateCustomField(final CustomFieldModelDao input, @Nullable CustomFieldModelDao existing) throws CustomFieldApiException { if (existing == null) { throw new CustomFieldApiException(ErrorCode.CUSTOM_FIELD_DOES_NOT_EXISTS_FOR_ID, input.getId()); } if (input.getObjectId() != null & !input.getObjectId().equals(existing.getObjectId())) { throw new CustomFieldApiException(ErrorCode.CUSTOM_FIELD_INVALID_UPDATE, input.getId(), input.getObjectId(), "ObjectId"); } if (input.getObjectType() != null && input.getObjectType() != existing.getObjectType()) { throw new CustomFieldApiException(ErrorCode.CUSTOM_FIELD_INVALID_UPDATE, input.getId(), input.getObjectType(), "ObjectType"); } if (input.getFieldName() != null && !input.getFieldName().equals(existing.getFieldName())) { throw new CustomFieldApiException(ErrorCode.CUSTOM_FIELD_INVALID_UPDATE, input.getId(), input.getFieldName(), "FieldName"); } }
@Test(groups = "slow") public void testCustomFieldUpdate() throws Exception { final CustomField customField1 = new StringCustomField("gtqre", "value1", ObjectType.ACCOUNT, accountId, callContext.getCreatedDate()); eventsListener.pushExpectedEvents(NextEvent.CUSTOM_FIELD); customFieldUserApi.addCustomFields(ImmutableList.<CustomField>of(customField1), callContext); assertListenerStatus(); final CustomField update1 = new StringCustomField(customField1.getId(), customField1.getFieldName(), "value2", customField1.getObjectType(), customField1.getObjectId(), callContext.getCreatedDate()); customFieldUserApi.updateCustomFields(ImmutableList.of(update1), callContext); List<CustomField> all = customFieldUserApi.getCustomFieldsForAccount(accountId, callContext); Assert.assertEquals(all.size(), 1); Assert.assertEquals(all.get(0).getId(), update1.getId()); Assert.assertEquals(all.get(0).getObjectType(), update1.getObjectType()); Assert.assertEquals(all.get(0).getObjectId(), update1.getObjectId()); Assert.assertEquals(all.get(0).getFieldName(), update1.getFieldName()); Assert.assertEquals(all.get(0).getFieldValue(), "value2"); try { customFieldUserApi.updateCustomFields(ImmutableList.<CustomField>of(new StringCustomField("gtqre", "value1", ObjectType.ACCOUNT, accountId, callContext.getCreatedDate())), callContext); Assert.fail("Updating custom field should fail"); } catch (final CustomFieldApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CUSTOM_FIELD_DOES_NOT_EXISTS_FOR_ID.getCode()); } try { customFieldUserApi.updateCustomFields(ImmutableList.<CustomField>of(new StringCustomField(customField1.getId(), "wrongName", "value2", customField1.getObjectType(), customField1.getObjectId(), callContext.getCreatedDate())), callContext); Assert.fail("Updating custom field should fail"); } catch (final CustomFieldApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.CUSTOM_FIELD_INVALID_UPDATE.getCode()); } }
@Override public void updateCustomFields(final Iterable<CustomFieldModelDao> customFieldIds, final InternalCallContext context) throws CustomFieldApiException { transactionalSqlDao.execute(false, CustomFieldApiException.class, new EntitySqlDaoTransactionWrapper<Void>() { private void validateCustomField(final CustomFieldModelDao input, @Nullable CustomFieldModelDao existing) throws CustomFieldApiException { if (existing == null) { throw new CustomFieldApiException(ErrorCode.CUSTOM_FIELD_DOES_NOT_EXISTS_FOR_ID, input.getId()); } if (input.getObjectId() != null & !input.getObjectId().equals(existing.getObjectId())) { throw new CustomFieldApiException(ErrorCode.CUSTOM_FIELD_INVALID_UPDATE, input.getId(), input.getObjectId(), "ObjectId"); } if (input.getObjectType() != null && input.getObjectType() != existing.getObjectType()) { throw new CustomFieldApiException(ErrorCode.CUSTOM_FIELD_INVALID_UPDATE, input.getId(), input.getObjectType(), "ObjectType"); } if (input.getFieldName() != null && !input.getFieldName().equals(existing.getFieldName())) { throw new CustomFieldApiException(ErrorCode.CUSTOM_FIELD_INVALID_UPDATE, input.getId(), input.getFieldName(), "FieldName"); } } @Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception { final CustomFieldSqlDao sqlDao = entitySqlDaoWrapperFactory.become(CustomFieldSqlDao.class); for (final CustomFieldModelDao cur : customFieldIds) { final CustomFieldModelDao customField = sqlDao.getById(cur.getId().toString(), context); validateCustomField(cur, customField); sqlDao.updateValue(cur.getId().toString(), cur.getFieldValue(), context); postBusEventFromTransaction(customField, customField, ChangeType.UPDATE, entitySqlDaoWrapperFactory, context); } return null; } }); }
@Override protected CustomFieldApiException generateAlreadyExistsException(final CustomFieldModelDao entity, final InternalCallContext context) { return new CustomFieldApiException(ErrorCode.CUSTOM_FIELD_ALREADY_EXISTS, entity.getId()); }
private void validateCustomField(final CustomFieldModelDao input, @Nullable CustomFieldModelDao existing) throws CustomFieldApiException { if (existing == null) { throw new CustomFieldApiException(ErrorCode.CUSTOM_FIELD_DOES_NOT_EXISTS_FOR_ID, input.getId()); } if (input.getObjectId() != null & !input.getObjectId().equals(existing.getObjectId())) { throw new CustomFieldApiException(ErrorCode.CUSTOM_FIELD_INVALID_UPDATE, input.getId(), input.getObjectId(), "ObjectId"); } if (input.getObjectType() != null && input.getObjectType() != existing.getObjectType()) { throw new CustomFieldApiException(ErrorCode.CUSTOM_FIELD_INVALID_UPDATE, input.getId(), input.getObjectType(), "ObjectType"); } if (input.getFieldName() != null && !input.getFieldName().equals(existing.getFieldName())) { throw new CustomFieldApiException(ErrorCode.CUSTOM_FIELD_INVALID_UPDATE, input.getId(), input.getFieldName(), "FieldName"); } }