Commit ebf42a71 authored by Matija Obreza's avatar Matija Obreza

FIX REST upsert handles "inTrust" property

parent 50c0b09c
......@@ -33,6 +33,7 @@ import org.genesys2.server.lucene.genesys.FaoInstituteBridge;
import org.genesys2.server.model.AclAwareModel;
import org.genesys2.server.model.BusinessModel;
import org.genesys2.server.model.EntityId;
import org.genesys2.server.servlet.controller.rest.serialization.FaoInstituteSerializer;
import org.hibernate.annotations.Index;
import org.hibernate.search.annotations.Boost;
import org.hibernate.search.annotations.ClassBridge;
......@@ -40,11 +41,14 @@ import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@Entity
@Table(name = "faoinstitute", uniqueConstraints = @UniqueConstraint(columnNames = { "code" }))
@org.hibernate.annotations.Table(appliesTo = "faoinstitute", indexes = { @Index(columnNames = { "code" }, name = "code_FAOINSTITUTE") })
@Indexed
@ClassBridge(name = "body", boost = @Boost(1.3f), impl = FaoInstituteBridge.class)
@JsonSerialize(using=FaoInstituteSerializer.class)
public class FaoInstitute extends BusinessModel implements GeoReferencedEntity, AclAwareModel, EntityId {
/**
......
......@@ -51,5 +51,8 @@ public interface FaoInstituteRepository extends JpaRepository<FaoInstitute, Long
@Query("from FaoInstitute fi where fi.pgrActivity=true")
Page<FaoInstitute> listInstitutes(Pageable pageable);
@Query("from FaoInstitute fi where fi.id in ( ?1 )")
List<FaoInstitute> findByIds(List<Long> oids, Sort sort);
}
......@@ -27,6 +27,7 @@ import org.genesys2.server.model.impl.Organization;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
/**
* @author mobreza
......@@ -61,4 +62,6 @@ public interface InstituteService {
void delete(String instCode);
List<FaoInstitute> listMyInstitutes(Sort sort);
}
......@@ -80,4 +80,5 @@ public interface UserService {
void addVettedUserRole(String uuid);
void updateRoles(User user, List<String> selectedRoles);
}
......@@ -160,6 +160,15 @@ public class BatchRESTServiceImpl implements BatchRESTService {
updated = true;
}
}
value = accnJson.get("inTrust");
if (value != null) {
Boolean inTrust = value.isNull() ? null : value.asBoolean();
if (!areEqual(inTrust, accession.getInTrust())) {
accession.setInTrust(inTrust);
updated = true;
}
}
value = accnJson.get("available");
if (value != null) {
Boolean availability = value.isNull() ? null : value.asBoolean();
......
......@@ -35,8 +35,11 @@ import org.genesys2.server.persistence.domain.FaoInstituteRepository;
import org.genesys2.server.persistence.domain.FaoInstituteSettingRepository;
import org.genesys2.server.persistence.domain.GenesysLowlevelRepository;
import org.genesys2.server.persistence.domain.OrganizationRepository;
import org.genesys2.server.security.AuthUserDetails;
import org.genesys2.server.service.AclService;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.spring.SecurityContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
......@@ -44,6 +47,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.acls.domain.BasePermission;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -74,6 +78,10 @@ public class InstituteServiceImpl implements InstituteService {
@Autowired
private OrganizationRepository organizationRepository;
@Autowired
private AclService aclService;
@Override
public Page<FaoInstitute> list(Pageable pageable) {
return instituteRepository.listInstitutes(pageable);
......@@ -189,4 +197,16 @@ public class InstituteServiceImpl implements InstituteService {
instituteRepository.delete(institute);
}
}
@Override
@PreAuthorize("isAuthenticated()")
public List<FaoInstitute> listMyInstitutes(Sort sort) {
AuthUserDetails user = SecurityContextUtil.getAuthUser();
List<Long> oids = aclService.listIdentitiesForSid(FaoInstitute.class, user, BasePermission.WRITE);
LOG.info("Got " + oids.size() + " elements for " + user.getUsername());
if (oids.size() == 0)
return null;
return instituteRepository.findByIds(oids, sort);
}
}
......@@ -29,9 +29,11 @@ import org.apache.commons.logging.LogFactory;
import org.genesys2.server.ServiceEndpoints;
import org.genesys2.server.exception.AuthorizationException;
import org.genesys2.server.model.impl.User;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.TeamService;
import org.genesys2.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
......@@ -52,6 +54,9 @@ public class UserController extends RestController {
@Autowired
protected TeamService teamService;
@Autowired
protected InstituteService instituteService;
/**
* Returns logged in user's profile
......@@ -64,6 +69,13 @@ public class UserController extends RestController {
User user = userService.getMe();
return OAuth2Cleanup.clean(user);
}
@RequestMapping(value = "/me/institutes", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
Object listInstitutes() {
return instituteService.listMyInstitutes(new Sort("code"));
}
@RequestMapping(value = "/me/teams", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
......
/**
* 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.genesys2.server.model.impl.FaoInstitute;
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 FaoInstituteSerializer extends JsonSerializer<FaoInstitute> {
@Override
public void serialize(FaoInstitute institute, JsonGenerator jgen, SerializerProvider sp) throws IOException, JsonProcessingException {
if (institute == null) {
jgen.writeNull();
} else {
jgen.writeStartObject();
jgen.writeObjectField("instCode", institute.getCode());
jgen.writeObjectField("name", institute.getFullName());
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