com.pkrete.locationservice.admin.controller.rest.v1.AdminRestController.java Source code

Java tutorial

Introduction

Here is the source code for com.pkrete.locationservice.admin.controller.rest.v1.AdminRestController.java

Source

/**
 * This file is part of Location Service :: Admin. Copyright (C) 2014 Petteri
 * Kivimki
 *
 * Location Service :: Admin is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * Location Service :: Admin is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * Location Service :: Admin. If not, see <http://www.gnu.org/licenses/>.
 */
package com.pkrete.locationservice.admin.controller.rest.v1;

import com.pkrete.locationservice.admin.exception.OperationFailedException;
import com.pkrete.locationservice.admin.service.LanguagesService;
import com.pkrete.locationservice.admin.service.LocationsService;
import com.pkrete.locationservice.admin.service.OwnersService;
import com.pkrete.locationservice.admin.solr.model.DocumentType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * This controller provides access to admin functionality e.g. regenerating
 * search indexes.
 *
 * INDEX /admin/index [GET] INDEX /admin/index [POST]
 * ?type=location|owner|language|all
 *
 * @author Petteri Kivimki
 */
@Controller
@RequestMapping("/admin")
public class AdminRestController extends RestController {

    private final static Logger logger = LoggerFactory.getLogger(AdminRestController.class.getName());
    @Autowired
    @Qualifier("locationsService")
    private LocationsService locationsService;
    @Autowired
    @Qualifier("ownersService")
    private OwnersService ownersService;
    @Autowired
    @Qualifier("languagesService")
    private LanguagesService languagesService;

    @RequestMapping(value = "/index", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public List indexList(HttpServletRequest request) {
        // Return document type list
        return Arrays.asList(DocumentType.values());
    }

    @RequestMapping(value = "/index", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public Map indexGenerate(HttpServletRequest request) throws OperationFailedException {
        // Get operator id
        String operator = (String) request.getAttribute("operator");
        // Message that's returned
        String message;
        if (request.getParameter("type") == null) {
            logger.warn("Indexing cannot be performed, because \"type\" parameter is missing. Operator : \"{}\".",
                    operator);
            // Throw exception
            throw new OperationFailedException(getMessage("rest.error.parameter.missing"));
        }
        // Get index type that should be regenerated
        DocumentType type = this.parseType(request.getParameter("type"));
        if (type == DocumentType.LOCATION) {
            this.locationsService.recreateSearchIndex();
            message = "Location index regenerated by \"" + operator + "\".";
        } else if (type == DocumentType.LANGUAGE) {
            this.languagesService.recreateSearchIndex();
            message = "Language index regenerated by \"" + operator + "\".";
        } else if (type == DocumentType.OWNER) {
            this.ownersService.recreateSearchIndex();
            message = "Owner index regenerated by \"" + operator + "\".";
        } else {
            this.locationsService.recreateSearchIndex();
            this.languagesService.recreateSearchIndex();
            this.ownersService.recreateSearchIndex();
            message = "Location, language and owner indexes regenerated by \"" + operator + "\".";
        }
        logger.info(message);
        // Create Map containing the message
        Map result = new HashMap();
        // Add message rest.admin.index.response
        result.put("message", getMessage("rest.admin.index.response"));
        // Return result
        return result;
    }

    private DocumentType parseType(String type) {
        if (type == null || type.isEmpty()) {
            return null;
        }
        if (type.equalsIgnoreCase(DocumentType.LOCATION.toString())) {
            return DocumentType.LOCATION;
        } else if (type.equalsIgnoreCase(DocumentType.LANGUAGE.toString())) {
            return DocumentType.LANGUAGE;
        } else if (type.equalsIgnoreCase(DocumentType.OWNER.toString())) {
            return DocumentType.OWNER;
        }
        return null;
    }
}