@Test public void testStoreExtAttributesForScopeDeletes() { baseServiceWithValidationAndExtScopes.setEntityFieldDAO(entityFieldDAO); ScopeDO scopeDO = new ScopeDO(); Map<String, String> exts = Maps.newHashMap(); exts.put("superAdmin", "true"); exts.put("specialCode", "8675309"); exts.put("secret", ""); scopeDO.setExtAttributes(exts); EntityFieldDO superAdminDO = new EntityFieldDO(); superAdminDO.setCode("superAdmin"); EntityFieldDO specialCodeDO = new EntityFieldDO(); specialCodeDO.setCode("specialCode"); EntityFieldDO secretDO = new EntityFieldDO(); secretDO.setCode("secret"); EntityFieldDO magicKeyDO = new EntityFieldDO(); magicKeyDO.setCode("false"); ScopeExtDO superAdmin = new ScopeExtDO(); superAdmin.setEntityField(superAdminDO); superAdmin.setValue("false"); ScopeExtDO specialCode = new ScopeExtDO(); specialCode.setEntityField(specialCodeDO); specialCode.setValue("9891212"); ScopeExtDO secret = new ScopeExtDO(); secret.setEntityField(secretDO); secret.setValue("password"); ScopeExtDO magicKey = new ScopeExtDO(); magicKey.setEntityField(magicKeyDO); magicKey.setValue("true"); List<ScopeExtDO> mockCurrentList = Lists.newArrayList(); mockCurrentList.add(superAdmin); mockCurrentList.add(specialCode); mockCurrentList.add(secret); mockCurrentList.add(magicKey); ServiceContext context = Mockito.mock(ServiceContext.class); Mockito.when(context.getScopeId()).thenReturn(4L); Mockito.when( entityFieldDAO.findByScopeAndTypeAndCode(Matchers.any(Long.class), Matchers.eq(EntityDAO.EntityTypeCode.SCOPE), Matchers.eq("superAdmin"))).thenReturn(superAdminDO); Mockito.when( entityFieldDAO.findByScopeAndTypeAndCode(Matchers.any(Long.class), Matchers.eq(EntityDAO.EntityTypeCode.SCOPE), Matchers.eq("specialCode"))).thenReturn(specialCodeDO); Mockito.when( entityFieldDAO.findByScopeAndTypeAndCode(Matchers.any(Long.class), Matchers.eq(EntityDAO.EntityTypeCode.SCOPE), Matchers.eq("secret"))).thenReturn(secretDO); Mockito.when( entityFieldDAO.findByScopeAndTypeAndCode(Matchers.any(Long.class), Matchers.eq(EntityDAO.EntityTypeCode.SCOPE), Matchers.eq("magicKey"))).thenReturn(magicKeyDO); Mockito.when(scopeExtDAO.getExtDOs(Matchers.any(ScopeDO.class))).thenReturn(mockCurrentList); Mockito.when(scopeExtDAO.getNew()).thenReturn(new ScopeExtDO()); try { baseServiceWithValidationAndExtScopes.storeExtFields(context, scopeDO, scopeExtDAO, EntityDAO.EntityTypeCode.SCOPE, 1L); } finally { verify(scopeExtDAO, Mockito.times(2)).delete((ScopeExtDO) Matchers.any()); } }
protected void storeExtFields(ServiceContext context, T entityDO, ExtDAO<T, S> extDAO, EntityDAO.EntityTypeCode type, Long scopeId) { if (entityDO.getExtAttributes() == null) { return; } List<S> currentList = extDAO.getExtDOs(entityDO); if (currentList == null) { currentList = Lists.newArrayList(); } for (S extDO : currentList) { if (!entityDO.getExtAttributes().containsKey(extDO.getEntityField().getCode()) || StringUtils.isEmpty(entityDO.getExtAttributes().get(extDO.getEntityField().getCode()))) { extDAO.delete(extDO); } } for (Map.Entry<String, String> entry : entityDO.getExtAttributes().entrySet()) { String attCode = entry.getKey(); String attValue = entry.getValue(); boolean found = false; for (S extDO : currentList) { if (extDO.getEntityField().getCode().equals(attCode)) { found = true; if (!StringUtils.isEmpty(attValue) && !extDO.getValue().equals(attValue)) { extDO.setValue(attValue); extDAO.update(extDO); } break; } } if (!found) { if (!StringUtils.isEmpty(attValue)) { EntityFieldDO entityFieldDO = getEntityFieldByScopeAndTypeAndCode(scopeId, type, attCode); if (entityFieldDO != null) { S extDO = extDAO.getNew(); extDO.setParent(entityDO); extDO.setEntityField(entityFieldDO); extDO.setValue(attValue); extDAO.create(extDO); } } } } }
BaseServiceWithValidationAndExt extends BaseServiceWithValidationImpl { protected void storeExtFields(ServiceContext context, T entityDO, ExtDAO<T, S> extDAO, EntityDAO.EntityTypeCode type, Long scopeId) { if (entityDO.getExtAttributes() == null) { return; } List<S> currentList = extDAO.getExtDOs(entityDO); if (currentList == null) { currentList = Lists.newArrayList(); } for (S extDO : currentList) { if (!entityDO.getExtAttributes().containsKey(extDO.getEntityField().getCode()) || StringUtils.isEmpty(entityDO.getExtAttributes().get(extDO.getEntityField().getCode()))) { extDAO.delete(extDO); } } for (Map.Entry<String, String> entry : entityDO.getExtAttributes().entrySet()) { String attCode = entry.getKey(); String attValue = entry.getValue(); boolean found = false; for (S extDO : currentList) { if (extDO.getEntityField().getCode().equals(attCode)) { found = true; if (!StringUtils.isEmpty(attValue) && !extDO.getValue().equals(attValue)) { extDO.setValue(attValue); extDAO.update(extDO); } break; } } if (!found) { if (!StringUtils.isEmpty(attValue)) { EntityFieldDO entityFieldDO = getEntityFieldByScopeAndTypeAndCode(scopeId, type, attCode); if (entityFieldDO != null) { S extDO = extDAO.getNew(); extDO.setParent(entityDO); extDO.setEntityField(entityFieldDO); extDO.setValue(attValue); extDAO.create(extDO); } } } } } }
BaseServiceWithValidationAndExt extends BaseServiceWithValidationImpl { protected void storeExtFields(ServiceContext context, T entityDO, ExtDAO<T, S> extDAO, EntityDAO.EntityTypeCode type, Long scopeId) { if (entityDO.getExtAttributes() == null) { return; } List<S> currentList = extDAO.getExtDOs(entityDO); if (currentList == null) { currentList = Lists.newArrayList(); } for (S extDO : currentList) { if (!entityDO.getExtAttributes().containsKey(extDO.getEntityField().getCode()) || StringUtils.isEmpty(entityDO.getExtAttributes().get(extDO.getEntityField().getCode()))) { extDAO.delete(extDO); } } for (Map.Entry<String, String> entry : entityDO.getExtAttributes().entrySet()) { String attCode = entry.getKey(); String attValue = entry.getValue(); boolean found = false; for (S extDO : currentList) { if (extDO.getEntityField().getCode().equals(attCode)) { found = true; if (!StringUtils.isEmpty(attValue) && !extDO.getValue().equals(attValue)) { extDO.setValue(attValue); extDAO.update(extDO); } break; } } if (!found) { if (!StringUtils.isEmpty(attValue)) { EntityFieldDO entityFieldDO = getEntityFieldByScopeAndTypeAndCode(scopeId, type, attCode); if (entityFieldDO != null) { S extDO = extDAO.getNew(); extDO.setParent(entityDO); extDO.setEntityField(entityFieldDO); extDO.setValue(attValue); extDAO.create(extDO); } } } } } BaseServiceWithValidationAndExt(); }
BaseServiceWithValidationAndExt extends BaseServiceWithValidationImpl { protected void storeExtFields(ServiceContext context, T entityDO, ExtDAO<T, S> extDAO, EntityDAO.EntityTypeCode type, Long scopeId) { if (entityDO.getExtAttributes() == null) { return; } List<S> currentList = extDAO.getExtDOs(entityDO); if (currentList == null) { currentList = Lists.newArrayList(); } for (S extDO : currentList) { if (!entityDO.getExtAttributes().containsKey(extDO.getEntityField().getCode()) || StringUtils.isEmpty(entityDO.getExtAttributes().get(extDO.getEntityField().getCode()))) { extDAO.delete(extDO); } } for (Map.Entry<String, String> entry : entityDO.getExtAttributes().entrySet()) { String attCode = entry.getKey(); String attValue = entry.getValue(); boolean found = false; for (S extDO : currentList) { if (extDO.getEntityField().getCode().equals(attCode)) { found = true; if (!StringUtils.isEmpty(attValue) && !extDO.getValue().equals(attValue)) { extDO.setValue(attValue); extDAO.update(extDO); } break; } } if (!found) { if (!StringUtils.isEmpty(attValue)) { EntityFieldDO entityFieldDO = getEntityFieldByScopeAndTypeAndCode(scopeId, type, attCode); if (entityFieldDO != null) { S extDO = extDAO.getNew(); extDO.setParent(entityDO); extDO.setEntityField(entityFieldDO); extDO.setValue(attValue); extDAO.create(extDO); } } } } } BaseServiceWithValidationAndExt();  }
BaseServiceWithValidationAndExt extends BaseServiceWithValidationImpl { protected void storeExtFields(ServiceContext context, T entityDO, ExtDAO<T, S> extDAO, EntityDAO.EntityTypeCode type, Long scopeId) { if (entityDO.getExtAttributes() == null) { return; } List<S> currentList = extDAO.getExtDOs(entityDO); if (currentList == null) { currentList = Lists.newArrayList(); } for (S extDO : currentList) { if (!entityDO.getExtAttributes().containsKey(extDO.getEntityField().getCode()) || StringUtils.isEmpty(entityDO.getExtAttributes().get(extDO.getEntityField().getCode()))) { extDAO.delete(extDO); } } for (Map.Entry<String, String> entry : entityDO.getExtAttributes().entrySet()) { String attCode = entry.getKey(); String attValue = entry.getValue(); boolean found = false; for (S extDO : currentList) { if (extDO.getEntityField().getCode().equals(attCode)) { found = true; if (!StringUtils.isEmpty(attValue) && !extDO.getValue().equals(attValue)) { extDO.setValue(attValue); extDAO.update(extDO); } break; } } if (!found) { if (!StringUtils.isEmpty(attValue)) { EntityFieldDO entityFieldDO = getEntityFieldByScopeAndTypeAndCode(scopeId, type, attCode); if (entityFieldDO != null) { S extDO = extDAO.getNew(); extDO.setParent(entityDO); extDO.setEntityField(entityFieldDO); extDO.setValue(attValue); extDAO.create(extDO); } } } } } BaseServiceWithValidationAndExt();   }