1
0
mirror of https://github.com/sismics/docs.git synced 2026-01-06 21:39:27 +00:00

Server side tag system

This commit is contained in:
jendib
2013-07-29 22:55:32 +02:00
parent 74132103bb
commit 62b6512996
14 changed files with 808 additions and 6 deletions

View File

@@ -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.

View File

@@ -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();
}
}

View File

@@ -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.
*/

View 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>