mirror of
https://github.com/sismics/docs.git
synced 2025-12-13 01:36:18 +00:00
Server side tag system
This commit is contained in:
@@ -2,7 +2,9 @@ package com.sismics.docs.rest.resource;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.ws.rs.DELETE;
|
||||
@@ -22,9 +24,12 @@ import org.codehaus.jettison.json.JSONObject;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.sismics.docs.core.dao.jpa.DocumentDao;
|
||||
import com.sismics.docs.core.dao.jpa.TagDao;
|
||||
import com.sismics.docs.core.dao.jpa.criteria.DocumentCriteria;
|
||||
import com.sismics.docs.core.dao.jpa.dto.DocumentDto;
|
||||
import com.sismics.docs.core.dao.jpa.dto.TagDto;
|
||||
import com.sismics.docs.core.model.jpa.Document;
|
||||
import com.sismics.docs.core.model.jpa.Tag;
|
||||
import com.sismics.docs.core.util.jpa.PaginatedList;
|
||||
import com.sismics.docs.core.util.jpa.PaginatedLists;
|
||||
import com.sismics.docs.core.util.jpa.SortCriteria;
|
||||
@@ -69,6 +74,18 @@ public class DocumentResource extends BaseResource {
|
||||
document.put("description", documentDb.getDescription());
|
||||
document.put("create_date", documentDb.getCreateDate().getTime());
|
||||
|
||||
// Get tags
|
||||
TagDao tagDao = new TagDao();
|
||||
List<TagDto> tagDtoList = tagDao.getByDocumentId(id);
|
||||
List<JSONObject> tags = new ArrayList<JSONObject>();
|
||||
for (TagDto tagDto : tagDtoList) {
|
||||
JSONObject tag = new JSONObject();
|
||||
tag.put("id", tagDto.getId());
|
||||
tag.put("name", tagDto.getName());
|
||||
tags.add(tag);
|
||||
}
|
||||
document.put("tags", tags);
|
||||
|
||||
return Response.ok().entity(document).build();
|
||||
}
|
||||
|
||||
@@ -132,7 +149,8 @@ public class DocumentResource extends BaseResource {
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response add(
|
||||
@FormParam("title") String title,
|
||||
@FormParam("description") String description) throws JSONException {
|
||||
@FormParam("description") String description,
|
||||
@FormParam("tags[]") List<String> tagList) throws JSONException {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
@@ -149,6 +167,9 @@ public class DocumentResource extends BaseResource {
|
||||
document.setDescription(description);
|
||||
String documentId = documentDao.create(document);
|
||||
|
||||
// Update tags
|
||||
updateTagList(documentId, tagList);
|
||||
|
||||
JSONObject response = new JSONObject();
|
||||
response.put("id", documentId);
|
||||
return Response.ok().entity(response).build();
|
||||
@@ -168,7 +189,8 @@ public class DocumentResource extends BaseResource {
|
||||
public Response update(
|
||||
@PathParam("id") String id,
|
||||
@FormParam("title") String title,
|
||||
@FormParam("description") String description) throws JSONException {
|
||||
@FormParam("description") String description,
|
||||
@FormParam("tags[]") List<String> tagList) throws JSONException {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
@@ -194,11 +216,40 @@ public class DocumentResource extends BaseResource {
|
||||
document.setDescription(description);
|
||||
}
|
||||
|
||||
// Update tags
|
||||
updateTagList(id, tagList);
|
||||
|
||||
// Always return ok
|
||||
JSONObject response = new JSONObject();
|
||||
response.put("id", id);
|
||||
return Response.ok().entity(response).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update tags list on a document.
|
||||
*
|
||||
* @param documentId
|
||||
* @param tagList
|
||||
* @throws JSONException
|
||||
*/
|
||||
private void updateTagList(String documentId, List<String> tagList) throws JSONException {
|
||||
if (tagList != null) {
|
||||
TagDao tagDao = new TagDao();
|
||||
Set<String> tagSet = new HashSet<String>();
|
||||
Set<String> tagIdSet = new HashSet<String>();
|
||||
List<Tag> tagDbList = tagDao.getByUserId(principal.getId());
|
||||
for (Tag tagDb : tagDbList) {
|
||||
tagIdSet.add(tagDb.getId());
|
||||
}
|
||||
for (String tagId : tagList) {
|
||||
if (!tagIdSet.contains(tagId)) {
|
||||
throw new ClientException("TagNotFound", MessageFormat.format("Tag not found: {0}", tagId));
|
||||
}
|
||||
tagSet.add(tagId);
|
||||
}
|
||||
tagDao.updateTagList(documentId, tagSet);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a document.
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
package com.sismics.docs.rest.resource;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.FormParam;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
import org.codehaus.jettison.json.JSONException;
|
||||
import org.codehaus.jettison.json.JSONObject;
|
||||
|
||||
import com.sismics.docs.core.dao.jpa.TagDao;
|
||||
import com.sismics.docs.core.model.jpa.Tag;
|
||||
import com.sismics.rest.exception.ClientException;
|
||||
import com.sismics.rest.exception.ForbiddenClientException;
|
||||
import com.sismics.rest.util.ValidationUtil;
|
||||
|
||||
/**
|
||||
* Tag REST resources.
|
||||
*
|
||||
* @author bgamard
|
||||
*/
|
||||
@Path("/tag")
|
||||
public class TagResource extends BaseResource {
|
||||
/**
|
||||
* Returns the list of all tags.
|
||||
*
|
||||
* @return Response
|
||||
* @throws JSONException
|
||||
*/
|
||||
@GET
|
||||
@Path("/list")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response list() throws JSONException {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
|
||||
TagDao tagDao = new TagDao();
|
||||
List<Tag> tagList = tagDao.getByUserId(principal.getId());
|
||||
JSONObject response = new JSONObject();
|
||||
List<JSONObject> items = new ArrayList<JSONObject>();
|
||||
for (Tag tag : tagList) {
|
||||
JSONObject item = new JSONObject();
|
||||
item.put("id", tag.getId());
|
||||
items.add(item);
|
||||
}
|
||||
response.put("tags", items);
|
||||
return Response.ok().entity(response).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new tag.
|
||||
*
|
||||
* @param name Name
|
||||
* @return Response
|
||||
* @throws JSONException
|
||||
*/
|
||||
@PUT
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response add(
|
||||
@FormParam("name") String name) throws JSONException {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
|
||||
// Validate input data
|
||||
name = ValidationUtil.validateLength(name, "title", 1, 36, false);
|
||||
|
||||
// Get the tag
|
||||
TagDao tagDao = new TagDao();
|
||||
Tag tag = tagDao.getByUserIdAndName(principal.getId(), name);
|
||||
if (tag != null) {
|
||||
throw new ClientException("AlreadyExistingTag", MessageFormat.format("Tag already exists: {0}", name));
|
||||
}
|
||||
|
||||
// Create the tag
|
||||
tag = new Tag();
|
||||
tag.setName(name);
|
||||
tag.setUserId(principal.getId());
|
||||
String tagId = tagDao.create(tag);
|
||||
|
||||
JSONObject response = new JSONObject();
|
||||
response.put("id", tagId);
|
||||
return Response.ok().entity(response).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a tag.
|
||||
*
|
||||
* @param name Name
|
||||
* @return Response
|
||||
* @throws JSONException
|
||||
*/
|
||||
@DELETE
|
||||
@Path("{id: [a-z0-9\\-]+}")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response delete(
|
||||
@PathParam("id") String tagId) throws JSONException {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
|
||||
// Get the tag
|
||||
TagDao tagDao = new TagDao();
|
||||
Tag tag = tagDao.getByUserIdAndTagId(principal.getId(), tagId);
|
||||
if (tag == null) {
|
||||
throw new ClientException("TagNotFound", MessageFormat.format("Tag not found: {0}", tagId));
|
||||
}
|
||||
|
||||
// Delete the tag
|
||||
tagDao.delete(tagId);
|
||||
|
||||
JSONObject response = new JSONObject();
|
||||
response.put("status", "ok");
|
||||
return Response.ok().entity(response).build();
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,26 @@ App.controller('DocumentView', function($rootScope, $scope, $state, $stateParams
|
||||
$state.transitionTo('document.view.file', { id: $stateParams.id, fileId: file.id })
|
||||
};
|
||||
|
||||
/**
|
||||
* Delete a document.
|
||||
*/
|
||||
$scope.deleteDocument = function(document) {
|
||||
var title = 'Delete document';
|
||||
var msg = 'Do you really want to delete this document?';
|
||||
var btns = [{result:'cancel', label: 'Cancel'}, {result:'ok', label: 'OK', cssClass: 'btn-primary'}];
|
||||
|
||||
$dialog.messageBox(title, msg, btns)
|
||||
.open()
|
||||
.then(function(result) {
|
||||
if (result == 'ok') {
|
||||
Restangular.one('document', document.id).remove().then(function() {
|
||||
$scope.loadDocuments();
|
||||
$state.transitionTo('document.default');
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Delete a file.
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<div class="text-right">
|
||||
<div class="btn-group">
|
||||
<button class="btn btn-danger" ng-click="deleteDocument(document)"><span class="icon-trash icon-white"></span> Delete</button>
|
||||
<button class="btn btn-primary" ng-click="editDocument(document.id)"><span class="icon-pencil icon-white"></span> Edit</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -30,15 +30,27 @@ public class TestDocumentResource extends BaseJerseyTest {
|
||||
clientUtil.createUser("document1");
|
||||
String document1Token = clientUtil.login("document1");
|
||||
|
||||
// Create a tag
|
||||
WebResource tagResource = resource().path("/tag");
|
||||
tagResource.addFilter(new CookieAuthenticationFilter(document1Token));
|
||||
MultivaluedMapImpl postParams = new MultivaluedMapImpl();
|
||||
postParams.add("name", "Super tag");
|
||||
ClientResponse response = tagResource.put(ClientResponse.class, postParams);
|
||||
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
||||
JSONObject json = response.getEntity(JSONObject.class);
|
||||
String tag1Id = json.optString("id");
|
||||
Assert.assertNotNull(tag1Id);
|
||||
|
||||
// Create a document
|
||||
WebResource documentResource = resource().path("/document");
|
||||
documentResource.addFilter(new CookieAuthenticationFilter(document1Token));
|
||||
MultivaluedMapImpl postParams = new MultivaluedMapImpl();
|
||||
postParams = new MultivaluedMapImpl();
|
||||
postParams.add("title", "My super document 1");
|
||||
postParams.add("description", "My super description for document 1");
|
||||
ClientResponse response = documentResource.put(ClientResponse.class, postParams);
|
||||
postParams.add("tags[]", tag1Id);
|
||||
response = documentResource.put(ClientResponse.class, postParams);
|
||||
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
||||
JSONObject json = response.getEntity(JSONObject.class);
|
||||
json = response.getEntity(JSONObject.class);
|
||||
String document1Id = json.optString("id");
|
||||
Assert.assertNotNull(document1Id);
|
||||
|
||||
@@ -85,6 +97,20 @@ public class TestDocumentResource extends BaseJerseyTest {
|
||||
json = response.getEntity(JSONObject.class);
|
||||
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
||||
Assert.assertEquals(document1Id, json.getString("id"));
|
||||
JSONArray tags = json.getJSONArray("tags");
|
||||
Assert.assertEquals(1, tags.length());
|
||||
Assert.assertEquals(tag1Id, tags.getJSONObject(0).getString("id"));
|
||||
|
||||
// Create a tag
|
||||
tagResource = resource().path("/tag");
|
||||
tagResource.addFilter(new CookieAuthenticationFilter(document1Token));
|
||||
postParams = new MultivaluedMapImpl();
|
||||
postParams.add("name", "Super tag 2");
|
||||
response = tagResource.put(ClientResponse.class, postParams);
|
||||
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
||||
json = response.getEntity(JSONObject.class);
|
||||
String tag2Id = json.optString("id");
|
||||
Assert.assertNotNull(tag1Id);
|
||||
|
||||
// Update a document
|
||||
documentResource = resource().path("/document/" + document1Id);
|
||||
@@ -92,6 +118,7 @@ public class TestDocumentResource extends BaseJerseyTest {
|
||||
postParams = new MultivaluedMapImpl();
|
||||
postParams.add("title", "My new super document 1");
|
||||
postParams.add("description", "My new super description for document 1");
|
||||
postParams.add("tags[]", tag2Id);
|
||||
response = documentResource.post(ClientResponse.class, postParams);
|
||||
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
||||
json = response.getEntity(JSONObject.class);
|
||||
@@ -105,6 +132,9 @@ public class TestDocumentResource extends BaseJerseyTest {
|
||||
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
||||
Assert.assertTrue(json.getString("title").contains("new"));
|
||||
Assert.assertTrue(json.getString("description").contains("new"));
|
||||
tags = json.getJSONArray("tags");
|
||||
Assert.assertEquals(1, tags.length());
|
||||
Assert.assertEquals(tag2Id, tags.getJSONObject(0).getString("id"));
|
||||
|
||||
// Deletes a document
|
||||
documentResource = resource().path("/document/" + document1Id);
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.sismics.docs.rest;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.codehaus.jettison.json.JSONArray;
|
||||
import org.codehaus.jettison.json.JSONException;
|
||||
import org.codehaus.jettison.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.sismics.docs.rest.BaseJerseyTest;
|
||||
import com.sismics.docs.rest.filter.CookieAuthenticationFilter;
|
||||
import com.sun.jersey.api.client.ClientResponse;
|
||||
import com.sun.jersey.api.client.ClientResponse.Status;
|
||||
import com.sun.jersey.api.client.WebResource;
|
||||
import com.sun.jersey.core.util.MultivaluedMapImpl;
|
||||
|
||||
/**
|
||||
* Test the tag resource.
|
||||
*
|
||||
* @author bgamard
|
||||
*/
|
||||
public class TestTagResource extends BaseJerseyTest {
|
||||
/**
|
||||
* Test the tag resource.
|
||||
*
|
||||
* @throws JSONException
|
||||
*/
|
||||
@Test
|
||||
public void testTagResource() throws JSONException {
|
||||
// Login tag1
|
||||
clientUtil.createUser("tag1");
|
||||
String tag1Token = clientUtil.login("tag1");
|
||||
|
||||
// Create a tag
|
||||
WebResource tagResource = resource().path("/tag");
|
||||
tagResource.addFilter(new CookieAuthenticationFilter(tag1Token));
|
||||
MultivaluedMapImpl postParams = new MultivaluedMapImpl();
|
||||
postParams.add("name", "Tag 3");
|
||||
ClientResponse response = tagResource.put(ClientResponse.class, postParams);
|
||||
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
||||
JSONObject json = response.getEntity(JSONObject.class);
|
||||
String tag3Id = json.optString("id");
|
||||
Assert.assertNotNull(tag3Id);
|
||||
|
||||
// Get all tags
|
||||
tagResource = resource().path("/tag/list");
|
||||
tagResource.addFilter(new CookieAuthenticationFilter(tag1Token));
|
||||
response = tagResource.get(ClientResponse.class);
|
||||
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
||||
json = response.getEntity(JSONObject.class);
|
||||
JSONArray tags = json.getJSONArray("tags");
|
||||
Assert.assertTrue(tags.length() > 0);
|
||||
|
||||
// Deletes a tag
|
||||
tagResource = resource().path("/tag/" + tag3Id);
|
||||
tagResource.addFilter(new CookieAuthenticationFilter(tag1Token));
|
||||
response = tagResource.delete(ClientResponse.class);
|
||||
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
||||
json = response.getEntity(JSONObject.class);
|
||||
Assert.assertEquals("ok", json.getString("status"));
|
||||
|
||||
// Get all tags
|
||||
tagResource = resource().path("/tag/list");
|
||||
tagResource.addFilter(new CookieAuthenticationFilter(tag1Token));
|
||||
response = tagResource.get(ClientResponse.class);
|
||||
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
||||
json = response.getEntity(JSONObject.class);
|
||||
tags = json.getJSONArray("tags");
|
||||
Assert.assertTrue(tags.length() == 0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user