Skip to content

Commit

Permalink
KAA-1146: fixed comments to PR
Browse files Browse the repository at this point in the history
  • Loading branch information
nocs00 committed Aug 18, 2016
1 parent 9531b2e commit f3f4f7e
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 41 deletions.
Expand Up @@ -17,6 +17,7 @@
package org.kaaproject.kaa.server.common.dao; package org.kaaproject.kaa.server.common.dao;


import java.util.List; import java.util.List;
import java.util.Set;


import org.kaaproject.kaa.common.dto.event.AefMapInfoDto; import org.kaaproject.kaa.common.dto.event.AefMapInfoDto;
import org.kaaproject.kaa.common.dto.event.EventClassDto; import org.kaaproject.kaa.common.dto.event.EventClassDto;
Expand Down Expand Up @@ -149,5 +150,5 @@ public interface EventClassService {
* @param ecfId string of the event class family id * @param ecfId string of the event class family id
* @return list of all FQNs * @return list of all FQNs
*/ */
List<String> getFqnListForECF(String ecfId); Set<String> getFqnListForECF(String ecfId);
} }
Expand Up @@ -41,6 +41,14 @@ public interface EventClassFamilyDao<T> extends SqlDao<T> {
*/ */
T findByTenantIdAndName(String tenantId, String name); T findByTenantIdAndName(String tenantId, String name);


/**
* Find EventClassFamily by ECF version id.
*
* @param ecfvId id of ECF version
* @return EventClassFamily
*/
T findByEcfvId(String ecfvId);

/** /**
* Remove all EventClassFamily objects by tenant id. * Remove all EventClassFamily objects by tenant id.
* *
Expand Down
Expand Up @@ -17,6 +17,7 @@
package org.kaaproject.kaa.server.common.dao.impl.sql; package org.kaaproject.kaa.server.common.dao.impl.sql;


import org.hibernate.Criteria; import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions; import org.hibernate.criterion.Restrictions;
import org.kaaproject.kaa.server.common.dao.impl.EventClassFamilyDao; import org.kaaproject.kaa.server.common.dao.impl.EventClassFamilyDao;
import org.kaaproject.kaa.server.common.dao.model.sql.EventClassFamily; import org.kaaproject.kaa.server.common.dao.model.sql.EventClassFamily;
Expand All @@ -29,12 +30,7 @@
import java.util.List; import java.util.List;


import static org.apache.commons.lang.StringUtils.isNotBlank; import static org.apache.commons.lang.StringUtils.isNotBlank;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.CLASS_NAME_PROPERTY; import static org.kaaproject.kaa.server.common.dao.DaoConstants.*;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.ID_PROPERTY;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.NAME_PROPERTY;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.TENANT_ALIAS;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.TENANT_PROPERTY;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.TENANT_REFERENCE;


@Repository @Repository
public class HibernateEventClassFamilyDao extends HibernateAbstractDao<EventClassFamily> implements EventClassFamilyDao<EventClassFamily> { public class HibernateEventClassFamilyDao extends HibernateAbstractDao<EventClassFamily> implements EventClassFamilyDao<EventClassFamily> {
Expand Down Expand Up @@ -79,6 +75,22 @@ public EventClassFamily findByTenantIdAndName(String tenantId, String name) {
return eventClassFamily; return eventClassFamily;
} }


@Override
public EventClassFamily findByEcfvId(String ecfvId) {
LOG.debug("Searching event class family by ecfv id [{}]", ecfvId);

Query query = getSession().createSQLQuery("select ecf.*" +
" from " + EVENT_CLASS_FAMILY_TABLE_NAME + " as ecf" +
" join " + EVENT_CLASS_FAMILY_VERSION_TABLE_NAME + " as ecfv" +
" on ecf.id = ecfv." + EVENT_CLASS_FAMILY_ID +
" where ecfv.id = :id").addEntity(getEntityClass());
query.setLong("id", Long.valueOf(ecfvId));
EventClassFamily eventClassFamily = (EventClassFamily)query.uniqueResult();

LOG.debug("[{}] Search result: {}.", ecfvId, eventClassFamily);
return eventClassFamily;
}

@Override @Override
public void removeByTenantId(String tenantId) { public void removeByTenantId(String tenantId) {
if (isNotBlank(tenantId)) { if (isNotBlank(tenantId)) {
Expand Down
Expand Up @@ -25,7 +25,10 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;


import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -101,12 +104,7 @@ public EventClassFamilyDto findEventClassFamilyById(String id) {
@Override @Override
public EventClassFamilyDto findEventClassFamilyByEcfvId(String id) { public EventClassFamilyDto findEventClassFamilyByEcfvId(String id) {
validateSqlId(id, "Event class family version id is incorrect. Can't find event class family by ECF version id " + id); validateSqlId(id, "Event class family version id is incorrect. Can't find event class family by ECF version id " + id);

return getDto(eventClassFamilyDao.findByEcfvId(id));
EventClassFamily eventClassFamily = eventClassFamilyDao.find().stream().
filter(ecf -> ecf.getSchemas().stream()
.filter(ecfv -> ecfv.getStringId().equals(id)).count() > 0)
.findFirst().get();
return getDto(eventClassFamily);
} }


@Override @Override
Expand Down Expand Up @@ -208,23 +206,31 @@ private void setBackreference(List<EventClassFamilyVersion> ecfvList) {


@Override @Override
public boolean validateEventClassFamilyFqns(String eventClassFamilyId, List<String> fqns) { public boolean validateEventClassFamilyFqns(String eventClassFamilyId, List<String> fqns) {
List<String> storedFQNs = getFqnListForECF(eventClassFamilyId); Set<String> storedFQNs = getFqnListForECF(eventClassFamilyId);
for (String storedFQN : storedFQNs) { for (String fqn : fqns) {
long duplicatedFqnCount = fqns.stream().filter(fqn -> fqn.equals(storedFQN)).count(); if (storedFQNs.contains(fqn)) return false;
if (duplicatedFqnCount > 0) return false;
} }

return true; return true;
} }


@Override @Override
public List<EventClassDto> findEventClassesByFamilyIdVersionAndType(String ecfId, int version, EventClassType type) { public List<EventClassDto> findEventClassesByFamilyIdVersionAndType(String ecfId, int version, EventClassType type) {
List<EventClassDto> eventClasses; List<EventClassDto> eventClasses = new ArrayList<>();
if (isValidSqlId(ecfId)) { if (isValidSqlId(ecfId)) {
LOG.debug("Find event classes by family id [{}] version [{}] and type [{}]", ecfId, version, type); LOG.debug("Find event classes by family id [{}] version [{}] and type [{}]", ecfId, version, type);
EventClassFamily ecf = eventClassFamilyDao.findById(ecfId); EventClassFamily ecf = eventClassFamilyDao.findById(ecfId);
EventClassFamilyVersion ecfv = ecf.getSchemas().stream().filter(s -> s.getVersion() == version).collect(Collectors.toList()).get(0); Optional<EventClassFamilyVersion> ecfv = ecf.getSchemas().stream().filter(s -> s.getVersion() == version).findFirst();
eventClasses = convertDtoList(ecfv.getRecords().stream().filter(ec -> type != null ? ec.getType() == type : true).collect(Collectors.toList()));
if (type == null) {
ecfv.ifPresent(
e -> eventClasses.addAll(convertDtoList(e.getRecords())));
}
else {
ecfv.ifPresent(
e -> eventClasses.addAll(convertDtoList(e.getRecords().stream()
.filter(ec -> ec.getType() == type)
.collect(Collectors.toList()))));
}
} else { } else {
throw new IncorrectParameterException("Incorrect event class family id: " + ecfId); throw new IncorrectParameterException("Incorrect event class family id: " + ecfId);
} }
Expand Down Expand Up @@ -263,28 +269,28 @@ public EventClassDto findEventClassById(String eventClassId) {
} }


@Override @Override
public boolean isValidECFListInSdkProfile(List<AefMapInfoDto> ecfList) { public boolean isValidECFListInSdkProfile(List<AefMapInfoDto> aefList) {
List<EventClass> ecList = new ArrayList<>(); Set<EventClass> ecList = new HashSet<>();
for (AefMapInfoDto ecfMap : ecfList) { for (AefMapInfoDto aef : aefList) {
EventClassFamily ecf = eventClassFamilyDao.findById(ecfMap.getEcfId()); EventClassFamily ecf = eventClassFamilyDao.findById(aef.getEcfId());
ecf.getSchemas().forEach(ecfv -> ecList.addAll(ecfv.getRecords()));
}


for (EventClass ec : ecList) { Optional<EventClassFamilyVersion> optEcfv = ecf.getSchemas().stream()
long fqnMatches = ecList.stream().filter(ec2 -> ec2.getFqn().equals(ec.getFqn())).count(); .filter(ecfv -> ecfv.getVersion() == aef.getVersion())
if (fqnMatches > 1) { .findFirst();
return false; if (optEcfv.isPresent()) {
for (EventClass ec : optEcfv.get().getRecords()) {
if (!ecList.add(ec)) return false;
}
} }
} }

return true; return true;
} }


@Override @Override
public List<String> getFqnListForECF(String ecfId) { public Set<String> getFqnListForECF(String ecfId) {
if (isValidSqlId(ecfId)) { if (isValidSqlId(ecfId)) {
LOG.debug("Get fqn list for event class family by id [{}] ", ecfId); LOG.debug("Get fqn list for event class family by id [{}] ", ecfId);
List<String> storedFQNs = new ArrayList<>(); Set<String> storedFQNs = new HashSet<>();
EventClassFamily ecf = eventClassFamilyDao.findById(ecfId); EventClassFamily ecf = eventClassFamilyDao.findById(ecfId);
ecf.getSchemas().forEach(ecfv -> ecfv.getRecords().forEach(ec -> storedFQNs.add(ec.getFqn()))); ecf.getSchemas().forEach(ecfv -> ecfv.getRecords().forEach(ec -> storedFQNs.add(ec.getFqn())));
return storedFQNs; return storedFQNs;
Expand Down
Expand Up @@ -27,7 +27,7 @@
import java.util.Date; import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;


Expand Down Expand Up @@ -408,12 +408,10 @@ protected List<EventClassFamilyVersion> generateEventClassFamilyVersion(EventCla
for (int j = 0; j < ecCount; j++) { for (int j = 0; j < ecCount; j++) {
EventClass ec = new EventClass(); EventClass ec = new EventClass();
ec.setTenant(eventClassFamily.getTenant()); ec.setTenant(eventClassFamily.getTenant());
Integer version;
try { Optional<CTLSchema> ctlMaxVersion = ctlSchemaDao.find().stream()
version = ctlSchemaDao.find().stream().max((ctl1, ctl2) -> Integer.compare(ctl1.getVersion(), ctl2.getVersion())).get().getVersion()+1; .max((ctl1, ctl2) -> Integer.compare(ctl1.getVersion(), ctl2.getVersion()));
} catch (NoSuchElementException e) { int version = ctlMaxVersion.isPresent() ? (ctlMaxVersion.get().getVersion() + 1) : 1;
version = 1;
}


ec.setCtlSchema(generateCTLSchema(DEFAULT_FQN, version, eventClassFamily.getTenant(), CTLSchemaScopeDto.TENANT)); ec.setCtlSchema(generateCTLSchema(DEFAULT_FQN, version, eventClassFamily.getTenant(), CTLSchemaScopeDto.TENANT));
ec.setEcfv(ecfv); ec.setEcfv(ecfv);
Expand Down
Expand Up @@ -22,6 +22,7 @@
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.kaaproject.kaa.server.common.dao.model.sql.EventClassFamily; import org.kaaproject.kaa.server.common.dao.model.sql.EventClassFamily;
import org.kaaproject.kaa.server.common.dao.model.sql.EventClassFamilyVersion;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Expand Down Expand Up @@ -83,4 +84,18 @@ public void findByTenantIdAndNameTest() {
Assert.assertNotNull(eventClassFamily); Assert.assertNotNull(eventClassFamily);
Assert.assertEquals(dto, eventClassFamily); Assert.assertEquals(dto, eventClassFamily);
} }

@Test
public void findByEcfvIdTest() {
List<EventClassFamily> eventClassFamilies = generateEventClassFamily(null, 1);
EventClassFamily ecf = eventClassFamilies.get(0);
List<EventClassFamilyVersion> ecfvList = generateEventClassFamilyVersion(ecf, 1, 1);
ecf.setSchemas(ecfvList);
ecf = eventClassFamilyDao.save(ecf);

EventClassFamilyVersion ecfv = ecfvList.get(0);
EventClassFamily ecfByEcfv = eventClassFamilyDao.findByEcfvId(ecfv.getStringId());
Assert.assertNotNull(ecfByEcfv);
Assert.assertEquals(ecf, ecfByEcfv);
}
} }

0 comments on commit f3f4f7e

Please sign in to comment.