From b2ab313d11702c4986d44d6f4ea15acab3193f06 Mon Sep 17 00:00:00 2001 From: jendib Date: Wed, 7 Aug 2013 23:45:47 +0200 Subject: [PATCH] Tag colors (server) --- .../com/sismics/docs/core/dao/jpa/TagDao.java | 6 +++-- .../sismics/docs/core/dao/jpa/dto/TagDto.java | 23 ++++++++++++++++++ .../com/sismics/docs/core/model/jpa/Tag.java | 24 +++++++++++++++++++ .../src/main/resources/config.properties | 2 +- .../resources/db/update/dbupdate-002-0.sql | 3 +++ docs-parent/TODO | 2 +- docs-web/src/dev/resources/config.properties | 2 +- .../docs/rest/resource/DocumentResource.java | 9 ++++--- .../docs/rest/resource/TagResource.java | 21 ++++++++++++---- docs-web/src/prod/resources/config.properties | 2 +- .../docs/rest/TestDocumentResource.java | 3 +++ .../sismics/docs/rest/TestTagResource.java | 6 +++++ 12 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 docs-core/src/main/resources/db/update/dbupdate-002-0.sql diff --git a/docs-core/src/main/java/com/sismics/docs/core/dao/jpa/TagDao.java b/docs-core/src/main/java/com/sismics/docs/core/dao/jpa/TagDao.java index badefb86..b1cc4a62 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/dao/jpa/TagDao.java +++ b/docs-core/src/main/java/com/sismics/docs/core/dao/jpa/TagDao.java @@ -82,7 +82,7 @@ public class TagDao { @SuppressWarnings("unchecked") public List getByDocumentId(String documentId) { EntityManager em = ThreadLocalContext.get().getEntityManager(); - StringBuilder sb = new StringBuilder("select t.TAG_ID_C, t.TAG_NAME_C from T_DOCUMENT_TAG dt "); + StringBuilder sb = new StringBuilder("select t.TAG_ID_C, t.TAG_NAME_C, t.TAG_COLOR_C from T_DOCUMENT_TAG dt "); sb.append(" join T_TAG t on t.TAG_ID_C = dt.DOT_IDTAG_C "); sb.append(" where dt.DOT_IDDOCUMENT_C = :documentId and t.TAG_DELETEDATE_D is null "); sb.append(" order by t.TAG_NAME_C "); @@ -99,6 +99,7 @@ public class TagDao { TagDto tagDto = new TagDto(); tagDto.setId((String) o[i++]); tagDto.setName((String) o[i++]); + tagDto.setColor((String) o[i++]); tagDtoList.add(tagDto); } return tagDtoList; @@ -112,7 +113,7 @@ public class TagDao { @SuppressWarnings("unchecked") public List getStats(String userId) { EntityManager em = ThreadLocalContext.get().getEntityManager(); - StringBuilder sb = new StringBuilder("select t.TAG_ID_C, t.TAG_NAME_C, count(d.DOC_ID_C) "); + StringBuilder sb = new StringBuilder("select t.TAG_ID_C, t.TAG_NAME_C, t.TAG_COLOR_C, count(d.DOC_ID_C) "); sb.append(" from T_TAG t "); sb.append(" left join T_DOCUMENT_TAG dt on t.TAG_ID_C = dt.DOT_IDTAG_C "); sb.append(" left join T_DOCUMENT d on d.DOC_ID_C = dt.DOT_IDDOCUMENT_C and d.DOC_DELETEDATE_D is null and d.DOC_IDUSER_C = :userId "); @@ -132,6 +133,7 @@ public class TagDao { TagStatDto tagDto = new TagStatDto(); tagDto.setId((String) o[i++]); tagDto.setName((String) o[i++]); + tagDto.setColor((String) o[i++]); tagDto.setCount(((Number) o[i++]).intValue()); tagStatDtoList.add(tagDto); } diff --git a/docs-core/src/main/java/com/sismics/docs/core/dao/jpa/dto/TagDto.java b/docs-core/src/main/java/com/sismics/docs/core/dao/jpa/dto/TagDto.java index 56322ad4..343acd90 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/dao/jpa/dto/TagDto.java +++ b/docs-core/src/main/java/com/sismics/docs/core/dao/jpa/dto/TagDto.java @@ -18,6 +18,11 @@ public class TagDto { * Name. */ private String name; + + /** + * Color. + */ + private String color; /** * Getter of id. @@ -54,4 +59,22 @@ public class TagDto { public void setName(String name) { this.name = name; } + + /** + * Getter of color. + * + * @return the color + */ + public String getColor() { + return color; + } + + /** + * Setter of color. + * + * @param color color + */ + public void setColor(String color) { + this.color = color; + } } diff --git a/docs-core/src/main/java/com/sismics/docs/core/model/jpa/Tag.java b/docs-core/src/main/java/com/sismics/docs/core/model/jpa/Tag.java index e5da92c4..7371b036 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/model/jpa/Tag.java +++ b/docs-core/src/main/java/com/sismics/docs/core/model/jpa/Tag.java @@ -47,6 +47,12 @@ public class Tag { */ @Column(name = "TAG_DELETEDATE_D") private Date deleteDate; + + /** + * Tag name. + */ + @Column(name = "TAG_COLOR_C", nullable = false, length = 6) + private String color; /** * Getter of id. @@ -119,6 +125,24 @@ public class Tag { public void setCreateDate(Date createDate) { this.createDate = createDate; } + + /** + * Getter of color. + * + * @return the color + */ + public String getColor() { + return color; + } + + /** + * Setter of color. + * + * @param color color + */ + public void setColor(String color) { + this.color = color; + } /** * Getter of deleteDate. diff --git a/docs-core/src/main/resources/config.properties b/docs-core/src/main/resources/config.properties index 9484a82d..d8b90e8b 100644 --- a/docs-core/src/main/resources/config.properties +++ b/docs-core/src/main/resources/config.properties @@ -1 +1 @@ -db.version=1 \ No newline at end of file +db.version=2 \ No newline at end of file diff --git a/docs-core/src/main/resources/db/update/dbupdate-002-0.sql b/docs-core/src/main/resources/db/update/dbupdate-002-0.sql new file mode 100644 index 00000000..79ff42c0 --- /dev/null +++ b/docs-core/src/main/resources/db/update/dbupdate-002-0.sql @@ -0,0 +1,3 @@ +alter table T_TAG add column TAG_COLOR_C varchar(6) not null; +update T_TAG set TAG_COLOR_C = '3a87ad'; +update T_CONFIG set CFG_VALUE_C='2' where CFG_ID_C='DB_VERSION'; diff --git a/docs-parent/TODO b/docs-parent/TODO index c4956b70..38913018 100644 --- a/docs-parent/TODO +++ b/docs-parent/TODO @@ -1,2 +1,2 @@ - Users administration (client) -- Tag color (client/server) +- Tag color (client) diff --git a/docs-web/src/dev/resources/config.properties b/docs-web/src/dev/resources/config.properties index f2362d2f..87577f48 100644 --- a/docs-web/src/dev/resources/config.properties +++ b/docs-web/src/dev/resources/config.properties @@ -1,3 +1,3 @@ api.current_version=${project.version} api.min_version=1.0 -db.version=1 \ No newline at end of file +db.version=2 \ No newline at end of file diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/DocumentResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/DocumentResource.java index 221f02cb..5c72254c 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/DocumentResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/DocumentResource.java @@ -20,6 +20,7 @@ import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import org.apache.commons.lang.StringUtils; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; @@ -83,6 +84,7 @@ public class DocumentResource extends BaseResource { JSONObject tag = new JSONObject(); tag.put("id", tagDto.getId()); tag.put("name", tagDto.getName()); + tag.put("color", tagDto.getColor()); tags.add(tag); } document.put("tags", tags); @@ -149,6 +151,7 @@ public class DocumentResource extends BaseResource { JSONObject tag = new JSONObject(); tag.put("id", tagDto.getId()); tag.put("name", tagDto.getName()); + tag.put("color", tagDto.getColor()); tags.add(tag); } document.put("tags", tags); @@ -228,7 +231,7 @@ public class DocumentResource extends BaseResource { } // Validate input data - title = ValidationUtil.validateLength(title, "title", 1, 100, false); + title = ValidationUtil.validateLength(title, "title", 1, 100, true); description = ValidationUtil.validateLength(description, "description", 0, 4000, true); Date createDate = ValidationUtil.validateDate(createDateStr, "create_date", true); @@ -242,10 +245,10 @@ public class DocumentResource extends BaseResource { } // Update the document - if (title != null) { + if (!StringUtils.isEmpty(title)) { document.setTitle(title); } - if (description != null) { + if (!StringUtils.isEmpty(description)) { document.setDescription(description); } if (createDate != null) { diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/TagResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/TagResource.java index 9190d0a8..67593266 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/TagResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/TagResource.java @@ -15,6 +15,7 @@ import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import org.apache.commons.lang.StringUtils; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; @@ -54,6 +55,7 @@ public class TagResource extends BaseResource { JSONObject item = new JSONObject(); item.put("id", tag.getId()); item.put("name", tag.getName()); + item.put("color", tag.getColor()); items.add(item); } response.put("tags", items); @@ -82,6 +84,7 @@ public class TagResource extends BaseResource { JSONObject item = new JSONObject(); item.put("id", tagStatDto.getId()); item.put("name", tagStatDto.getName()); + item.put("color", tagStatDto.getColor()); item.put("count", tagStatDto.getCount()); items.add(item); } @@ -99,13 +102,15 @@ public class TagResource extends BaseResource { @PUT @Produces(MediaType.APPLICATION_JSON) public Response add( - @FormParam("name") String name) throws JSONException { + @FormParam("name") String name, + @FormParam("color") String color) throws JSONException { if (!authenticate()) { throw new ForbiddenClientException(); } // Validate input data name = ValidationUtil.validateLength(name, "name", 1, 36, false); + color = ValidationUtil.validateLength(color, "color", 6, 6, false); // Get the tag TagDao tagDao = new TagDao(); @@ -117,6 +122,7 @@ public class TagResource extends BaseResource { // Create the tag tag = new Tag(); tag.setName(name); + tag.setColor(color); tag.setUserId(principal.getId()); String tagId = tagDao.create(tag); @@ -137,13 +143,15 @@ public class TagResource extends BaseResource { @Produces(MediaType.APPLICATION_JSON) public Response update( @PathParam("id") String id, - @FormParam("name") String name) throws JSONException { + @FormParam("name") String name, + @FormParam("color") String color) throws JSONException { if (!authenticate()) { throw new ForbiddenClientException(); } // Validate input data - name = ValidationUtil.validateLength(name, "name", 1, 36, false); + name = ValidationUtil.validateLength(name, "name", 1, 36, true); + color = ValidationUtil.validateLength(color, "color", 6, 6, true); // Get the tag TagDao tagDao = new TagDao(); @@ -153,7 +161,12 @@ public class TagResource extends BaseResource { } // Update the tag - tag.setName(name); + if (!StringUtils.isEmpty(name)) { + tag.setName(name); + } + if (!StringUtils.isEmpty(color)) { + tag.setColor(color); + } JSONObject response = new JSONObject(); response.put("id", id); diff --git a/docs-web/src/prod/resources/config.properties b/docs-web/src/prod/resources/config.properties index f2362d2f..87577f48 100644 --- a/docs-web/src/prod/resources/config.properties +++ b/docs-web/src/prod/resources/config.properties @@ -1,3 +1,3 @@ api.current_version=${project.version} api.min_version=1.0 -db.version=1 \ No newline at end of file +db.version=2 \ No newline at end of file diff --git a/docs-web/src/test/java/com/sismics/docs/rest/TestDocumentResource.java b/docs-web/src/test/java/com/sismics/docs/rest/TestDocumentResource.java index 98fbd533..f4f81a63 100644 --- a/docs-web/src/test/java/com/sismics/docs/rest/TestDocumentResource.java +++ b/docs-web/src/test/java/com/sismics/docs/rest/TestDocumentResource.java @@ -38,6 +38,7 @@ public class TestDocumentResource extends BaseJerseyTest { tagResource.addFilter(new CookieAuthenticationFilter(document1Token)); MultivaluedMapImpl postParams = new MultivaluedMapImpl(); postParams.add("name", "Super tag"); + postParams.add("color", "ffff00"); ClientResponse response = tagResource.put(ClientResponse.class, postParams); Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus())); JSONObject json = response.getEntity(JSONObject.class); @@ -75,6 +76,7 @@ public class TestDocumentResource extends BaseJerseyTest { Assert.assertEquals(1, tags.length()); Assert.assertEquals(tag1Id, tags.getJSONObject(0).getString("id")); Assert.assertEquals("Super tag", tags.getJSONObject(0).getString("name")); + Assert.assertEquals("ffff00", tags.getJSONObject(0).getString("color")); // Search documents by query documentResource = resource().path("/document/list"); @@ -141,6 +143,7 @@ public class TestDocumentResource extends BaseJerseyTest { tagResource.addFilter(new CookieAuthenticationFilter(document1Token)); postParams = new MultivaluedMapImpl(); postParams.add("name", "Super tag 2"); + postParams.add("color", "00ffff"); response = tagResource.put(ClientResponse.class, postParams); Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus())); json = response.getEntity(JSONObject.class); diff --git a/docs-web/src/test/java/com/sismics/docs/rest/TestTagResource.java b/docs-web/src/test/java/com/sismics/docs/rest/TestTagResource.java index dab48e80..ca041176 100644 --- a/docs-web/src/test/java/com/sismics/docs/rest/TestTagResource.java +++ b/docs-web/src/test/java/com/sismics/docs/rest/TestTagResource.java @@ -35,6 +35,7 @@ public class TestTagResource extends BaseJerseyTest { tagResource.addFilter(new CookieAuthenticationFilter(tag1Token)); MultivaluedMapImpl postParams = new MultivaluedMapImpl(); postParams.add("name", "Tag 3"); + postParams.add("color", "ff0000"); ClientResponse response = tagResource.put(ClientResponse.class, postParams); Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus())); JSONObject json = response.getEntity(JSONObject.class); @@ -46,6 +47,7 @@ public class TestTagResource extends BaseJerseyTest { tagResource.addFilter(new CookieAuthenticationFilter(tag1Token)); postParams = new MultivaluedMapImpl(); postParams.add("name", "Tag 4"); + postParams.add("color", "00ff00"); response = tagResource.put(ClientResponse.class, postParams); Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus())); json = response.getEntity(JSONObject.class); @@ -91,12 +93,15 @@ public class TestTagResource extends BaseJerseyTest { json = response.getEntity(JSONObject.class); JSONArray tags = json.getJSONArray("tags"); Assert.assertTrue(tags.length() > 0); + Assert.assertEquals("Tag 4", tags.getJSONObject(1).getString("name")); + Assert.assertEquals("00ff00", tags.getJSONObject(1).getString("color")); // Update a tag tagResource = resource().path("/tag/" + tag4Id); tagResource.addFilter(new CookieAuthenticationFilter(tag1Token)); postParams = new MultivaluedMapImpl(); postParams.add("name", "Updated name"); + postParams.add("color", "0000ff"); response = tagResource.post(ClientResponse.class, postParams); Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus())); json = response.getEntity(JSONObject.class); @@ -111,6 +116,7 @@ public class TestTagResource extends BaseJerseyTest { tags = json.getJSONArray("tags"); Assert.assertTrue(tags.length() > 0); Assert.assertEquals("Updated name", tags.getJSONObject(1).getString("name")); + Assert.assertEquals("0000ff", tags.getJSONObject(1).getString("color")); // Deletes a tag tagResource = resource().path("/tag/" + tag4Id);