Commit 611c201d authored by Matija Obreza's avatar Matija Obreza

Lookup controller and country JSON serializer

parent 3023a192
......@@ -32,17 +32,20 @@ import javax.persistence.Table;
import javax.persistence.Transient;
import org.genesys2.server.model.BusinessModel;
import org.genesys2.server.servlet.controller.rest.serialization.CountrySerializer;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@Entity
@Table(name = "country")
// TODO Fix indexer by i18n names!
@Indexed
@JsonSerialize(using = CountrySerializer.class)
public class Country extends BusinessModel {
private static final long serialVersionUID = -1688723909298769804L;
......
/**
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.servlet.controller.rest;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import org.apache.commons.lang3.StringUtils;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.TaxonomyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = { "/api/v0/lookup", "/json/v0/lookup" })
public class LookupController extends RestController {
@Autowired
GenesysService genesysService;
@Autowired
InstituteService instituteService;
@Autowired
GeoService geoService;
@Autowired
TaxonomyService taxonomyService;
@RequestMapping(value = "/orgCty", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Map<String, String> get(@RequestParam(value = "lang", required = false, defaultValue = "") String lang) {
Map<String, String> orgCty = new TreeMap<String, String>();
List<Country> list = null;
if (StringUtils.isNotBlank(lang)) {
Locale locale = new Locale(lang);
list = geoService.listAll(locale);
for (Country c : list) {
orgCty.put(c.getCode3(), c.getName(locale));
}
} else {
list = geoService.listAll();
for (Country c : list) {
orgCty.put(c.getCode3(), c.getName());
}
}
return orgCty;
}
}
package org.genesys2.server.servlet.controller.rest.serialization;
import java.io.IOException;
import org.genesys2.server.model.impl.Country;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
public class CountrySerializer extends JsonSerializer<Country> {
@Override
public void serialize(Country country, JsonGenerator jgen, SerializerProvider sp) throws IOException, JsonProcessingException {
if (country == null) {
jgen.writeNull();
} else {
jgen.writeStartObject();
jgen.writeObjectField("code", country.getCode3());
jgen.writeObjectField("name", country.getName());
jgen.writeObjectField("refnameId", country.getRefnameId());
jgen.writeObjectField("current", country.isCurrent());
jgen.writeEndObject();
}
}
}
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