Search in sources :

Example 1 with TAGSLINK

use of com.erudika.scoold.ScooldServer.TAGSLINK in project scoold by Erudika.

the class TagsController method delete.

@PostMapping("/delete")
public String delete(@RequestParam String tag, HttpServletRequest req, HttpServletResponse res) {
    Profile authUser = utils.getAuthUser(req);
    if (utils.isMod(authUser)) {
        Tag t = pc.read(Utils.type(Tag.class), new Tag(tag).getId());
        if (t != null) {
            pc.delete(t);
            logger.info("User {} ({}) deleted tag '{}'.", authUser.getName(), authUser.getCreatorid(), t.getTag());
            Pager pager = new Pager(1, "_docid", false, Config.MAX_ITEMS_PER_PAGE);
            List<Question> questionslist;
            do {
                questionslist = pc.findTagged(Utils.type(Question.class), new String[] { t.getTag() }, pager);
                for (Question q : questionslist) {
                    t.setCount(t.getCount() + 1);
                    q.setTags(Optional.ofNullable(q.getTags()).orElse(Collections.emptyList()).stream().filter(ts -> !ts.equals(t.getTag())).distinct().collect(Collectors.toList()));
                    logger.debug("Removed tag {} from {} out of {} questions.", t.getTag(), questionslist.size(), pager.getCount());
                }
                if (!questionslist.isEmpty()) {
                    pc.updateAll(questionslist);
                }
            } while (!questionslist.isEmpty());
        }
    }
    if (utils.isAjaxRequest(req)) {
        res.setStatus(200);
        return "blank";
    } else {
        return "redirect:" + TAGSLINK + "?" + req.getQueryString();
    }
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) SIGNINLINK(com.erudika.scoold.ScooldServer.SIGNINLINK) Question(com.erudika.scoold.core.Question) RequestParam(org.springframework.web.bind.annotation.RequestParam) LoggerFactory(org.slf4j.LoggerFactory) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ParaClient(com.erudika.para.client.ParaClient) Pager(com.erudika.para.core.utils.Pager) Controller(org.springframework.stereotype.Controller) Inject(javax.inject.Inject) HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaType(javax.ws.rs.core.MediaType) Model(org.springframework.ui.Model) GetMapping(org.springframework.web.bind.annotation.GetMapping) Config(com.erudika.para.core.utils.Config) ScooldUtils(com.erudika.scoold.utils.ScooldUtils) PostMapping(org.springframework.web.bind.annotation.PostMapping) Logger(org.slf4j.Logger) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) Collectors(java.util.stream.Collectors) Utils(com.erudika.para.core.utils.Utils) List(java.util.List) Tag(com.erudika.para.core.Tag) TAGSLINK(com.erudika.scoold.ScooldServer.TAGSLINK) Optional(java.util.Optional) Collections(java.util.Collections) Profile(com.erudika.scoold.core.Profile) Pager(com.erudika.para.core.utils.Pager) Question(com.erudika.scoold.core.Question) Tag(com.erudika.para.core.Tag) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 2 with TAGSLINK

use of com.erudika.scoold.ScooldServer.TAGSLINK in project scoold by Erudika.

the class TagsController method rename.

