Commit 43c94803 authored by Matija Obreza's avatar Matija Obreza Committed by Matija Obreza
Browse files

AccessionList

parent 1dc84979
/**
* Copyright 2015 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.model.impl;
import java.util.Set;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import org.genesys2.server.model.AclAwareModel;
import org.genesys2.server.model.VersionedAuditedModel;
import org.genesys2.server.model.genesys.Accession;
@Entity
@Table(name = "accelist")
public class AccessionList extends VersionedAuditedModel implements AclAwareModel {
private static final long serialVersionUID = 991886970995006680L;
@Column(name = "uuid", unique = true, nullable = false, updatable = false)
protected UUID uuid;
@OneToMany(cascade = {}, fetch = FetchType.LAZY)
@JoinTable(name = "accelistitems")
@JoinColumn(name = "listId")
private Set<Accession> accessions;
@Column(name = "title", nullable = false)
private String title;
@Column(name = "description", nullable = true)
private String description;
public UUID getUuid() {
return uuid;
}
protected void setUuid(UUID uuid) {
this.uuid = uuid;
}
@PrePersist
protected void pre() {
if (uuid == null)
uuid = UUID.randomUUID();
}
public AccessionList() {
super();
}
public Set<Accession> getAccessions() {
return accessions;
}
public void setAccessions(Set<Accession> accessions) {
this.accessions = accessions;
}
public String getDescription() {
return description;
}
public String getTitle() {
return title;
}
public void setDescription(String description) {
this.description = description;
}
public void setTitle(String title) {
this.title = title;
}
}
\ No newline at end of file
/**
* Copyright 2015 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.model.impl;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.model.AclAwareModel;
import org.genesys2.server.model.VersionedAuditedModel;
import org.hibernate.annotations.Type;
@Entity
@Table
public class UserAccessionList extends VersionedAuditedModel implements AclAwareModel {
private static final long serialVersionUID = 1L;
@Column(name = "title", nullable = false)
private String title;
@Column(name = "description", nullable = true)
private String description;
@Lob
@Type(type = "org.hibernate.type.TextType")
@Column(name = "accessionIds", nullable = true)
private String accessionIdsStr;
@Transient
private Set<Long> accessionIds;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAccessionIdsStr() {
return accessionIdsStr;
}
public void setAccessionIdsStr(String accessionIdsStr) {
this.accessionIdsStr = accessionIdsStr;
// TODO review
if (accessionIdsStr != null) {
accessionIds = new HashSet<>();
for (String id : accessionIdsStr.split(",")) {
accessionIds.add(Long.valueOf(id));
}
}
}
public Set<Long> getAccessionIds() {
if (accessionIdsStr != null) {
accessionIds = new HashSet<>();
for (String id : accessionIdsStr.split(",")) {
accessionIds.add(Long.valueOf(id));
}
}
return accessionIds;
}
public void setAccessionIds(Set<Long> accessionIds) {
this.accessionIds = accessionIds;
if (accessionIds != null) {
this.accessionIdsStr = StringUtils.join(accessionIds, ",");
}
}
public class UserAccessionList {
public static class UserAccessionList_ implements Serializable {
private static final long serialVersionUID = -2092185410372557368L;
......@@ -89,7 +26,7 @@ public class UserAccessionList extends VersionedAuditedModel implements AclAware
public String title;
public String description;
public static UserAccessionList_ from(UserAccessionList ual) {
public static UserAccessionList_ from(AccessionList ual) {
UserAccessionList_ ul = new UserAccessionList_();
ul.id = ual.getId();
ul.title = ual.getTitle();
......
/**
* Copyright 2015 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.persistence.domain;
import java.util.List;
import java.util.Set;
import org.genesys2.server.model.impl.AccessionList;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface AccessionListRepository extends JpaRepository<AccessionList, Long> {
@Query("from AccessionList al where al.createdBy=?1")
List<AccessionList> listByOwner(Long userId);
@Query("select a.id from AccessionList al join al.accessions a where al.id=?1")
Set<Long> getAccessionIds(long listId);
}
package org.genesys2.server.persistence.domain;
import java.util.List;
import org.genesys2.server.model.impl.UserAccessionList;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface UserAccessionListRepository extends JpaRepository<UserAccessionList, Long> {
@Query("from UserAccessionList ual where ual.createdBy=?1")
List<UserAccessionList> listByOwner(Long userId);
}
/**
* Copyright 2015 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.service;
import java.util.List;
import java.util.Set;
import org.genesys2.server.model.impl.AccessionList;
public interface AccessionListService {
AccessionList getList(Long id);
void save(AccessionList accessionList);
List<AccessionList> getAll();
void delete(AccessionList accessionList);
List<AccessionList> getMyLists();
Set<Long> getAccessionIds(long listId);
}
\ No newline at end of file
package org.genesys2.server.service.impl;
import java.util.List;
import java.util.Set;
import javax.transaction.Transactional;
import org.genesys2.server.model.impl.AccessionList;
import org.genesys2.server.model.impl.User;
import org.genesys2.server.model.impl.UserAccessionList;
import org.genesys2.server.persistence.domain.UserAccessionListRepository;
import org.genesys2.server.service.UserAccessionListService;
import org.genesys2.server.persistence.domain.AccessionListRepository;
import org.genesys2.server.service.AccessionListService;
import org.genesys2.spring.SecurityContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class UserAccessionListServiceImpl implements UserAccessionListService {
@Transactional(readOnly = true)
public class AccessionListServiceImpl implements AccessionListService {
@Autowired
UserAccessionListRepository userAccessionListRepository;
AccessionListRepository accessionListRepository;
@Override
@PostAuthorize("hasRole('ADMINISTRATOR') or hasPermission(returnObject, 'READ')")
public UserAccessionList getList(Long id) {
return userAccessionListRepository.findOne(id);
public AccessionList getList(Long id) {
return accessionListRepository.findOne(id);
}
@Override
@PreAuthorize("isAuthenticated() and (#userAccessionList.id==null or hasRole('ADMINISTRATOR') or hasPermission(#userAccessionList, 'WRITE'))")
public void save(UserAccessionList userAccessionList) {
userAccessionListRepository.save(userAccessionList);
@Transactional
public void save(AccessionList accessionList) {
accessionListRepository.save(accessionList);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
public List<UserAccessionList> getAll() {
return userAccessionListRepository.findAll();
public List<AccessionList> getAll() {
return accessionListRepository.findAll();
}
@Override
@PreAuthorize("isAuthenticated()")
public List<UserAccessionList> getMyLists() {
public List<AccessionList> getMyLists() {
final User user = SecurityContextUtil.getCurrentUser();
return userAccessionListRepository.listByOwner(user.getId());
return accessionListRepository.listByOwner(user.getId());
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(returnObject, 'DELETE')")
public void delete(UserAccessionList userAccessionList) {
userAccessionListRepository.delete(userAccessionList);
@Transactional
public void delete(AccessionList accessionList) {
accessionListRepository.delete(accessionList);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(returnObject, 'DELETE')")
public Set<Long> getAccessionIds(long listId) {
return accessionListRepository.getAccessionIds(listId);
}
}
......@@ -19,6 +19,7 @@ package org.genesys2.server.servlet.controller;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
......@@ -27,11 +28,12 @@ import javax.servlet.http.HttpServletResponse;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.genesys.AccessionGeo;
import org.genesys2.server.model.impl.AccessionList;
import org.genesys2.server.model.impl.UserAccessionList;
import org.genesys2.server.service.AccessionListService;
import org.genesys2.server.service.DownloadService;
import org.genesys2.server.service.FilterConstants;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.UserAccessionListService;
import org.genesys2.server.service.impl.FilterHandler;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilter;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
......@@ -68,7 +70,7 @@ public class SelectionController extends BaseController {
private DownloadService downloadService;
@Autowired
private UserAccessionListService userAccessionListService;
private AccessionListService accessionListService;
@RequestMapping(value = "/")
public String view(ModelMap model, @RequestParam(value = "page", required = false, defaultValue = "1") int page) {
......@@ -79,7 +81,7 @@ public class SelectionController extends BaseController {
// Add userAccessionLists when authenticated
try {
if (!(SecurityContextHolder.getContext().getAuthentication() instanceof AnonymousAuthenticationToken))
model.addAttribute("userAccessionLists", userAccessionListService.getMyLists());
model.addAttribute("userAccessionLists", accessionListService.getMyLists());
} catch (Throwable e) {
_logger.error(e.getMessage(), e);
}
......@@ -92,12 +94,14 @@ public class SelectionController extends BaseController {
@RequestParam(value = "listId", defaultValue = "0") long listid) {
if (listid != 0) {
UserAccessionList userAccessionList = userAccessionListService.getList(listid);
Set<Long> accessionIds = accessionListService.getAccessionIds(listid);
selectionBean.clear();
for (Long accessionId : userAccessionList.getAccessionIds()) {
for (Long accessionId : accessionIds) {
selectionBean.add(accessionId);
}
selectionBean.setUserAccessionList(UserAccessionList.UserAccessionList_.from(userAccessionList));
// FIXME
// selectionBean.setUserAccessionList(UserAccessionList.UserAccessionList_.from(userAccessionList));
}
return "redirect:/sel/";
......@@ -278,20 +282,19 @@ public class SelectionController extends BaseController {
public String saveAccessionList(@RequestParam(value = "page", required = false, defaultValue = "1") int page, @RequestParam String title,
@RequestParam String description, ModelMap model) {
UserAccessionList userAccessionList = new UserAccessionList();
AccessionList accessionList = new AccessionList();
return saveOrUpdateList(page, title, description, model, userAccessionList);
return saveOrUpdateList(page, title, description, model, accessionList);
}
@RequestMapping(value = "/userList", params = { "update", "listId" }, method = RequestMethod.POST)
@PreAuthorize("isAuthenticated()")
public String updateAccessionList(@RequestParam(value = "listId") long listId,
@RequestParam(value = "page", required = false, defaultValue = "1") int page, @RequestParam String title, @RequestParam String description,
ModelMap model) {
public String updateAccessionList(@RequestParam(value = "listId") long listId, @RequestParam(value = "page", required = false, defaultValue = "1") int page,
@RequestParam String title, @RequestParam String description, ModelMap model) {
UserAccessionList userAccessionList = userAccessionListService.getList(listId);
AccessionList accessionList = accessionListService.getList(listId);
return saveOrUpdateList(page, title, description, model, userAccessionList);
return saveOrUpdateList(page, title, description, model, accessionList);
}
/**
......@@ -301,16 +304,17 @@ public class SelectionController extends BaseController {
* @param title
* @param description
* @param model
* @param userAccessionList
* @param accessionList
* @return
*/
private String saveOrUpdateList(int page, String title, String description, ModelMap model, UserAccessionList userAccessionList) {
userAccessionList.setDescription(description);
userAccessionList.setTitle(title);
userAccessionList.setAccessionIds(selectionBean.copy());
private String saveOrUpdateList(int page, String title, String description, ModelMap model, AccessionList accessionList) {
accessionList.setDescription(description);
accessionList.setTitle(title);
accessionList
.setAccessions(new HashSet<Accession>(genesysService.listAccessions(selectionBean.copy(), new PageRequest(0, Integer.MAX_VALUE)).getContent()));
userAccessionListService.save(userAccessionList);
selectionBean.setUserAccessionList(UserAccessionList.UserAccessionList_.from(userAccessionList));
accessionListService.save(accessionList);
selectionBean.setUserAccessionList(UserAccessionList.UserAccessionList_.from(accessionList));
model.addAttribute("resultFromSave", "user.accession.list.saved-updated");
......@@ -322,14 +326,14 @@ public class SelectionController extends BaseController {
public String deleteAccessionList(@RequestParam(value = "listId", defaultValue = "0") long listId,
@RequestParam(value = "page", required = false, defaultValue = "1") int page, Model model) {
userAccessionListService.delete(userAccessionListService.getList(listId));
accessionListService.delete(accessionListService.getList(listId));
selectionBean.setUserAccessionList(null);
model.addAttribute("resultFromSave", "user.accession.list.deleted");
model.addAttribute("pagedData", genesysService.listAccessions(selectionBean.copy(), new PageRequest(page - 1, 50, new Sort("accessionName"))));
model.addAttribute("selection", selectionBean);
model.addAttribute("userAccessionLists", userAccessionListService.getMyLists());
model.addAttribute("userAccessionLists", accessionListService.getMyLists());
return "/selection/index";
}
......
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>API Test</title>
</head>
<body>
<h1>Genesys Website API</h1>
<content tag="javascript">
<script type="text/javascript" src="https://www.genesys-pgr.org/webapi/genesys-webapi.js?client_id=Q6YS2.Khh6U8DtqjNe5fNTRAaa@www.genesys-pgr.org&client_secret=DFnHMh9TASWOlBK5SAaQmYjQuJGor7Nt"></script>
<script>
jQuery(document).ready(function() {
var filters={};
GenesysPGR.listAccessions(filters, {
success: function(accessions) {
debugger;
}
});
});
</script>
</content>
</body>
</html>
\ No newline at end of file
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