Commit 6cfaffce authored by Matija Obreza's avatar Matija Obreza
Browse files

Material request serializers and REST calls to list requests

parent 58ef443c
......@@ -17,19 +17,25 @@
package org.genesys2.server.model.genesys;
import java.text.MessageFormat;
import java.util.List;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import org.apache.commons.lang.math.RandomUtils;
import org.genesys2.server.model.VersionedAuditedModel;
import org.genesys2.server.servlet.controller.rest.serialization.MaterialRequestSerializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@Entity
@Table(name = "request")
@JsonSerialize(using = MaterialRequestSerializer.class)
public class MaterialRequest extends VersionedAuditedModel {
public static final int NOTVALIDATED = 0;
public static final int VALIDATED = 1;
......@@ -50,6 +56,9 @@ public class MaterialRequest extends VersionedAuditedModel {
@Column(length = 32, nullable = true)
private String pid;
@OneToMany(cascade={}, mappedBy="sourceRequest")
private List<MaterialSubRequest> subRequests;
@PrePersist
void prepersist() {
......@@ -112,4 +121,12 @@ public class MaterialRequest extends VersionedAuditedModel {
public String getPid() {
return pid;
}
public List<MaterialSubRequest> getSubRequests() {
return this.subRequests;
}
public void setSubRequests(List<MaterialSubRequest> subRequests) {
this.subRequests = subRequests;
}
}
......@@ -28,6 +28,9 @@ import javax.persistence.Table;
import org.apache.commons.lang.math.RandomUtils;
import org.genesys2.server.model.VersionedAuditedModel;
import org.genesys2.server.servlet.controller.rest.serialization.MaterialSubRequestSerializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
/**
* {@link MaterialRequest} is broken down into individual requests to
......@@ -38,6 +41,7 @@ import org.genesys2.server.model.VersionedAuditedModel;
*/
@Entity
@Table(name = "requestsub")
@JsonSerialize(using = MaterialSubRequestSerializer.class)
public class MaterialSubRequest extends VersionedAuditedModel {
public static final int NOTCONFIRMED = 0;
public static final int CONFIRMED = 1;
......
......@@ -20,11 +20,13 @@ import java.util.List;
import org.genesys2.server.model.genesys.MaterialRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface MaterialRequestRepository extends JpaRepository<MaterialRequest, Long> {
List<MaterialRequest> findByEmail(String email);
@Query("from MaterialRequest mr left join fetch mr.subRequests where mr.uuid= ?1")
MaterialRequest findByUuid(String uuid);
}
......@@ -17,10 +17,16 @@
package org.genesys2.server.persistence.domain;
import org.genesys2.server.model.genesys.MaterialSubRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MaterialSubRequestRepository extends JpaRepository<MaterialSubRequest, Long> {
MaterialSubRequest findByUuid(String uuid);
Page<MaterialSubRequest> findAllByInstCode(String instCode, Pageable pageable);
MaterialSubRequest findByInstCodeAndUuid(String instCode, String uuid);
}
package org.genesys2.server.service;
import java.util.Collection;
import java.util.List;
import org.genesys2.server.model.json.AccessionJson;
public interface GenesysRESTService {
AccessionJson getAccessionJSON(String instCode, long accessionId);
List<AccessionJson> getAccessionJSON(Collection<Long> accessionIds);
}
......@@ -21,7 +21,10 @@ import java.util.Set;
import org.genesys2.server.model.genesys.MaterialRequest;
import org.genesys2.server.model.genesys.MaterialSubRequest;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.TokenVerificationService.NoSuchVerificationTokenException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
public interface RequestService {
......@@ -93,4 +96,13 @@ public interface RequestService {
}
}
Page<MaterialRequest> list(Pageable pageable);
Page<MaterialSubRequest> list(FaoInstitute faoInstitute, Pageable pageable);
MaterialRequest get(String uuid);
MaterialSubRequest get(FaoInstitute institute, String uuid);
}
package org.genesys2.server.service.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionBreeding;
......@@ -10,6 +14,7 @@ import org.genesys2.server.model.json.GenesysJsonFactory;
import org.genesys2.server.service.GenesysRESTService;
import org.genesys2.server.service.GenesysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -44,4 +49,15 @@ public class GenesysRESTServiceImpl implements GenesysRESTService {
aj.setGeo(GenesysJsonFactory.from(geo));
return aj;
}
@Override
public List<AccessionJson> getAccessionJSON(Collection<Long> accessionIds) {
List<AccessionJson> list = new ArrayList<AccessionJson>();
for (Accession accession : genesysService.listAccessions(accessionIds, new PageRequest(0, Integer.MAX_VALUE))) {
list.add(GenesysJsonFactory.from(accession));
}
return list;
}
}
......@@ -50,6 +50,8 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -345,4 +347,28 @@ public class RequestServiceImpl implements RequestService {
public MaterialRequest getRequest(String uuid) {
return requestRepository.findByUuid(uuid);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
public Page<MaterialRequest> list(Pageable pageable) {
return requestRepository.findAll(pageable);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#faoInstitute, 'ADMINISTRATION')")
public Page<MaterialSubRequest> list(FaoInstitute faoInstitute, Pageable pageable) {
return subRequestRepository.findAllByInstCode(faoInstitute.getCode(), pageable);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
public MaterialRequest get(String uuid) {
return requestRepository.findByUuid(uuid);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#faoInstitute, 'ADMINISTRATION')")
public MaterialSubRequest get(FaoInstitute institute, String uuid) {
return subRequestRepository.findByInstCodeAndUuid(institute.getCode(), uuid);
}
}
......@@ -21,6 +21,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.FaoInstitute;
......@@ -254,6 +255,13 @@ public class AccessionController extends RestController {
return restService.getAccessionJSON(instCode, accessionId);
}
@RequestMapping(value = "/list", method = { RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
List<AccessionJson> get(@RequestBody Set<Long> accessionIds) {
return restService.getAccessionJSON(accessionIds);
}
private AccessionHeaderJson readAid3(JsonNode json) {
AccessionHeaderJson dataJson = new AccessionHeaderJson();
......
/**
* 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 org.genesys2.server.model.genesys.MaterialRequest;
import org.genesys2.server.model.genesys.MaterialSubRequest;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.RequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
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" })
public class RequestsController extends RestController {
private static final int PAGE_SIZE = 10;
@Autowired
private RequestService requestService;
@Autowired
private InstituteService instituteService;
/**
* List all requests
*
* @return
*/
@RequestMapping(value = "/requests", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Object listRequests(@RequestParam(value = "page", required = false, defaultValue = "0") int page) {
LOG.info("Listing requests");
Page<MaterialRequest> requests = requestService.list(new PageRequest(page, PAGE_SIZE, new Sort(Direction.DESC, "createdDate")));
return requests;
}
/**
* Get request
*
* @return
*/
@RequestMapping(value = "/requests/r/{uuid:.{36}}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Object getRequest(@PathVariable("uuid") String uuid) {
LOG.info("Loading request uuid=" + uuid);
MaterialRequest request = requestService.get(uuid);
return request;
}
/**
* List institute requests
*
* @return
*/
@RequestMapping(value = "/requests/{instCode}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Object listInstituteRequests(@PathVariable("instCode") String instCode, @RequestParam(value = "page", required = false, defaultValue = "0") int page) {
LOG.info("Listing requests for " + instCode);
FaoInstitute institute = instituteService.getInstitute(instCode);
Page<MaterialSubRequest> requests = requestService.list(institute, new PageRequest(page, PAGE_SIZE, new Sort(Direction.DESC, "createdDate")));
return requests;
}
/**
* Get institute request
*
* @return
*/
@RequestMapping(value = "/requests/{instCode}/r/{uuid:.{36}}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
Object getInstituteRequest(@PathVariable("instCode") String instCode, @PathVariable("uuid") String uuid) {
LOG.info("Loading request for " + instCode + " uuid=" + uuid);
FaoInstitute institute = instituteService.getInstitute(instCode);
MaterialSubRequest request = requestService.get(institute, uuid);
return request;
}
}
/**
* 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.serialization;
import java.io.IOException;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.model.genesys.MaterialRequest;
import org.genesys2.server.model.genesys.MaterialSubRequest;
import org.hibernate.LazyInitializationException;
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 MaterialRequestSerializer extends JsonSerializer<MaterialRequest> {
@Override
public void serialize(MaterialRequest request, JsonGenerator jgen, SerializerProvider sp) throws IOException, JsonProcessingException {
if (request == null) {
jgen.writeNull();
} else {
jgen.writeStartObject();
jgen.writeObjectField("uuid", request.getUuid());
jgen.writeObjectField("version", request.getVersion());
jgen.writeObjectField("email", request.getEmail());
jgen.writeObjectField("state", request.getState());
jgen.writeObjectField("createdDate", request.getCreatedDate());
jgen.writeObjectField("lastModifiedDate", request.getLastModifiedDate());
try {
List<MaterialSubRequest> list;
if (request.getSubRequests() != null && (list = request.getSubRequests()).size() > 0) {
jgen.writeObjectField("subrequests", list);
}
} catch (LazyInitializationException e) {
System.err.println("It died anyway");
//e.printStackTrace();
}
if (StringUtils.isNotBlank(request.getBody())) {
jgen.writeRaw(",\"body\":");
jgen.writeRaw(request.getBody());
}
jgen.writeEndObject();
}
}
}
/**
* 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.serialization;
import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.model.genesys.MaterialSubRequest;
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 MaterialSubRequestSerializer extends JsonSerializer<MaterialSubRequest> {
@Override
public void serialize(MaterialSubRequest request, JsonGenerator jgen, SerializerProvider sp) throws IOException, JsonProcessingException {
if (request == null) {
jgen.writeNull();
} else {
jgen.writeStartObject();
jgen.writeObjectField("uuid", request.getUuid());
jgen.writeObjectField("version", request.getVersion());
jgen.writeObjectField("instCode", request.getInstCode());
jgen.writeObjectField("instEmail", request.getInstEmail());
jgen.writeObjectField("state", request.getState());
jgen.writeObjectField("createdDate", request.getCreatedDate());
jgen.writeObjectField("lastModifiedDate", request.getLastModifiedDate());
if (StringUtils.isNotBlank(request.getBody())) {
jgen.writeRaw(",\"body\":");
jgen.writeRaw(request.getBody());
}
jgen.writeEndObject();
}
}
}
Supports Markdown
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