@PostMapping
public String rename(@RequestParam String tag, @RequestParam String newtag, HttpServletRequest req, HttpServletResponse res, Model model) {
    Profile authUser = utils.getAuthUser(req);
    int count = 0;
    if (utils.isMod(authUser)) {
        Tag updated;
        Tag oldTag = new Tag(tag);
        Tag newTag = new Tag(newtag);
        Tag t = pc.read(Utils.type(Tag.class), oldTag.getId());
        if (t != null && !oldTag.getTag().equals(newTag.getTag())) {
            if (oldTag.getTag().equals(newTag.getTag())) {
                t.setCount(pc.getCount(Utils.type(Question.class), Collections.singletonMap(Config._TAGS, oldTag.getTag())).intValue());
                updated = pc.update(t);
            } else {
                pc.delete(t);
                t.setId(newtag);
                logger.info("User {} ({}) is renaming tag '{}' to '{}'.", authUser.getName(), authUser.getCreatorid(), oldTag.getTag(), t.getTag());
                t.setCount(pc.getCount(Utils.type(Question.class), Collections.singletonMap(Config._TAGS, newTag.getTag())).intValue());
                Pager pager = new Pager(1, "_docid", false, Config.MAX_ITEMS_PER_PAGE);
                List<Question> questionslist;
                do {
                    questionslist = pc.findTagged(Utils.type(Question.class), new String[] { oldTag.getTag() }, pager);
                    for (Question q : questionslist) {
                        t.setCount(t.getCount() + 1);
                        q.setTags(Optional.ofNullable(q.getTags()).orElse(Collections.emptyList()).stream().map(ts -> {
                            if (ts.equals(newTag.getTag())) {
                                t.setCount(t.getCount() - 1);
                            }
                            return ts.equals(oldTag.getTag()) ? t.getTag() : ts;
                        }).distinct().collect(Collectors.toList()));
                        logger.debug("Updated {} out of {} questions with new tag {}.", questionslist.size(), pager.getCount(), t.getTag());
                    }
                    if (!questionslist.isEmpty()) {
                        pc.updateAll(questionslist);
                    }
                } while (!questionslist.isEmpty());
                // overwrite new tag object
                updated = pc.create(t);
            }
            model.addAttribute("tag", updated);
            count = t.getCount();
        }
    }
    if (utils.isAjaxRequest(req)) {
        res.setStatus(200);
        res.setContentType("application/json");
        try {
            res.getWriter().println("{\"count\":" + count + ", \"tag\":\"" + new Tag(newtag).getTag() + "\"}");
        } catch (IOException ex) {
        }
        return "blank";
    } else {
        return "redirect:" + TAGSLINK + "?" + req.getQueryString();
    }
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) SIGNINLINK(com.erudika.scoold.ScooldServer.SIGNINLINK) Question(com.erudika.scoold.core.Question) RequestParam(org.springframework.web.bind.annotation.RequestParam) LoggerFactory(org.slf4j.LoggerFactory) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ParaClient(com.erudika.para.client.ParaClient) Pager(com.erudika.para.core.utils.Pager) Controller(org.springframework.stereotype.Controller) Inject(javax.inject.Inject) HttpServletRequest(javax.servlet.http.HttpServletRequest) MediaType(javax.ws.rs.core.MediaType) Model(org.springframework.ui.Model) GetMapping(org.springframework.web.bind.annotation.GetMapping) Config(com.erudika.para.core.utils.Config) ScooldUtils(com.erudika.scoold.utils.ScooldUtils) PostMapping(org.springframework.web.bind.annotation.PostMapping) Logger(org.slf4j.Logger) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) Collectors(java.util.stream.Collectors) Utils(com.erudika.para.core.utils.Utils) List(java.util.List) Tag(com.erudika.para.core.Tag) TAGSLINK(com.erudika.scoold.ScooldServer.TAGSLINK) Optional(java.util.Optional) Collections(java.util.Collections) Profile(com.erudika.scoold.core.Profile) Pager(com.erudika.para.core.utils.Pager) Question(com.erudika.scoold.core.Question) Tag(com.erudika.para.core.Tag) IOException(java.io.IOException) Profile(com.erudika.scoold.core.Profile) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

ParaClient (com.erudika.para.client.ParaClient)2 Tag (com.erudika.para.core.Tag)2 Config (com.erudika.para.core.utils.Config)2 Pager (com.erudika.para.core.utils.Pager)2 Utils (com.erudika.para.core.utils.Utils)2 SIGNINLINK (com.erudika.scoold.ScooldServer.SIGNINLINK)2 TAGSLINK (com.erudika.scoold.ScooldServer.TAGSLINK)2 Profile (com.erudika.scoold.core.Profile)2 Question (com.erudika.scoold.core.Question)2 ScooldUtils (com.erudika.scoold.utils.ScooldUtils)2 IOException (java.io.IOException)2 Collections (java.util.Collections)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 MediaType (javax.ws.rs.core.MediaType)2 Logger (org.slf4j.Logger)2