Merge pull request #11 from rbkmoney/ft/NEW-13/dmt_categories

NEW-13: Saving category object
This commit is contained in:
Inal Arsanukaev 2018-09-12 12:44:52 +03:00 committed by GitHub
commit 8034b79a1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 291 additions and 67 deletions

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>newway</artifactId>
<version>1.0.8-SNAPSHOT</version>
<version>1.0.9-SNAPSHOT</version>
<packaging>jar</packaging>
<name>newway</name>

View File

@ -9,7 +9,5 @@ public interface CategoryDao extends GenericDao {
Long save(Category category) throws DaoException;
Category get(Integer categoryId) throws DaoException;
void updateNotCurrent(Integer categoryId) throws DaoException;
}

View File

@ -1,13 +1,11 @@
package com.rbkmoney.newway.dao.dominant.impl;
import com.rbkmoney.newway.dao.common.impl.AbstractGenericDao;
import com.rbkmoney.newway.dao.common.mapper.RecordRowMapper;
import com.rbkmoney.newway.dao.dominant.iface.CategoryDao;
import com.rbkmoney.newway.domain.tables.pojos.Category;
import com.rbkmoney.newway.domain.tables.records.CategoryRecord;
import com.rbkmoney.newway.exception.DaoException;
import org.jooq.Query;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Component;
@ -18,11 +16,8 @@ import static com.rbkmoney.newway.domain.Tables.CATEGORY;
@Component
public class CategoryDaoImpl extends AbstractGenericDao implements CategoryDao {
private final RowMapper<Category> categoryRowMapper;
public CategoryDaoImpl(DataSource dataSource) {
super(dataSource);
this.categoryRowMapper = new RecordRowMapper<>(CATEGORY, Category.class);
}
@Override
@ -34,14 +29,6 @@ public class CategoryDaoImpl extends AbstractGenericDao implements CategoryDao {
return keyHolder.getKey().longValue();
}
@Override
public Category get(Integer categoryId) throws DaoException {
Query query = getDslContext().selectFrom(CATEGORY)
.where(CATEGORY.CATEGORY_ID.eq(categoryId).and(CATEGORY.CURRENT));
return fetchOne(query, categoryRowMapper);
}
@Override
public void updateNotCurrent(Integer categoryId) throws DaoException {
Query query = getDslContext().update(CATEGORY).set(CATEGORY.CURRENT, false)

View File

@ -36,7 +36,7 @@ import org.jooq.impl.TableImpl;
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Category extends TableImpl<CategoryRecord> {
private static final long serialVersionUID = -1456182321;
private static final long serialVersionUID = 1729394875;
/**
* The reference instance of <code>nw.category</code>
@ -66,6 +66,21 @@ public class Category extends TableImpl<CategoryRecord> {
*/
public final TableField<CategoryRecord, Integer> CATEGORY_ID = createField("category_id", org.jooq.impl.SQLDataType.INTEGER.nullable(false), this, "");
/**
* The column <code>nw.category.name</code>.
*/
public final TableField<CategoryRecord, String> NAME = createField("name", org.jooq.impl.SQLDataType.VARCHAR.nullable(false), this, "");
/**
* The column <code>nw.category.description</code>.
*/
public final TableField<CategoryRecord, String> DESCRIPTION = createField("description", org.jooq.impl.SQLDataType.VARCHAR.nullable(false), this, "");
/**
* The column <code>nw.category.type</code>.
*/
public final TableField<CategoryRecord, String> TYPE = createField("type", org.jooq.impl.SQLDataType.VARCHAR, this, "");
/**
* The column <code>nw.category.wtime</code>.
*/

View File

@ -23,11 +23,14 @@ import javax.annotation.Generated;
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Category implements Serializable {
private static final long serialVersionUID = 4913789;
private static final long serialVersionUID = -1860168428;
private Long id;
private Long versionId;
private Integer categoryId;
private String name;
private String description;
private String type;
private LocalDateTime wtime;
private Boolean current;
@ -37,6 +40,9 @@ public class Category implements Serializable {
this.id = value.id;
this.versionId = value.versionId;
this.categoryId = value.categoryId;
this.name = value.name;
this.description = value.description;
this.type = value.type;
this.wtime = value.wtime;
this.current = value.current;
}
@ -45,12 +51,18 @@ public class Category implements Serializable {
Long id,
Long versionId,
Integer categoryId,
String name,
String description,
String type,
LocalDateTime wtime,
Boolean current
) {
this.id = id;
this.versionId = versionId;
this.categoryId = categoryId;
this.name = name;
this.description = description;
this.type = type;
this.wtime = wtime;
this.current = current;
}
@ -79,6 +91,30 @@ public class Category implements Serializable {
this.categoryId = categoryId;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
public LocalDateTime getWtime() {
return this.wtime;
}
@ -122,6 +158,24 @@ public class Category implements Serializable {
}
else if (!categoryId.equals(other.categoryId))
return false;
if (name == null) {
if (other.name != null)
return false;
}
else if (!name.equals(other.name))
return false;
if (description == null) {
if (other.description != null)
return false;
}
else if (!description.equals(other.description))
return false;
if (type == null) {
if (other.type != null)
return false;
}
else if (!type.equals(other.type))
return false;
if (wtime == null) {
if (other.wtime != null)
return false;
@ -144,6 +198,9 @@ public class Category implements Serializable {
result = prime * result + ((this.id == null) ? 0 : this.id.hashCode());
result = prime * result + ((this.versionId == null) ? 0 : this.versionId.hashCode());
result = prime * result + ((this.categoryId == null) ? 0 : this.categoryId.hashCode());
result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
result = prime * result + ((this.description == null) ? 0 : this.description.hashCode());
result = prime * result + ((this.type == null) ? 0 : this.type.hashCode());
result = prime * result + ((this.wtime == null) ? 0 : this.wtime.hashCode());
result = prime * result + ((this.current == null) ? 0 : this.current.hashCode());
return result;
@ -156,6 +213,9 @@ public class Category implements Serializable {
sb.append(id);
sb.append(", ").append(versionId);
sb.append(", ").append(categoryId);
sb.append(", ").append(name);
sb.append(", ").append(description);
sb.append(", ").append(type);
sb.append(", ").append(wtime);
sb.append(", ").append(current);

View File

@ -12,8 +12,8 @@ import javax.annotation.Generated;
import org.jooq.Field;
import org.jooq.Record1;
import org.jooq.Record5;
import org.jooq.Row5;
import org.jooq.Record8;
import org.jooq.Row8;
import org.jooq.impl.UpdatableRecordImpl;
@ -28,9 +28,9 @@ import org.jooq.impl.UpdatableRecordImpl;
comments = "This class is generated by jOOQ"
)
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class CategoryRecord extends UpdatableRecordImpl<CategoryRecord> implements Record5<Long, Long, Integer, LocalDateTime, Boolean> {
public class CategoryRecord extends UpdatableRecordImpl<CategoryRecord> implements Record8<Long, Long, Integer, String, String, String, LocalDateTime, Boolean> {
private static final long serialVersionUID = 124038482;
private static final long serialVersionUID = 1401274706;
/**
* Setter for <code>nw.category.id</code>.
@ -74,32 +74,74 @@ public class CategoryRecord extends UpdatableRecordImpl<CategoryRecord> implemen
return (Integer) get(2);
}
/**
* Setter for <code>nw.category.name</code>.
*/
public void setName(String value) {
set(3, value);
}
/**
* Getter for <code>nw.category.name</code>.
*/
public String getName() {
return (String) get(3);
}
/**
* Setter for <code>nw.category.description</code>.
*/
public void setDescription(String value) {
set(4, value);
}
/**
* Getter for <code>nw.category.description</code>.
*/
public String getDescription() {
return (String) get(4);
}
/**
* Setter for <code>nw.category.type</code>.
*/
public void setType(String value) {
set(5, value);
}
/**
* Getter for <code>nw.category.type</code>.
*/
public String getType() {
return (String) get(5);
}
/**
* Setter for <code>nw.category.wtime</code>.
*/
public void setWtime(LocalDateTime value) {
set(3, value);
set(6, value);
}
/**
* Getter for <code>nw.category.wtime</code>.
*/
public LocalDateTime getWtime() {
return (LocalDateTime) get(3);
return (LocalDateTime) get(6);
}
/**
* Setter for <code>nw.category.current</code>.
*/
public void setCurrent(Boolean value) {
set(4, value);
set(7, value);
}
/**
* Getter for <code>nw.category.current</code>.
*/
public Boolean getCurrent() {
return (Boolean) get(4);
return (Boolean) get(7);
}
// -------------------------------------------------------------------------
@ -115,23 +157,23 @@ public class CategoryRecord extends UpdatableRecordImpl<CategoryRecord> implemen
}
// -------------------------------------------------------------------------
// Record5 type implementation
// Record8 type implementation
// -------------------------------------------------------------------------
/**
* {@inheritDoc}
*/
@Override
public Row5<Long, Long, Integer, LocalDateTime, Boolean> fieldsRow() {
return (Row5) super.fieldsRow();
public Row8<Long, Long, Integer, String, String, String, LocalDateTime, Boolean> fieldsRow() {
return (Row8) super.fieldsRow();
}
/**
* {@inheritDoc}
*/
@Override
public Row5<Long, Long, Integer, LocalDateTime, Boolean> valuesRow() {
return (Row5) super.valuesRow();
public Row8<Long, Long, Integer, String, String, String, LocalDateTime, Boolean> valuesRow() {
return (Row8) super.valuesRow();
}
/**
@ -162,7 +204,31 @@ public class CategoryRecord extends UpdatableRecordImpl<CategoryRecord> implemen
* {@inheritDoc}
*/
@Override
public Field<LocalDateTime> field4() {
public Field<String> field4() {
return Category.CATEGORY.NAME;
}
/**
* {@inheritDoc}
*/
@Override
public Field<String> field5() {
return Category.CATEGORY.DESCRIPTION;
}
/**
* {@inheritDoc}
*/
@Override
public Field<String> field6() {
return Category.CATEGORY.TYPE;
}
/**
* {@inheritDoc}
*/
@Override
public Field<LocalDateTime> field7() {
return Category.CATEGORY.WTIME;
}
@ -170,7 +236,7 @@ public class CategoryRecord extends UpdatableRecordImpl<CategoryRecord> implemen
* {@inheritDoc}
*/
@Override
public Field<Boolean> field5() {
public Field<Boolean> field8() {
return Category.CATEGORY.CURRENT;
}
@ -202,7 +268,31 @@ public class CategoryRecord extends UpdatableRecordImpl<CategoryRecord> implemen
* {@inheritDoc}
*/
@Override
public LocalDateTime value4() {
public String value4() {
return getName();
}
/**
* {@inheritDoc}
*/
@Override
public String value5() {
return getDescription();
}
/**
* {@inheritDoc}
*/
@Override
public String value6() {
return getType();
}
/**
* {@inheritDoc}
*/
@Override
public LocalDateTime value7() {
return getWtime();
}
@ -210,7 +300,7 @@ public class CategoryRecord extends UpdatableRecordImpl<CategoryRecord> implemen
* {@inheritDoc}
*/
@Override
public Boolean value5() {
public Boolean value8() {
return getCurrent();
}
@ -245,7 +335,34 @@ public class CategoryRecord extends UpdatableRecordImpl<CategoryRecord> implemen
* {@inheritDoc}
*/
@Override
public CategoryRecord value4(LocalDateTime value) {
public CategoryRecord value4(String value) {
setName(value);
return this;
}
/**
* {@inheritDoc}
*/
@Override
public CategoryRecord value5(String value) {
setDescription(value);
return this;
}
/**
* {@inheritDoc}
*/
@Override
public CategoryRecord value6(String value) {
setType(value);
return this;
}
/**
* {@inheritDoc}
*/
@Override
public CategoryRecord value7(LocalDateTime value) {
setWtime(value);
return this;
}
@ -254,7 +371,7 @@ public class CategoryRecord extends UpdatableRecordImpl<CategoryRecord> implemen
* {@inheritDoc}
*/
@Override
public CategoryRecord value5(Boolean value) {
public CategoryRecord value8(Boolean value) {
setCurrent(value);
return this;
}
@ -263,12 +380,15 @@ public class CategoryRecord extends UpdatableRecordImpl<CategoryRecord> implemen
* {@inheritDoc}
*/
@Override
public CategoryRecord values(Long value1, Long value2, Integer value3, LocalDateTime value4, Boolean value5) {
public CategoryRecord values(Long value1, Long value2, Integer value3, String value4, String value5, String value6, LocalDateTime value7, Boolean value8) {
value1(value1);
value2(value2);
value3(value3);
value4(value4);
value5(value5);
value6(value6);
value7(value7);
value8(value8);
return this;
}
@ -286,13 +406,16 @@ public class CategoryRecord extends UpdatableRecordImpl<CategoryRecord> implemen
/**
* Create a detached, initialised CategoryRecord
*/
public CategoryRecord(Long id, Long versionId, Integer categoryId, LocalDateTime wtime, Boolean current) {
public CategoryRecord(Long id, Long versionId, Integer categoryId, String name, String description, String type, LocalDateTime wtime, Boolean current) {
super(Category.CATEGORY);
set(0, id);
set(1, versionId);
set(2, categoryId);
set(3, wtime);
set(4, current);
set(3, name);
set(4, description);
set(5, type);
set(6, wtime);
set(7, current);
}
}

View File

@ -1,6 +1,7 @@
package com.rbkmoney.newway.poller.dominant;
import com.rbkmoney.damsel.domain_config.Commit;
import com.rbkmoney.damsel.domain_config.Operation;
import com.rbkmoney.damsel.domain_config.RepositorySrv;
import com.rbkmoney.newway.service.DominantService;
import org.apache.thrift.TException;
@ -19,26 +20,29 @@ public class DominantPoller {
private final List<DominantHandler> handlers;
private final RepositorySrv.Iface dominantClient;
private final int maxQuerySize = 10;
private final int maxQuerySize;
private long after;
public DominantPoller(List<DominantHandler> handlers, RepositorySrv.Iface dominantClient,
DominantService dominantService) {
DominantService dominantService, @Value("${dmt.polling.maxQuerySize}") int maxQuerySize) {
this.handlers = handlers;
this.dominantClient = dominantClient;
this.after = dominantService.getLastVersionId().orElse(0L);
this.maxQuerySize = maxQuerySize;
}
@Scheduled(fixedDelay = 10000)
@Scheduled(fixedDelayString = "${dmt.polling.delay}")
public void process() throws TException {
Map<Long, Commit> pullRange = dominantClient.pullRange(after, maxQuerySize);
pullRange.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)).forEach(e -> {
e.getValue().getOps().forEach(op -> handlers.forEach(h -> {
List<Operation> operations = e.getValue().getOps();
Long versionId = e.getKey();
operations.forEach(op -> handlers.forEach(h -> {
if (h.accept(op)) {
h.handle(op, e.getKey());
h.handle(op, versionId);
}
}));
after = e.getKey();
after = versionId;
});
}
}

View File

@ -5,6 +5,8 @@ import com.rbkmoney.newway.dao.dominant.iface.CategoryDao;
import com.rbkmoney.newway.domain.tables.pojos.Category;
import com.rbkmoney.newway.poller.dominant.AbstractDominantHandler;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Component
public class CategoryHandler extends AbstractDominantHandler {
@ -21,29 +23,36 @@ public class CategoryHandler extends AbstractDominantHandler {
}
@Override
@Transactional(propagation = Propagation.REQUIRED)
protected void insertDomainObject(DomainObject domainObject, Long versionId) {
saveNewCategory(domainObject, versionId, true);
}
@Override
@Transactional(propagation = Propagation.REQUIRED)
protected void updateDomainObject(DomainObject domainObject, Long versionId) {
categoryDao.updateNotCurrent(domainObject.getCategory().getRef().getId());
saveNewCategory(domainObject, versionId, true);
}
@Override
@Transactional(propagation = Propagation.REQUIRED)
protected void removeDomainObject(DomainObject domainObject, Long versionId) {
categoryDao.updateNotCurrent(domainObject.getCategory().getRef().getId());
saveNewCategory(domainObject, versionId, false);
}
private void saveNewCategory(DomainObject domainObject, Long versionId, boolean current) {
Category category = new Category();
category.setVersionId(versionId);
category.setCategoryId(domainObject.getCategory().getRef().getId());
categoryDao.save(category);
}
@Override
protected void updateDomainObject(DomainObject domainObject, Long versionId) {
int categoryId = domainObject.getCategory().getRef().getId();
Category category = categoryDao.get(categoryId);
category.setVersionId(versionId);
categoryDao.updateNotCurrent(categoryId);
categoryDao.save(category);
}
@Override
protected void removeDomainObject(DomainObject domainObject, Long versionId) {
int categoryId = domainObject.getCategory().getRef().getId();
Category category = categoryDao.get(categoryId);
category.setVersionId(versionId);
category.setCurrent(false);
categoryDao.updateNotCurrent(categoryId);
com.rbkmoney.damsel.domain.Category data = domainObject.getCategory().getData();
category.setName(data.getName());
category.setDescription(data.getDescription());
if (data.isSetType()) {
category.setType(data.getType().name());
}
category.setCurrent(current);
categoryDao.save(category);
}
}

View File

@ -33,5 +33,6 @@ dmt:
url: http://dominant:8022/v1/domain/repository
networkTimeout: 5000
polling:
delay: 10000
maxQuerySize: 10

View File

@ -2,7 +2,11 @@ CREATE TABLE nw.category(
id BIGSERIAL NOT NULL,
version_id BIGINT NOT NULL,
category_id INT NOT NULL,
name CHARACTER VARYING NOT NULL,
description CHARACTER VARYING NOT NULL,
type CHARACTER VARYING,
wtime TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT (now() at time zone 'utc'),
current BOOLEAN NOT NULL DEFAULT TRUE,
CONSTRAINT category_pkey PRIMARY KEY (id)
);
);

View File

@ -0,0 +1,23 @@
package com.rbkmoney.newway.poller.dominant.impl;
import com.rbkmoney.newway.AbstractIntegrationTest;
import com.rbkmoney.newway.dao.dominant.iface.CategoryDao;
import com.rbkmoney.newway.domain.tables.pojos.Category;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static io.github.benas.randombeans.api.EnhancedRandom.random;
public class CategoryHandlerTest extends AbstractIntegrationTest {
@Autowired
private CategoryDao categoryDao;
@Test
public void test() {
Category category = random(Category.class);
category.setCurrent(true);
categoryDao.save(category);
categoryDao.updateNotCurrent(category.getCategoryId());
}
}