Commit 5764cf3a by lanmw

add operable attribute

parent 196f32d6
......@@ -85,7 +85,8 @@ public class TagCtrl {
@RequestParam(required = false, value = "domain") Integer domain,
@RequestParam("pageNo") Integer pageNo,
@RequestParam("pageSize") Integer pageSize) {
return tagService.searchSystemTagByPage(keyword, domain, new Page(pageSize, pageNo));
String userName = UserInfoUtils.getUserName();
return tagService.searchSystemTagByPage(userName, keyword, domain, new Page(pageSize, pageNo));
}
@ApiOperation(value = "搜索个人标签", notes = "搜索个人标签")
......
package com.keymobile.tagmanager.model;
import org.springframework.beans.BeanUtils;
import com.keymobile.tagmanager.util.Constants;
public class ExtTag extends Tag{
/**
*
*/
private static final long serialVersionUID = 1L;
private String operable = Constants.TAG_OPERABLE_FALSE;
public ExtTag(Tag t, String operable) {
BeanUtils.copyProperties(t, this);
this.operable = operable;
}
public void setOperable(String operable) {
this.operable = operable;
}
public String getOperable() {
return operable;
}
}
......@@ -20,6 +20,7 @@ import org.springframework.util.StringUtils;
import com.keymobile.tagmanager.exception.TagDuplicateException;
import com.keymobile.tagmanager.exception.TagException;
import com.keymobile.tagmanager.exception.TagNotExistException;
import com.keymobile.tagmanager.model.ExtTag;
import com.keymobile.tagmanager.model.Page;
import com.keymobile.tagmanager.model.Tag;
import com.keymobile.tagmanager.persistence.TagRepository;
......@@ -39,17 +40,18 @@ public class TagService {
private Logger logger = LoggerFactory.getLogger(TagService.class);
public Tag addOrUpdateTag(String parentId, Tag tag, String userName) throws TagNotExistException, TagDuplicateException, TagException {
tag.setPath(tag.getName());
if (!StringUtils.isEmpty(parentId)) {
Tag parent = getTagById(parentId);
tag.setPath(parent.getPath() + Constants.TAG_PATH_SEPARATOR + tag.getName());
} else {
tag.setPath(tag.getName());
}
changeNameToAvoidConflict(tag, userName);
checkTagValid(tag, userName);
if (!StringUtils.isEmpty(tag.getId())) {
Tag origin = mongoOperations.findOne(Query.query(Criteria.where("_id").is(tag.getId())), Tag.class);
List<Tag> relationTags = mongoOperations.find(
Query.query(Criteria.where("path").regex("^" + origin.getPath() + Constants.TAG_PATH_SEPARATOR)
.and("creator").is(userName)), Tag.class);
Query.query(createRelationCriteria(userName, origin)), Tag.class);
relationTags.forEach(p -> {
p.setPath(p.getPath().replaceAll(origin.getPath(), tag.getPath()));
});
......@@ -58,6 +60,25 @@ public class TagService {
tag.setLevel(tag.getPath().split(",").length);
return tagRepository.save(tag);
}
private void changeNameToAvoidConflict(Tag tag, String userName) {
Tag t = mongoOperations.findOne(Query.query(Criteria.where("path").is(tag.getPath())
.and("creator").ne(userName)), Tag.class);
if (t != null) {
String changeName = String.format("%s(%s)", t.getName(), userName);
logger.info(String.format("other creator has create same name in target, change tagName to %s", changeName));
tag.setName(changeName);
tag.setPath(t.getPath().substring(0, t.getPath().lastIndexOf(Constants.TAG_PATH_SEPARATOR)) + t.getName());
}
}
private Criteria createRelationCriteria(String userName, Tag origin) {
if (Constants.TAG_DIMENSION_TRUE.equals(origin.getDimensionType())) {
return Criteria.where("path").regex("^" + origin.getPath() + Constants.TAG_PATH_SEPARATOR);
}
return Criteria.where("path").regex("^" + origin.getPath() + Constants.TAG_PATH_SEPARATOR)
.and("creator").is(userName);
}
public Tag getTagById(String tagId) throws TagNotExistException {
Tag t = mongoOperations.findOne(Query.query(Criteria.where("_id").is(tagId)), Tag.class);
......@@ -100,14 +121,19 @@ public class TagService {
return new JsonNode[] {};
}
private Query createPersonalTagQuery(String userName) {
return Query.query(Criteria.where("tagType").is(Constants.TAG_PERSONAL_TYPE)
.orOperator(
Criteria.where("dimensionType").is(Constants.TAG_DIMENSION_TRUE),
Criteria.where("isOpen").is(Constants.TAG_OPEN_STATUS),
Criteria.where("creator").is(userName)))
private Query createPersonalTagQuery(String userName, Criteria... serachCriterias) {
return Query.query(
createPersonalTagCriteria(userName))
.with(Sort.by(getDefaultTagOrders()));
}
private Criteria createPersonalTagCriteria(String userName) {
return Criteria.where("tagType").is(Constants.TAG_PERSONAL_TYPE)
.orOperator(
Criteria.where("dimensionType").is(Constants.TAG_DIMENSION_TRUE),
Criteria.where("isOpen").is(Constants.TAG_OPEN_STATUS),
Criteria.where("creator").is(userName));
}
private Query createPersonalExcludeOpenTypeTagQuery(String userName) {
return Query.query(Criteria.where("tagType").is(Constants.TAG_PERSONAL_TYPE)
......@@ -138,27 +164,27 @@ public class TagService {
public Page searchPersonalTagByPage(String userName, String keyword, Integer domain, Page page) {
Query q = createPersonalTagQuery(userName);
Criteria andCriterias = createPersonalTagCriteria(userName);
if (org.apache.commons.lang.StringUtils.isNotBlank(keyword) && !"*".equals(keyword)) {
Criteria c = new Criteria();
c.orOperator(createRegexQuery(Arrays.asList("name", "nameEn", "desc"), keyword));
q.addCriteria(c);
andCriterias.andOperator(createRegexQuery(Arrays.asList("name", "nameEn", "desc"), keyword));
}
Query q = new Query(andCriterias);
q.skip(page.getOffset());
q.limit(page.getPageSize());
List<Tag> tags = mongoOperations
.find(q, Tag.class);
long count = mongoOperations.count(q, Tag.class);
page.setData(tags);
page.setData(decoratorToExtTag(tags, userName));
page.setTotal(count);
return page;
}
private Criteria[] createRegexQuery(List<String> asList, String keyword) {
return asList.stream().map(col -> {
private Criteria createRegexQuery(List<String> asList, String keyword) {
Criteria criteriaSearch = new Criteria();
return criteriaSearch.orOperator(asList.stream().map(col -> {
return Criteria.where(col).regex(keyword, "i");
}).collect(Collectors.toList()).toArray(new Criteria[0]);
}).collect(Collectors.toList()).toArray(new Criteria[0]));
}
public JsonNode[] querySystemTagAsTree(String parentId) throws TagNotExistException {
......@@ -183,7 +209,7 @@ public class TagService {
return new JsonNode[] {};
}
public Page searchSystemTagByPage(String keyword, Integer domain, Page page) {
public Page searchSystemTagByPage(String userName, String keyword, Integer domain, Page page) {
Map<String, Object> condition = new HashMap<>();
if (org.apache.commons.lang.StringUtils.isNotBlank(keyword) && !"*".equals(keyword)) {
MongoOperationsUtil.addSearchCriteria(Arrays.asList("name", "nameEn", "desc"), keyword, condition);
......@@ -192,12 +218,19 @@ public class TagService {
Query q = MongoOperationsUtil.createQueryWithSpecifiedField(condition, page, getDefaultTagOrders());
List<Tag> tags = mongoOperations.find(q, Tag.class);
long count = mongoOperations.count(q, Tag.class);
page.setData(tags);
page.setData(decoratorToExtTag(tags, userName));
page.setTotal(count);
return page;
}
private List<Tag> getSystemSubTag(Tag parentTag) {
private List<ExtTag> decoratorToExtTag(List<Tag> tags, String userName) {
return tags.stream().map(t -> {
return new ExtTag(t, t.getCreator().equals(userName) ? Constants.TAG_OPERABLE_TRUE :
Constants.TAG_OPERABLE_FALSE);
}).collect(Collectors.toList());
}
private List<Tag> getSystemSubTag(Tag parentTag) {
List<Tag> dirs = mongoOperations.find(new Query().addCriteria(Criteria.where("path")
.regex("^"+parentTag.getPath() + Constants.TAG_PATH_SEPARATOR)
.and("tagType").is(Constants.TAG_SYSTEM_TYPE))
......@@ -210,9 +243,7 @@ public class TagService {
Optional<Tag> optional = tagRepository.findById(tagId);
if (optional.isPresent()) {
Tag parentTag = optional.get();
List<Tag> childs = mongoOperations.find(Query.query(Criteria.where("path")
.regex("^"+parentTag.getPath() + Constants.TAG_PATH_SEPARATOR)
.and("creator").is(userName)), Tag.class);
List<Tag> childs = mongoOperations.find(Query.query(createRelationCriteria(userName, parentTag)), Tag.class);
if (!childs.isEmpty()) {
throw new TagException("存在子节点,不允许删除!");
}
......@@ -224,9 +255,7 @@ public class TagService {
Optional<Tag> optional = tagRepository.findById(tagId);
if (optional.isPresent()) {
Tag parentTag = optional.get();
List<Tag> childs = mongoOperations.find(Query.query(Criteria.where("path")
.regex("^"+parentTag.getPath() + Constants.TAG_PATH_SEPARATOR)
.and("creator").is(userName)), Tag.class);
List<Tag> childs = mongoOperations.find(Query.query(createRelationCriteria(userName, parentTag)), Tag.class);
return !childs.isEmpty();
}
return false;
......@@ -271,7 +300,8 @@ public class TagService {
}
private Query createDimensionTagQuery() {
return Query.query(Criteria.where("tagType").is(Constants.TAG_DIMENSION_TRUE))
return Query.query(Criteria.where("dimensionType").is(Constants.TAG_DIMENSION_TRUE)
.and("tagType").is(Constants.TAG_PERSONAL_TYPE))
.with(Sort.by(getDefaultTagOrders()));
}
......
......@@ -46,6 +46,8 @@ public final class Constants {
public static final String TAG_CLOSE_STATUS = "0";
public static final String TAG_DIMENSION_TRUE = "1";
public static final String TAG_DIMENSION_FALSE = "0";
public static final String TAG_OPERABLE_TRUE = "1";
public static final String TAG_OPERABLE_FALSE = "0";
public static final String PROCESS_AUDITSTATUS_APPLYING = "applying";
......
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