Commit 384ed70e authored by Valentin Suhnjov's avatar Valentin Suhnjov

Merge branch 'releases/release-0.4.0' into master

parents 544cd9f5 94f41bd6
......@@ -5,7 +5,7 @@
<groupId>ee.eesti.riha</groupId>
<artifactId>rest</artifactId>
<version>0.3.0</version>
<version>0.4.0</version>
<packaging>war</packaging>
......
......@@ -150,6 +150,9 @@ COMMENT ON COLUMN riha.main_resource.kind_id IS 'Ressursi liik (infosystem, clas
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE riha.main_resource TO riha;
CREATE INDEX main_resource_uuid
ON riha.main_resource (((json_content ->> 'uuid')));
-- Index: riha.ixfk_main_resource_main_resource
-- DROP INDEX riha.ixfk_main_resource_main_resource;
......
CREATE VIEW riha.main_resource_view AS
SELECT DISTINCT ON (json_content ->> 'uuid') *
FROM riha.main_resource
ORDER BY json_content ->> 'uuid',
creation_date DESC,
main_resource_id DESC;
\ No newline at end of file
-- v_main_resource provides a view on latest info system versions from main_resource
-- provided rows are with unique uuid, latest creation_date and, in case of ties, greatest entity id.
CREATE INDEX main_resource_uuid
ON riha.main_resource (((json_content ->> 'uuid')));
CREATE VIEW riha.main_resource_view AS
SELECT DISTINCT ON (json_content ->> 'uuid') *
FROM riha.main_resource
ORDER BY json_content ->> 'uuid',
creation_date DESC,
main_resource_id DESC;
\ No newline at end of file
-- some of comment records may not have info system uuid defined
ALTER TABLE riha.comment ALTER COLUMN infosystem_uuid DROP DEFAULT;
ALTER TABLE riha.comment ALTER COLUMN infosystem_uuid DROP NOT NULL;
\ No newline at end of file
ALTER TABLE riha.comment ADD sub_type VARCHAR(150) NULL;
\ No newline at end of file
......@@ -304,8 +304,11 @@ public class ApiGenericDAOImpl<T, K> implements ApiGenericDAO<T, K> {
qry.append(joinedAsOneStr);
if (jsonFieldExists(session, tableName, orderData.getOrderByField())) {
String jsonField = Finals.JSON_CONTENT + "->>" + "'" + orderData.getOrderByField() + "'";
qry.append(" ORDER BY " + "item." + jsonField + (orderData.isAsc() ? " ASC " : " DESC "));
String orderByParameterName = "jOrderParameter";
qry.append(" ").append(createJsonQueryClause(orderByParameterName, orderData));
String jsonOrderByFieldName = "{" + orderData.getOrderByField().replaceAll("\\.", ",") + "}";
params.put(orderByParameterName, jsonOrderByFieldName);
}
if (isCount) {
......@@ -324,6 +327,14 @@ public class ApiGenericDAOImpl<T, K> implements ApiGenericDAO<T, K> {
return query;
}
private String createJsonQueryClause(String orderByParameterName, OrderByData orderData) {
return "ORDER BY" +
" item." + Finals.JSON_CONTENT +
" #>>" +
" :" + orderByParameterName + "\\:\\:text[]" +
(orderData.isAsc() ? " ASC" : " DESC");
}
/**
* No filter order helper.
*
......@@ -340,14 +351,20 @@ public class ApiGenericDAOImpl<T, K> implements ApiGenericDAO<T, K> {
boolean orderByIsJsonContentField = (!DaoHelper.isFieldPartOfModel(orderData.getOrderByField(), clazz));
if (orderByIsJsonContentField) {
StringBuffer qry = new StringBuffer();
// qry.append("SELECT * FROM " + clazz.getSimpleName() + " item ");
qry.append("SELECT * FROM " + getTableName(clazz) + " item ");
String jsonField = Finals.JSON_CONTENT + "->>" + "'" + orderData.getOrderByField() + "'";
qry.append(" ORDER BY " + "item." + jsonField + (orderData.isAsc() ? " ASC " : " DESC "));
Query queryExisting = session.createSQLQuery(qry.toString()).addEntity(clazz);
queryExisting.setFirstResult(offset);
objectList = queryExisting.setMaxResults(limit).list();
StringBuilder qry = new StringBuilder();
qry.append("SELECT *")
.append(" FROM ").append(getTableName(clazz)).append(" item ");
String orderByParameterName = "jOrderParameter";
qry.append(" ").append(createJsonQueryClause(orderByParameterName, orderData));
Query query = session.createSQLQuery(qry.toString()).addEntity(clazz);
String jsonOrderByFieldName = "{" + orderData.getOrderByField().replaceAll("\\.", ",") + "}";
query.setParameter(orderByParameterName, jsonOrderByFieldName);
query.setFirstResult(offset);
objectList = query.setMaxResults(limit).list();
} else {
if (orderData.isAsc()) {
criteria.addOrder(Order.asc(orderData.getOrderByField()));
......@@ -493,6 +510,18 @@ public class ApiGenericDAOImpl<T, K> implements ApiGenericDAO<T, K> {
if (existing == null) {
return 0;
}
if (JsonContentBasedTable.isJsonContentBasedTable(clazz)) {
return updateJsonContentEntity(existing, newValue);
} else {
return updateEntity(newValue);
}
}
private int updateJsonContentEntity(T existing, T newValue) throws RihaRestException {
Class<T> clazz = (Class<T>) newValue.getClass();
Session session = sessionFactory.getCurrentSession();
try {
BaseModel baseModel = (BaseModel) existing;
BaseModel updateInfo = (BaseModel) newValue;
......@@ -523,7 +552,12 @@ public class ApiGenericDAOImpl<T, K> implements ApiGenericDAO<T, K> {
}
// success then 1 updated
return 1;
}
private int updateEntity(T updatedEntity) {
Session session = sessionFactory.getCurrentSession();
session.merge(updatedEntity);
return 1;
}
/**
......@@ -638,6 +672,18 @@ public class ApiGenericDAOImpl<T, K> implements ApiGenericDAO<T, K> {
*
* @see ee.eesti.riha.rest.dao.ApiGenericDAO#update(java.util.List, java.lang.String)
*/
/**
* Update of non json entities is not supported. Use {@link #update(Object, Integer)} method instead.
* @param objects
* @param idFieldName name of the field by which the update will be done, must not be null in object list elements
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws RihaRestException
*/
@Override
public int update(List<T> objects, String idFieldName) throws NoSuchFieldException, SecurityException,
IllegalArgumentException, IllegalAccessException, RihaRestException {
......
......@@ -12,6 +12,7 @@ import ee.eesti.riha.rest.model.Document;
import ee.eesti.riha.rest.model.Main_resource;
import ee.eesti.riha.rest.model.readonly.Asutus;
import ee.eesti.riha.rest.model.readonly.Isik;
import ee.eesti.riha.rest.model.readonly.Main_resource_view;
import ee.eesti.riha.rest.util.PropsReader;
// TODO: Auto-generated Javadoc
......@@ -38,8 +39,10 @@ public final class Finals {
// read only
addClassRepresentingTable(Isik.class);
addClassRepresentingTable(Asutus.class);
addClassRepresentingTable(Main_resource_view.class);
TABLE_CLASS_MAP_READONLY.put(Isik.class.getSimpleName().toLowerCase(), Isik.class);
TABLE_CLASS_MAP_READONLY.put(Asutus.class.getSimpleName().toLowerCase(), Asutus.class);
TABLE_CLASS_MAP_READONLY.put(Main_resource_view.class.getSimpleName().toLowerCase(), Main_resource_view.class);
}
/**
......
......@@ -7,6 +7,7 @@ import ee.eesti.riha.rest.model.Comment;
import ee.eesti.riha.rest.model.Data_object;
import ee.eesti.riha.rest.model.Document;
import ee.eesti.riha.rest.model.Main_resource;
import ee.eesti.riha.rest.model.readonly.Main_resource_view;
/**
* Json_content based table is table that contains field json_content. All the other fields that exist in table must
......@@ -26,7 +27,7 @@ public final class JsonContentBasedTable {
jsonContentBasedTables.add(Main_resource.class);
jsonContentBasedTables.add(Document.class);
jsonContentBasedTables.add(Data_object.class);
// jsonContentBasedTables.add(Comment.class);
jsonContentBasedTables.add(Main_resource_view.class);
}
/**
......
......@@ -69,6 +69,10 @@ public class Comment implements BaseModel {
@Column(name = "type")
private String type;
@Column(name = "sub_type")
private String sub_type;
/*
* (non-Javadoc)
*
......@@ -340,4 +344,12 @@ public class Comment implements BaseModel {
public void setType(String type) {
this.type = type;
}
public String getSub_type() {
return sub_type;
}
public void setSub_type(String sub_type) {
this.sub_type = sub_type;
}
}
package ee.eesti.riha.rest.model.readonly;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonRawValue;
import com.google.gson.JsonObject;
import ee.eesti.riha.rest.logic.Finals;
import ee.eesti.riha.rest.model.BaseModel;
import ee.eesti.riha.rest.model.hibernate.JsonObjectUserType;
import ee.eesti.riha.rest.model.util.FieldIsPK;
import org.hibernate.annotations.Immutable;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.*;
import java.util.Date;
@TypeDefs({@TypeDef(name = "JsonObject", typeClass = JsonObjectUserType.class)})
@Transactional
@Entity
@Table(name = "main_resource_view")
@JsonInclude(JsonInclude.Include.NON_NULL)
@Immutable
public class Main_resource_view implements BaseModel {
@FieldIsPK
@Id
@Column(updatable = false)
private Integer main_resource_id;
private String uri;
@JsonRawValue
@Type(type = "JsonObject")
private JsonObject json_content;
private String creator;
private String modifier;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = Finals.DATE_FORMAT)
@Temporal(TemporalType.TIMESTAMP)
private Date creation_date;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = Finals.DATE_FORMAT)
@Temporal(TemporalType.TIMESTAMP)
private Date modified_date;
private String kind;
public Integer getMain_resource_id() {
return main_resource_id;
}
@Override
public int callGetId() {
return getMain_resource_id();
}
@Override
public void callSetId(int id) {
throw new UnsupportedOperationException();
}
@Override
public String getUri() {
return uri;
}
@Override
public void setUri(String uri) {
throw new UnsupportedOperationException();
}
@Override
public JsonObject getJson_content() {
return json_content;
}
@Override
public void setJson_content(JsonObject json_content) {
throw new UnsupportedOperationException();
}
@Override
public String getCreator() {
return creator;
}
@Override
public void setCreator(String creator) {
throw new UnsupportedOperationException();
}
@Override
public String getModifier() {
return modifier;
}
@Override
public void setModifier(String modifier) {
throw new UnsupportedOperationException();
}
@Override
public Date getCreation_date() {
return creation_date;
}
@Override
public void setCreation_date(Date creation_date) {
throw new UnsupportedOperationException();
}
@Override
public Date getModified_date() {
return modified_date;
}
@Override
public void setModified_date(Date modified_date) {
throw new UnsupportedOperationException();
}
@Override
public String getKind() {
return kind;
}
@Override
public void setKind(String kind) {
throw new UnsupportedOperationException();
}
}
......@@ -100,8 +100,4 @@ public interface ApiClassicService {
@DELETE
Response delete(@PathParam(value = "tableName") String tableName, @PathParam(value = "id") Integer id);
// getters and setters won't work without being a service themselves
// this is needed to use fake AuthService in integration tests
// TODO find if better solution exists, maybe ignore those paths?
}
......@@ -36,6 +36,7 @@
<value>ee.eesti.riha.rest.model.readonly.Kind</value>
<value>ee.eesti.riha.rest.model.readonly.Isik</value>
<value>ee.eesti.riha.rest.model.readonly.Asutus</value>
<value>ee.eesti.riha.rest.model.readonly.Main_resource_view</value>
</list>
</property>
<property name="hibernateProperties">
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment