Search in sources :

Example 46 with RequestParam

use of org.springframework.web.bind.annotation.RequestParam in project goci by EBISPOT.

the class AssociationController method addSnpInteraction.

@RequestMapping(value = "/studies/{studyId}/associations/add_interaction", produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.POST)
public String addSnpInteraction(@ModelAttribute("form") @Valid SnpAssociationInteractionForm snpAssociationInteractionForm, BindingResult bindingResult, @PathVariable Long studyId, Model model, @RequestParam(required = true) String measurementType, HttpServletRequest request) throws EnsemblMappingException {
    Study study = studyRepository.findOne(studyId);
    model.addAttribute("study", study);
    model.addAttribute("measurementType", measurementType);
    // Binding vs Validator issue. File: messages.properties
    if (bindingResult.hasErrors()) {
        model.addAttribute("form", snpAssociationInteractionForm);
        return "add_snp_interaction_association";
    }
    // Check for errors in form that would prevent saving an association
    List<AssociationValidationView> colErrors = associationOperationsService.checkSnpAssociationInteractionFormErrorsForView(snpAssociationInteractionForm, measurementType);
    if (!colErrors.isEmpty()) {
        model.addAttribute("errors", colErrors);
        model.addAttribute("form", snpAssociationInteractionForm);
        model.addAttribute("criticalErrorsFound", true);
        return "add_snp_interaction_association";
    } else {
        // Create an association object from details in returned form
        Association newAssociation = snpInteractionAssociationService.createAssociation(snpAssociationInteractionForm);
        // Save and validate form
        String eRelease = ensemblRestTemplateService.getRelease();
        Collection<AssociationValidationView> errors = associationOperationsService.saveAssociationCreatedFromForm(study, newAssociation, currentUserDetailsService.getUserFromRequest(request), eRelease);
        // Determine if we have any errors rather than warnings
        long errorCount = errors.stream().filter(validationError -> !validationError.getWarning()).count();
        if (errorCount > 0) {
            model.addAttribute("errors", errors);
            model.addAttribute("form", snpAssociationInteractionForm);
            model.addAttribute("criticalErrorsFound", true);
            return "add_snp_interaction_association";
        } else {
            return "redirect:/associations/" + newAssociation.getId();
        }
    }
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) CheckMappingService(uk.ac.ebi.spot.goci.curation.service.CheckMappingService) LoggerFactory(org.slf4j.LoggerFactory) SnpAssociationInteractionForm(uk.ac.ebi.spot.goci.curation.model.SnpAssociationInteractionForm) Autowired(org.springframework.beans.factory.annotation.Autowired) Valid(javax.validation.Valid) AssociationOperationsService(uk.ac.ebi.spot.goci.curation.service.AssociationOperationsService) PreDestroy(javax.annotation.PreDestroy) Model(org.springframework.ui.Model) Future(java.util.concurrent.Future) AssociationDeletionService(uk.ac.ebi.spot.goci.curation.service.AssociationDeletionService) uk.ac.ebi.spot.goci.model(uk.ac.ebi.spot.goci.model) StudyRepository(uk.ac.ebi.spot.goci.repository.StudyRepository) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) EnsemblRestTemplateService(uk.ac.ebi.spot.goci.service.EnsemblRestTemplateService) DateFormat(java.text.DateFormat) RedirectAttributes(org.springframework.web.servlet.mvc.support.RedirectAttributes) HttpSession(javax.servlet.http.HttpSession) MediaType(org.springframework.http.MediaType) EventsViewService(uk.ac.ebi.spot.goci.curation.service.EventsViewService) PageRequest(org.springframework.data.domain.PageRequest) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Page(org.springframework.data.domain.Page) SnpFormColumn(uk.ac.ebi.spot.goci.curation.model.SnpFormColumn) AssociationUploadErrorView(uk.ac.ebi.spot.goci.curation.model.AssociationUploadErrorView) FileNotFoundException(java.io.FileNotFoundException) Executors(java.util.concurrent.Executors) SingleSnpMultiSnpAssociationService(uk.ac.ebi.spot.goci.curation.service.SingleSnpMultiSnpAssociationService) SnpAssociationTableViewService(uk.ac.ebi.spot.goci.curation.service.SnpAssociationTableViewService) LastViewedAssociation(uk.ac.ebi.spot.goci.curation.model.LastViewedAssociation) SnpInteractionAssociationService(uk.ac.ebi.spot.goci.curation.service.SnpInteractionAssociationService) InitBinder(org.springframework.web.bind.annotation.InitBinder) CurrentUserDetailsService(uk.ac.ebi.spot.goci.curation.service.CurrentUserDetailsService) FileUploadException(uk.ac.ebi.spot.goci.curation.exception.FileUploadException) Async(org.springframework.scheduling.annotation.Async) java.util(java.util) SnpAssociationTableView(uk.ac.ebi.spot.goci.curation.model.SnpAssociationTableView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) SimpleDateFormat(java.text.SimpleDateFormat) BindingResult(org.springframework.validation.BindingResult) Callable(java.util.concurrent.Callable) Controller(org.springframework.stereotype.Controller) SnpAssociationStandardMultiForm(uk.ac.ebi.spot.goci.curation.model.SnpAssociationStandardMultiForm) EfoTraitRepository(uk.ac.ebi.spot.goci.repository.EfoTraitRepository) AssociationService(uk.ac.ebi.spot.goci.service.AssociationService) Value(org.springframework.beans.factory.annotation.Value) AssociationValidationReportService(uk.ac.ebi.spot.goci.curation.service.AssociationValidationReportService) HttpServletRequest(javax.servlet.http.HttpServletRequest) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ExecutorService(java.util.concurrent.ExecutorService) DataIntegrityException(uk.ac.ebi.spot.goci.curation.exception.DataIntegrityException) Logger(org.slf4j.Logger) AssociationUploadService(uk.ac.ebi.spot.goci.curation.service.AssociationUploadService) AssociationRepository(uk.ac.ebi.spot.goci.repository.AssociationRepository) HttpServletResponse(javax.servlet.http.HttpServletResponse) SnpAssociationForm(uk.ac.ebi.spot.goci.curation.model.SnpAssociationForm) EnsemblMappingException(uk.ac.ebi.spot.goci.exception.EnsemblMappingException) SheetProcessingException(uk.ac.ebi.spot.goci.exception.SheetProcessingException) IOException(java.io.IOException) CheckEfoTermAssignmentService(uk.ac.ebi.spot.goci.curation.service.CheckEfoTermAssignmentService) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) StudyAssociationBatchDeletionEventService(uk.ac.ebi.spot.goci.curation.service.StudyAssociationBatchDeletionEventService) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) MapCatalogService(uk.ac.ebi.spot.goci.service.MapCatalogService) SnpFormRow(uk.ac.ebi.spot.goci.curation.model.SnpFormRow) WebDataBinder(org.springframework.web.bind.WebDataBinder) MultipartFile(org.springframework.web.multipart.MultipartFile) AssociationDownloadService(uk.ac.ebi.spot.goci.curation.service.AssociationDownloadService) AssociationValidationView(uk.ac.ebi.spot.goci.curation.model.AssociationValidationView) MappingDetails(uk.ac.ebi.spot.goci.curation.model.MappingDetails) LastViewedAssociation(uk.ac.ebi.spot.goci.curation.model.LastViewedAssociation) AssociationValidationView(uk.ac.ebi.spot.goci.curation.model.AssociationValidationView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 47 with RequestParam

use of org.springframework.web.bind.annotation.RequestParam in project goci by EBISPOT.

the class AssociationController method addStandardSnps.

// Add new standard association/snp information to a study
@RequestMapping(value = "/studies/{studyId}/associations/add_standard", produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.POST)
public String addStandardSnps(@ModelAttribute("form") @Valid SnpAssociationStandardMultiForm snpAssociationStandardMultiForm, BindingResult bindingResult, @PathVariable Long studyId, Model model, @RequestParam(required = true) String measurementType, HttpServletRequest request) throws EnsemblMappingException {
    Study study = studyRepository.findOne(studyId);
    model.addAttribute("study", study);
    model.addAttribute("measurementType", measurementType);
    // Binding vs Validator issue. File: messages.properties
    if (bindingResult.hasErrors()) {
        model.addAttribute("form", snpAssociationStandardMultiForm);
        return "add_standard_snp_association";
    }
    // Check for errors in form that would prevent saving an association
    List<AssociationValidationView> rowErrors = associationOperationsService.checkSnpAssociationFormErrors(snpAssociationStandardMultiForm, measurementType);
    if (!rowErrors.isEmpty()) {
        model.addAttribute("errors", rowErrors);
        model.addAttribute("form", snpAssociationStandardMultiForm);
        model.addAttribute("criticalErrorsFound", true);
        return "add_standard_snp_association";
    } else {
        // Create an association object from details in returned form
        Association newAssociation = singleSnpMultiSnpAssociationService.createAssociation(snpAssociationStandardMultiForm);
        // Save and validate form
        String eRelease = ensemblRestTemplateService.getRelease();
        Collection<AssociationValidationView> errors = associationOperationsService.saveAssociationCreatedFromForm(study, newAssociation, currentUserDetailsService.getUserFromRequest(request), eRelease);
        // Determine if we have any errors rather than warnings
        long errorCount = errors.stream().filter(validationError -> !validationError.getWarning()).count();
        if (errorCount > 0) {
            model.addAttribute("errors", errors);
            model.addAttribute("form", snpAssociationStandardMultiForm);
            model.addAttribute("criticalErrorsFound", true);
            return "add_standard_snp_association";
        } else {
            return "redirect:/associations/" + newAssociation.getId();
        }
    }
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) CheckMappingService(uk.ac.ebi.spot.goci.curation.service.CheckMappingService) LoggerFactory(org.slf4j.LoggerFactory) SnpAssociationInteractionForm(uk.ac.ebi.spot.goci.curation.model.SnpAssociationInteractionForm) Autowired(org.springframework.beans.factory.annotation.Autowired) Valid(javax.validation.Valid) AssociationOperationsService(uk.ac.ebi.spot.goci.curation.service.AssociationOperationsService) PreDestroy(javax.annotation.PreDestroy) Model(org.springframework.ui.Model) Future(java.util.concurrent.Future) AssociationDeletionService(uk.ac.ebi.spot.goci.curation.service.AssociationDeletionService) uk.ac.ebi.spot.goci.model(uk.ac.ebi.spot.goci.model) StudyRepository(uk.ac.ebi.spot.goci.repository.StudyRepository) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) EnsemblRestTemplateService(uk.ac.ebi.spot.goci.service.EnsemblRestTemplateService) DateFormat(java.text.DateFormat) RedirectAttributes(org.springframework.web.servlet.mvc.support.RedirectAttributes) HttpSession(javax.servlet.http.HttpSession) MediaType(org.springframework.http.MediaType) EventsViewService(uk.ac.ebi.spot.goci.curation.service.EventsViewService) PageRequest(org.springframework.data.domain.PageRequest) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Page(org.springframework.data.domain.Page) SnpFormColumn(uk.ac.ebi.spot.goci.curation.model.SnpFormColumn) AssociationUploadErrorView(uk.ac.ebi.spot.goci.curation.model.AssociationUploadErrorView) FileNotFoundException(java.io.FileNotFoundException) Executors(java.util.concurrent.Executors) SingleSnpMultiSnpAssociationService(uk.ac.ebi.spot.goci.curation.service.SingleSnpMultiSnpAssociationService) SnpAssociationTableViewService(uk.ac.ebi.spot.goci.curation.service.SnpAssociationTableViewService) LastViewedAssociation(uk.ac.ebi.spot.goci.curation.model.LastViewedAssociation) SnpInteractionAssociationService(uk.ac.ebi.spot.goci.curation.service.SnpInteractionAssociationService) InitBinder(org.springframework.web.bind.annotation.InitBinder) CurrentUserDetailsService(uk.ac.ebi.spot.goci.curation.service.CurrentUserDetailsService) FileUploadException(uk.ac.ebi.spot.goci.curation.exception.FileUploadException) Async(org.springframework.scheduling.annotation.Async) java.util(java.util) SnpAssociationTableView(uk.ac.ebi.spot.goci.curation.model.SnpAssociationTableView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) SimpleDateFormat(java.text.SimpleDateFormat) BindingResult(org.springframework.validation.BindingResult) Callable(java.util.concurrent.Callable) Controller(org.springframework.stereotype.Controller) SnpAssociationStandardMultiForm(uk.ac.ebi.spot.goci.curation.model.SnpAssociationStandardMultiForm) EfoTraitRepository(uk.ac.ebi.spot.goci.repository.EfoTraitRepository) AssociationService(uk.ac.ebi.spot.goci.service.AssociationService) Value(org.springframework.beans.factory.annotation.Value) AssociationValidationReportService(uk.ac.ebi.spot.goci.curation.service.AssociationValidationReportService) HttpServletRequest(javax.servlet.http.HttpServletRequest) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ExecutorService(java.util.concurrent.ExecutorService) DataIntegrityException(uk.ac.ebi.spot.goci.curation.exception.DataIntegrityException) Logger(org.slf4j.Logger) AssociationUploadService(uk.ac.ebi.spot.goci.curation.service.AssociationUploadService) AssociationRepository(uk.ac.ebi.spot.goci.repository.AssociationRepository) HttpServletResponse(javax.servlet.http.HttpServletResponse) SnpAssociationForm(uk.ac.ebi.spot.goci.curation.model.SnpAssociationForm) EnsemblMappingException(uk.ac.ebi.spot.goci.exception.EnsemblMappingException) SheetProcessingException(uk.ac.ebi.spot.goci.exception.SheetProcessingException) IOException(java.io.IOException) CheckEfoTermAssignmentService(uk.ac.ebi.spot.goci.curation.service.CheckEfoTermAssignmentService) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) StudyAssociationBatchDeletionEventService(uk.ac.ebi.spot.goci.curation.service.StudyAssociationBatchDeletionEventService) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) MapCatalogService(uk.ac.ebi.spot.goci.service.MapCatalogService) SnpFormRow(uk.ac.ebi.spot.goci.curation.model.SnpFormRow) WebDataBinder(org.springframework.web.bind.WebDataBinder) MultipartFile(org.springframework.web.multipart.MultipartFile) AssociationDownloadService(uk.ac.ebi.spot.goci.curation.service.AssociationDownloadService) AssociationValidationView(uk.ac.ebi.spot.goci.curation.model.AssociationValidationView) MappingDetails(uk.ac.ebi.spot.goci.curation.model.MappingDetails) LastViewedAssociation(uk.ac.ebi.spot.goci.curation.model.LastViewedAssociation) AssociationValidationView(uk.ac.ebi.spot.goci.curation.model.AssociationValidationView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 48 with RequestParam

use of org.springframework.web.bind.annotation.RequestParam in project ontrack by nemerosa.

the class APIController method describe.

@RequestMapping(value = "/describe", method = RequestMethod.GET)
public APIDescription describe(HttpServletRequest request, @RequestParam String path) throws Exception {
    HandlerExecutionChain executionChain = handlerMapping.getHandler(new HttpServletRequestWrapper(request) {

        @Override
        public String getRequestURI() {
            return path;
        }

        @Override
        public String getServletPath() {
            return path;
        }
    });
    // Gets the handler
    Object handler = executionChain.getHandler();
    if (handler instanceof HandlerMethod) {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        String type = handlerMethod.getBeanType().getName();
        String method = handlerMethod.getMethod().getName();
        // Gets all the infos
        List<APIInfo> apiInfos = getApiInfos();
        // Looking for any GET mapping
        APIMethodInfo get = apiInfos.stream().flatMap(i -> i.getMethods().stream()).filter(mi -> StringUtils.equals(type, mi.getApiInfo().getType()) && StringUtils.equals(method, mi.getMethod())).findFirst().orElseThrow(() -> new APIMethodInfoNotFoundException(path));
        // Gets all methods with the same path pattern
        List<APIMethodInfo> methods = apiInfos.stream().flatMap(i -> i.getMethods().stream()).filter(mi -> StringUtils.equals(get.getPath(), mi.getPath())).collect(Collectors.toList());
        // OK
        return new APIDescription(path, methods);
    } else {
        throw new APIMethodInfoNotFoundException(path);
    }
}
Also used : API(net.nemerosa.ontrack.ui.support.API) RequestParam(org.springframework.web.bind.annotation.RequestParam) java.util(java.util) RunProfile(net.nemerosa.ontrack.common.RunProfile) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Controller(org.springframework.stereotype.Controller) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) StringUtils(org.apache.commons.lang3.StringUtils) HandlerMethod(org.springframework.web.method.HandlerMethod) HttpServletRequest(javax.servlet.http.HttpServletRequest) Resources(net.nemerosa.ontrack.ui.resource.Resources) NameDescription.nd(net.nemerosa.ontrack.model.structure.NameDescription.nd) APIMethodInfo(net.nemerosa.ontrack.boot.support.APIMethodInfo) Qualifier(org.springframework.beans.factory.annotation.Qualifier) Method(java.lang.reflect.Method) APIDescription(net.nemerosa.ontrack.boot.support.APIDescription) AbstractResourceController(net.nemerosa.ontrack.ui.controller.AbstractResourceController) StringUtils.capitalize(org.apache.commons.lang3.StringUtils.capitalize) MvcUriComponentsBuilder.on(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.on) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) NameDescription(net.nemerosa.ontrack.model.structure.NameDescription) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) APIMethodInfoNotFoundException(net.nemerosa.ontrack.model.exceptions.APIMethodInfoNotFoundException) ApplicationContext(org.springframework.context.ApplicationContext) Profile(org.springframework.context.annotation.Profile) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) ReflectionUtils(org.springframework.util.ReflectionUtils) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) APIInfo(net.nemerosa.ontrack.boot.support.APIInfo) APIMethod(net.nemerosa.ontrack.ui.support.APIMethod) APIInfo(net.nemerosa.ontrack.boot.support.APIInfo) APIDescription(net.nemerosa.ontrack.boot.support.APIDescription) HandlerMethod(org.springframework.web.method.HandlerMethod) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) APIMethodInfoNotFoundException(net.nemerosa.ontrack.model.exceptions.APIMethodInfoNotFoundException) APIMethodInfo(net.nemerosa.ontrack.boot.support.APIMethodInfo) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 49 with RequestParam

use of org.springframework.web.bind.annotation.RequestParam 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 50 with RequestParam

use of org.springframework.web.bind.annotation.RequestParam 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

RequestParam (org.springframework.web.bind.annotation.RequestParam)62 List (java.util.List)46 PathVariable (org.springframework.web.bind.annotation.PathVariable)42 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)42 Collectors (java.util.stream.Collectors)37 HttpServletRequest (javax.servlet.http.HttpServletRequest)34 HttpServletResponse (javax.servlet.http.HttpServletResponse)34 IOException (java.io.IOException)32 Autowired (org.springframework.beans.factory.annotation.Autowired)32 Map (java.util.Map)28 Controller (org.springframework.stereotype.Controller)28 GetMapping (org.springframework.web.bind.annotation.GetMapping)27 HttpStatus (org.springframework.http.HttpStatus)25 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)25 RequestMethod (org.springframework.web.bind.annotation.RequestMethod)23 MediaType (org.springframework.http.MediaType)22 Model (org.springframework.ui.Model)20 StringUtils (org.apache.commons.lang3.StringUtils)19 Set (java.util.Set)18 Logger (org.slf4j.Logger)18