Skip to content

Commit

Permalink
Refactoring of EventClass field ecf
Browse files Browse the repository at this point in the history
  • Loading branch information
nocs00 committed Jul 27, 2016
1 parent cae304f commit 09bd9bd
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 65 deletions.
Expand Up @@ -37,6 +37,7 @@ public class DaoConstants {
public static final String TOPICS_PROPERTY = "topics";
public static final String CONFIGURATION_SCHEMA_PROPERTY = "configurationSchema";
public static final String ECF_PROPERTY = "ecf";
public static final String ECFV_PROPERTY = "ecfv";
public static final String VERSION_PROPERTY = "version";
public static final String SCHEMA_VERSION_PROPERTY = "schemaVersion";
public static final String EVENT_CLASS_TYPE_PROPERTY = "type";
Expand All @@ -53,6 +54,7 @@ public class DaoConstants {
public static final String TOPIC_ALIAS = "topic";
public static final String CONFIGURATION_SCHEMA_ALIAS = "configurationSchema";
public static final String ECF_ALIAS = "ecf";
public static final String ECFV_ALIAS = "ecfv";

public static final String TENANT_REFERENCE = TENANT_ALIAS + "." + ID_PROPERTY;
public static final String ENDPOINT_PROFILE_SCHEMA_REFERENCE = ENDPOINT_PROFILE_SCHEMA_ALIAS + "." + ID_PROPERTY;
Expand All @@ -64,6 +66,7 @@ public class DaoConstants {
public static final String TOPIC_REFERENCE = TOPIC_ALIAS + "." + ID_PROPERTY;
public static final String CONFIGURATION_SCHEMA_REFERENCE = CONFIGURATION_SCHEMA_ALIAS + "." + ID_PROPERTY;
public static final String ECF_REFERENCE = ECF_ALIAS + "." + ID_PROPERTY;
public static final String ECFV_REFERENCE = ECFV_ALIAS + "." + ID_PROPERTY;

public static final String OPTIMISTIC_LOCK_PROPERTY = "OPT_LOCK";

Expand Down
Expand Up @@ -44,6 +44,14 @@ public interface EventClassService {
*/
EventClassFamilyDto findEventClassFamilyById(String id);

/**
* Find event class family by ECF version id.
*
* @param id the string id of event class family version
* @return the event class family dto object
*/
EventClassFamilyDto findEventClassFamilyByECFVersionId(String id);

/**
* Find event class family versions by id.
*
Expand Down
Expand Up @@ -33,7 +33,7 @@ public interface EventClassDao<T> extends SqlDao<T> {
* @param id the user id
* @return the list of users
*/
List<T> findByEcfId(String id);
List<T> findByEcfvId(String id);

/**
* Find all Event Classes by Event Class Family id, version and type.
Expand All @@ -43,14 +43,14 @@ public interface EventClassDao<T> extends SqlDao<T> {
* @param type the type
* @return the list of event classes
*/
List<T> findByEcfIdVersionAndType(String ecfId, int version, EventClassType type);
List<T> findByEcfvIdVersionAndType(String ecfId, int version, EventClassType type);

/**
* Remove all Event Classes by Event Class Family id.
*
* @param tenantId the tenant id
*/
void removeByEcfId(String tenantId);
void removeByEcfvId(String tenantId);

/**
* Find Event Class by Tenant id and FQN.
Expand Down
Expand Up @@ -35,6 +35,9 @@
import static org.kaaproject.kaa.server.common.dao.DaoConstants.ECF_ALIAS;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.ECF_PROPERTY;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.ECF_REFERENCE;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.ECFV_ALIAS;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.ECFV_PROPERTY;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.ECFV_REFERENCE;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.EVENT_CLASS_TYPE_PROPERTY;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.FQN_PROPERTY;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.TENANT_ALIAS;
Expand All @@ -53,50 +56,50 @@ protected Class<EventClass> getEntityClass() {
}

@Override
public List<EventClass> findByEcfId(String ecfId) {
public List<EventClass> findByEcfvId(String ecfvId) {
List<EventClass> eventClasses = Collections.emptyList();
LOG.debug("Searching event classes by ecf id [{}] ", ecfId);
if (isNotBlank(ecfId)) {
eventClasses = findListByCriterionWithAlias(ECF_PROPERTY, ECF_ALIAS, Restrictions.eq(ECF_REFERENCE, Long.valueOf(ecfId)));
LOG.debug("Searching event classes by ecfv id [{}] ", ecfvId);
if (isNotBlank(ecfvId)) {
eventClasses = findListByCriterionWithAlias(ECFV_PROPERTY, ECFV_ALIAS, Restrictions.eq(ECFV_REFERENCE, Long.valueOf(ecfvId)));
}
if (LOG.isTraceEnabled()) {
LOG.trace("[{}] Search result: {}.", ecfId, Arrays.toString(eventClasses.toArray()));
LOG.trace("[{}] Search result: {}.", ecfvId, Arrays.toString(eventClasses.toArray()));
} else {
LOG.debug("[{}] Search result: {}.", ecfId, eventClasses.size());
LOG.debug("[{}] Search result: {}.", ecfvId, eventClasses.size());
}
return eventClasses;
}

@Override
public List<EventClass> findByEcfIdVersionAndType(String ecfId, int version, EventClassType type) {
LOG.debug("Searching event class by ecf id [{}] version [{}] and type [{}]", ecfId, version, type);
public List<EventClass> findByEcfvIdVersionAndType(String ecfvId, int version, EventClassType type) {
LOG.debug("Searching event class by ecfv id [{}] version [{}] and type [{}]", ecfvId, version, type);
List<EventClass> eventClasses = Collections.emptyList();
if (isNotBlank(ecfId)) {
if (isNotBlank(ecfvId)) {
List<Criterion> predicates = new ArrayList<>();
predicates.add(Restrictions.eq(ECF_REFERENCE, Long.valueOf(ecfId)));
predicates.add(Restrictions.eq(ECFV_REFERENCE, Long.valueOf(ecfvId)));
predicates.add(Restrictions.eq(VERSION_PROPERTY, version));
if (type != null) {
predicates.add(Restrictions.eq(EVENT_CLASS_TYPE_PROPERTY, type));
}
eventClasses = findListByCriterionWithAlias(ECF_PROPERTY, ECF_ALIAS,
eventClasses = findListByCriterionWithAlias(ECFV_PROPERTY, ECFV_ALIAS,
Restrictions.and(predicates.toArray(new Criterion[]{})));
}
if (LOG.isTraceEnabled()) {
LOG.trace("[{},{},{}] Search result: {}.", ecfId, version, type, Arrays.toString(eventClasses.toArray()));
LOG.trace("[{},{},{}] Search result: {}.", ecfvId, version, type, Arrays.toString(eventClasses.toArray()));
} else {
LOG.debug("[{},{},{}] Search result: {}.", ecfId, version, type, eventClasses.size());
LOG.debug("[{},{},{}] Search result: {}.", ecfvId, version, type, eventClasses.size());
}
return eventClasses;
}

@Override
public void removeByEcfId(String ecfId) {
if (isNotBlank(ecfId)) {
List<EventClass> eventClasses = findListByCriterionWithAlias(ECF_PROPERTY, ECF_ALIAS,
Restrictions.eq(ECF_REFERENCE, Long.valueOf(ecfId)));
public void removeByEcfvId(String ecfvId) {
if (isNotBlank(ecfvId)) {
List<EventClass> eventClasses = findListByCriterionWithAlias(ECFV_PROPERTY, ECFV_ALIAS,
Restrictions.eq(ECFV_REFERENCE, Long.valueOf(ecfvId)));
removeList(eventClasses);
}
LOG.debug("Removed event class by ecf id [{}] ", ecfId);
LOG.debug("Removed event class by ecfv id [{}] ", ecfvId);
}

@Override
Expand Down Expand Up @@ -148,10 +151,10 @@ public boolean validateFqns(String tenantId, String ecfId, List<String> fqns) {
}
Criteria criteria = getCriteria();
criteria.createAlias(TENANT_PROPERTY, TENANT_ALIAS);
criteria.createAlias(ECF_PROPERTY, ECF_ALIAS);
criteria.createAlias(ECFV_PROPERTY, ECFV_ALIAS);
criteria.add(Restrictions.and(
Restrictions.eq(TENANT_REFERENCE, Long.valueOf(tenantId)),
Restrictions.ne(ECF_REFERENCE, Long.valueOf(ecfId)),
Restrictions.ne(ECFV_REFERENCE, Long.valueOf(ecfId)),
Restrictions.in(FQN_PROPERTY, fqns)));
eventClasses = findListByCriteria(criteria);
}
Expand Down
Expand Up @@ -49,7 +49,7 @@ public class EventClass extends BaseSchema<EventClassDto> {

@ManyToOne
@OnDelete(action = OnDeleteAction.CASCADE)
private EventClassFamilyVersion ecf;
private EventClassFamilyVersion ecfv;

@Column(name = EVENT_CLASS_FQN)
private String fqn;
Expand Down Expand Up @@ -86,12 +86,12 @@ public void setTenant(Tenant tenant) {
this.tenant = tenant;
}

public EventClassFamilyVersion getEcf() {
return ecf;
public EventClassFamilyVersion getEcfv() {
return ecfv;
}

public void setEcf(EventClassFamilyVersion ecf) {
this.ecf = ecf;
public void setEcfv(EventClassFamilyVersion ecfv) {
this.ecfv = ecfv;
}

public String getFqn() {
Expand All @@ -114,7 +114,7 @@ public void setType(EventClassType type) {
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((ecf == null) ? 0 : ecf.hashCode());
result = prime * result + ((ecfv == null) ? 0 : ecfv.hashCode());
result = prime * result + ((fqn == null) ? 0 : fqn.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((getCtlSchema() == null) ? 0 : getCtlSchema().hashCode());
Expand All @@ -135,11 +135,11 @@ public boolean equals(Object obj) {
return false;
}
EventClass other = (EventClass) obj;
if (ecf == null) {
if (other.ecf != null) {
if (ecfv == null) {
if (other.ecfv != null) {
return false;
}
} else if (!ecf.equals(other.ecf)) {
} else if (!ecfv.equals(other.ecfv)) {
return false;
}
if (fqn == null) {
Expand Down Expand Up @@ -197,8 +197,8 @@ public EventClassDto toDto() {
if (tenant != null) {
dto.setTenantId(tenant.getStringId());
}
if (ecf != null) {
dto.setEcfId(ecf.getStringId());
if (ecfv != null) {
dto.setEcfvId(ecfv.getStringId());
}
dto.setFqn(fqn);
dto.setType(type);
Expand All @@ -209,7 +209,7 @@ public EventClassDto toDto() {

@Override
public String toString() {
return "EventClass [ecf=" + ecf + ", fqn=" + fqn + ", type=" + type + ", ctlSchema=" + getCtlSchema() + ", id=" + id + "]";
return "EventClass [ecfv=" + ecfv + ", fqn=" + fqn + ", type=" + type + ", ctlSchema=" + getCtlSchema() + ", id=" + id + "]";
}


Expand Down
Expand Up @@ -26,6 +26,7 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -97,6 +98,17 @@ public EventClassFamilyDto findEventClassFamilyById(String id) {
return getDto(eventClassFamilyDao.findById(id));
}

@Override
public EventClassFamilyDto findEventClassFamilyByECFVersionId(String id) {
validateSqlId(id, "Event class family version id is incorrect. Can't find event class family by ECF version id " + 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
public List<EventClassFamilyVersionDto> findEventClassFamilyVersionsById(String id) {
validateSqlId(id, "Event class family id is incorrect. Can't find event class family by id " + id);
Expand Down Expand Up @@ -191,7 +203,7 @@ private void setEventClassProperties(EventClassFamilyDto eventClassFamilyDto, Ev
}

private void setBackreference(List<EventClassFamilyVersion> ecfvList) {
ecfvList.forEach(ecfv -> ecfv.getRecords().forEach(ec -> ec.setEcf(ecfv)));
ecfvList.forEach(ecfv -> ecfv.getRecords().forEach(ec -> ec.setEcfv(ecfv)));
}

private boolean validateEventClassFamilyFqns(EventClassFamilyDto eventClassFamily, List<String> fqns) {
Expand All @@ -205,7 +217,7 @@ public List<EventClassDto> findEventClassesByFamilyIdVersionAndType(String ecfId
LOG.debug("Find event classes by family id [{}] version [{}] and type [{}]", ecfId, version, type);
EventClassFamily ecf = eventClassFamilyDao.findById(ecfId);
EventClassFamilyVersion ecfv = ecf.getSchemas().stream().filter(s -> s.getVersion() == version).collect(Collectors.toList()).get(0);
eventClasses = convertDtoList(eventClassDao.findByEcfIdVersionAndType(String.valueOf(ecfv.getId()), ecfv.getVersion(), type));
eventClasses = convertDtoList(eventClassDao.findByEcfvIdVersionAndType(String.valueOf(ecfv.getId()), ecfv.getVersion(), type));
} else {
throw new IncorrectParameterException("Incorrect event class family id: " + ecfId);
}
Expand Down
Expand Up @@ -30,7 +30,6 @@
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

import org.junit.Assert;
import org.kaaproject.kaa.common.dto.KaaAuthorityDto;
Expand All @@ -40,7 +39,6 @@
import org.kaaproject.kaa.common.dto.ctl.CTLSchemaScopeDto;
import org.kaaproject.kaa.common.dto.event.ApplicationEventAction;
import org.kaaproject.kaa.common.dto.event.EventClassType;
import org.kaaproject.kaa.server.common.core.schema.KaaSchemaFactoryImpl;
import org.kaaproject.kaa.server.common.dao.AbstractTest;
import org.kaaproject.kaa.server.common.dao.model.sql.Application;
import org.kaaproject.kaa.server.common.dao.model.sql.ApplicationEventFamilyMap;
Expand Down Expand Up @@ -418,7 +416,7 @@ protected List<EventClassFamilyVersion> generateEventClassFamilyVersion(EventCla
}

ec.setCtlSchema(generateCTLSchema(DEFAULT_FQN, version, eventClassFamily.getTenant(), CTLSchemaScopeDto.TENANT));
ec.setEcf(ecfv);
ec.setEcfv(ecfv);
ec.setFqn("Test FQN" + RANDOM.nextInt());
ec.setType(EventClassType.EVENT);
ecList.add(ec);
Expand Down
Expand Up @@ -37,15 +37,15 @@
public class HibernateEventClassDaoTest extends HibernateAbstractTest {

@Test
public void removeByEcfId() {
public void removeByEcfvId() {
List<EventClass> eventClasses = generateEventClass(null, null, 2);
String id = eventClasses.get(0).getStringId();
EventClass dto = eventClassDao.findById(id);
Assert.assertNotNull(dto);
String ecfId = dto.getEcf().getStringId();
Assert.assertNotNull(ecfId);
String ecfvId = dto.getEcfv().getStringId();
Assert.assertNotNull(ecfvId);

eventClassDao.removeByEcfId(ecfId);
eventClassDao.removeByEcfvId(ecfvId);
dto = eventClassDao.findById(id);
Assert.assertNull(dto);
}
Expand All @@ -67,7 +67,7 @@ public void findByEcfIdTest() {
List<EventClass> eventClasses = generateEventClass(null, null, 2);
EventClass dto = eventClassDao.findById(eventClasses.get(0).getStringId());
Assert.assertNotNull(dto);
List<EventClass> eventClassesList = eventClassDao.findByEcfId(dto.getEcf().getStringId());
List<EventClass> eventClassesList = eventClassDao.findByEcfvId(dto.getEcfv().getStringId());
EventClass eventClass = null;
for (EventClass found : eventClassesList) {
if (dto.getId().equals(found.getId())) {
Expand Down
Expand Up @@ -24,7 +24,7 @@ public class EventClassDto extends BaseSchemaDto {
private static final long serialVersionUID = 2052580632293959408L;

private String tenantId;
private String ecfId;
private String ecfvId;
private String fqn;
private EventClassType type;

Expand All @@ -36,12 +36,12 @@ public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}

public String getEcfId() {
return ecfId;
public String getEcfvId() {
return ecfvId;
}

public void setEcfId(String ecfId) {
this.ecfId = ecfId;
public void setEcfvId(String ecfvId) {
this.ecfvId = ecfvId;
}

public String getFqn() {
Expand All @@ -64,7 +64,7 @@ public void setType(EventClassType type) {
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((ecfId == null) ? 0 : ecfId.hashCode());
result = prime * result + ((ecfvId == null) ? 0 : ecfvId.hashCode());
result = prime * result + ((fqn == null) ? 0 : fqn.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result
Expand All @@ -87,11 +87,11 @@ public boolean equals(Object obj) {
return false;
}
EventClassDto other = (EventClassDto) obj;
if (ecfId == null) {
if (other.ecfId != null) {
if (ecfvId == null) {
if (other.ecfvId != null) {
return false;
}
} else if (!ecfId.equals(other.ecfId)) {
} else if (!ecfvId.equals(other.ecfvId)) {
return false;
}
if (fqn == null) {
Expand Down Expand Up @@ -136,7 +136,7 @@ public String toString() {
builder.append(", tenantId=");
builder.append(tenantId);
builder.append(", ecfId=");
builder.append(ecfId);
builder.append(ecfvId);
builder.append(", fqn=");
builder.append(fqn);
builder.append(", type=");
Expand Down

0 comments on commit 09bd9bd

Please sign in to comment.