1
0
mirror of https://github.com/sismics/docs.git synced 2025-12-14 10:16:21 +00:00

Closes #205: action: remote tag

This commit is contained in:
Benjamin Gamard
2018-03-13 14:09:39 +01:00
parent 995e45d28f
commit 2678ff4477
11 changed files with 204 additions and 34 deletions

View File

@@ -9,5 +9,10 @@ public enum ActionType {
/**
* Add a tag.
*/
ADD_TAG
ADD_TAG,
/**
* Remove a tag.
*/
REMOVE_TAG
}

View File

@@ -4,6 +4,7 @@ import com.sismics.docs.core.constant.ActionType;
import com.sismics.docs.core.dao.jpa.dto.DocumentDto;
import com.sismics.docs.core.util.action.Action;
import com.sismics.docs.core.util.action.AddTagAction;
import com.sismics.docs.core.util.action.RemoveTagAction;
import org.slf4j.LoggerFactory;
import javax.json.JsonObject;
@@ -19,6 +20,41 @@ public class ActionUtil {
*/
private static final org.slf4j.Logger log = LoggerFactory.getLogger(LuceneUtil.class);
/**
* Find the action associated to an action type.
*
* @param actionType Action type
* @return Action
*/
private static Action findAction(ActionType actionType) {
Action action = null;
switch (actionType) {
case ADD_TAG:
action = new AddTagAction();
break;
case REMOVE_TAG:
action = new RemoveTagAction();
break;
default:
log.error("Action type not handled: " + actionType);
break;
}
return action;
}
/**
* Validate an action.
*
* @param actionType Action type
* @param actionData Action data
* @throws Exception Validation error
*/
public static void validateAction(ActionType actionType, JsonObject actionData) throws Exception {
Action action = findAction(actionType);
action.validate(actionData);
}
/**
* Execute an action.
*
@@ -27,16 +63,7 @@ public class ActionUtil {
* @param documentDto Document DTO
*/
public static void executeAction(ActionType actionType, JsonObject actionData, DocumentDto documentDto) {
Action action;
switch (actionType) {
case ADD_TAG:
action = new AddTagAction();
break;
default:
log.error("Action type not handled: " + actionType);
return;
}
Action action = findAction(actionType);
action.execute(documentDto, actionData);
}
}

View File

@@ -17,4 +17,12 @@ public interface Action {
* @param action Action data
*/
void execute(DocumentDto documentDto, JsonObject action);
/**
* Validate the action.
*
* @param action Action data
* @throws Exception Validation error
*/
void validate(JsonObject action) throws Exception;
}

View File

@@ -15,7 +15,7 @@ import java.util.Set;
*
* @author bgamard
*/
public class AddTagAction implements Action {
public class AddTagAction extends TagAction {
@Override
public void execute(DocumentDto documentDto, JsonObject action) {
if (action.getString("tag") == null) {

View File

@@ -0,0 +1,37 @@
package com.sismics.docs.core.util.action;
import com.google.common.collect.Sets;
import com.sismics.docs.core.dao.jpa.TagDao;
import com.sismics.docs.core.dao.jpa.criteria.TagCriteria;
import com.sismics.docs.core.dao.jpa.dto.DocumentDto;
import com.sismics.docs.core.dao.jpa.dto.TagDto;
import javax.json.JsonObject;
import java.util.List;
import java.util.Set;
/**
* Action to remove a tag.
*
* @author bgamard
*/
public class RemoveTagAction extends TagAction {
@Override
public void execute(DocumentDto documentDto, JsonObject action) {
if (action.getString("tag") == null) {
return;
}
String tagId = action.getString("tag");
TagDao tagDao = new TagDao();
List<TagDto> tagDtoList = tagDao.findByCriteria(new TagCriteria().setDocumentId(documentDto.getId()), null);
Set<String> tagIdSet = Sets.newHashSet();
for (TagDto tagDto : tagDtoList) {
tagIdSet.add(tagDto.getId());
}
tagIdSet.remove(tagId);
tagDao.updateTagList(documentDto.getId(), tagIdSet);
}
}

View File

@@ -0,0 +1,23 @@
package com.sismics.docs.core.util.action;
import com.sismics.docs.core.dao.jpa.TagDao;
import com.sismics.docs.core.dao.jpa.criteria.TagCriteria;
import com.sismics.docs.core.dao.jpa.dto.TagDto;
import javax.json.JsonObject;
import java.util.List;
public abstract class TagAction implements Action {
@Override
public void validate(JsonObject action) throws Exception {
TagDao tagDao = new TagDao();
String tagId = action.getString("tag");
if (tagId == null) {
throw new Exception("step.transitions.actions.tag is required");
}
List<TagDto> tagDtoList = tagDao.findByCriteria(new TagCriteria().setId(tagId), null);
if (tagDtoList.size() != 1) {
throw new Exception(tagId + " is not a valid tag");
}
}
}