Commit c64295a1 authored by Matija Obreza's avatar Matija Obreza
Browse files

Revision of UserAccessionList

parent 0f9229ad
...@@ -5,43 +5,35 @@ import java.util.Set; ...@@ -5,43 +5,35 @@ import java.util.Set;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.Lob;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Transient; import javax.persistence.Transient;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.genesys2.server.model.AclAwareModel; import org.genesys2.server.model.AclAwareModel;
import org.genesys2.server.model.VersionedAuditedModel;
import org.hibernate.annotations.Type;
@Entity @Entity
@Table(name = "user_accession_list") @Table
public class UserAccessionList implements AclAwareModel { public class UserAccessionList extends VersionedAuditedModel implements AclAwareModel {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", unique = true, nullable = false, length = 20)
private Long id;
@Column(name = "title", nullable = false) @Column(name = "title", nullable = false)
private String title; private String title;
@Column(name = "description", nullable = true) @Column(name = "description", nullable = true)
private String description; private String description;
@Lob
@Type(type = "org.hibernate.type.TextType")
@Column(name = "accessionIds", nullable = true) @Column(name = "accessionIds", nullable = true)
private String accessionIdsStr; private String accessionIdsStr;
@Transient @Transient
private Set<Long> accessionIds; private Set<Long> accessionIds;
@Override
public Long getId() {
return id;
}
public String getTitle() { public String getTitle() {
return title; return title;
} }
...@@ -90,4 +82,17 @@ public class UserAccessionList implements AclAwareModel { ...@@ -90,4 +82,17 @@ public class UserAccessionList implements AclAwareModel {
} }
} }
public static class UserAccessionList_ {
public long id;
public String title;
public String description;
public static UserAccessionList_ from(UserAccessionList ual) {
UserAccessionList_ ul = new UserAccessionList_();
ul.id = ual.getId();
ul.title = ual.getTitle();
ul.description = ual.getDescription();
return ul;
}
}
} }
...@@ -2,11 +2,7 @@ package org.genesys2.server.persistence.domain; ...@@ -2,11 +2,7 @@ package org.genesys2.server.persistence.domain;
import org.genesys2.server.model.impl.UserAccessionList; import org.genesys2.server.model.impl.UserAccessionList;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface UserAccessionListRepository extends JpaRepository<UserAccessionList, Long> { public interface UserAccessionListRepository extends JpaRepository<UserAccessionList, Long> {
@Query("select ual from UserAccessionList ual where ual.id = :id")
UserAccessionList findById(@Param("id") Long id);
} }
...@@ -12,7 +12,5 @@ public interface UserAccessionListService { ...@@ -12,7 +12,5 @@ public interface UserAccessionListService {
List<UserAccessionList> getAll(); List<UserAccessionList> getAll();
void delete(Long id); void delete(UserAccessionList userAccessionList);
void update(UserAccessionList userAccessionList);
} }
...@@ -19,7 +19,7 @@ public class UserAccessionListServiceImpl implements UserAccessionListService { ...@@ -19,7 +19,7 @@ public class UserAccessionListServiceImpl implements UserAccessionListService {
@Override @Override
public UserAccessionList getUserAccesionListById(Long id) { public UserAccessionList getUserAccesionListById(Long id) {
return userAccessionListRepository.findById(id); return userAccessionListRepository.findOne(id);
} }
@Override @Override
...@@ -33,21 +33,8 @@ public class UserAccessionListServiceImpl implements UserAccessionListService { ...@@ -33,21 +33,8 @@ public class UserAccessionListServiceImpl implements UserAccessionListService {
} }
@Override @Override
public void delete(Long id) { public void delete(UserAccessionList userAccessionList) {
userAccessionListRepository.delete(id); userAccessionListRepository.delete(userAccessionList);
} }
@Override
public void update(UserAccessionList userAccessionList) {
Long ualId = userAccessionList.getId();
UserAccessionList updateUserAccessionList = userAccessionListRepository.findById(ualId);
updateUserAccessionList.setTitle(userAccessionList.getTitle());
updateUserAccessionList.setAccessionIds(userAccessionList.getAccessionIds());
updateUserAccessionList.setAccessionIdsStr(userAccessionList.getAccessionIdsStr());
updateUserAccessionList.setDescription(userAccessionList.getDescription());
userAccessionListRepository.save(updateUserAccessionList);
}
} }
...@@ -21,6 +21,7 @@ import java.util.HashSet; ...@@ -21,6 +21,7 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.genesys2.server.model.genesys.Accession; import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.UserAccessionList.UserAccessionList_;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -31,6 +32,8 @@ public class SelectionBean implements Serializable { ...@@ -31,6 +32,8 @@ public class SelectionBean implements Serializable {
private final Set<Long> accessionIds = new HashSet<Long>(); private final Set<Long> accessionIds = new HashSet<Long>();
private UserAccessionList_ userAccessionList;
public Set<Long> getAccessionIds() { public Set<Long> getAccessionIds() {
return accessionIds; return accessionIds;
} }
...@@ -62,4 +65,12 @@ public class SelectionBean implements Serializable { ...@@ -62,4 +65,12 @@ public class SelectionBean implements Serializable {
public int size() { public int size() {
return accessionIds.size(); return accessionIds.size();
} }
public void setUserAccessionList(UserAccessionList_ userAccessionList) {
this.userAccessionList=userAccessionList;
}
public UserAccessionList_ getUserAccessionList() {
return userAccessionList;
}
} }
...@@ -41,6 +41,7 @@ import org.springframework.data.domain.PageRequest; ...@@ -41,6 +41,7 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
...@@ -68,20 +69,32 @@ public class SelectionController extends BaseController { ...@@ -68,20 +69,32 @@ public class SelectionController extends BaseController {
@Autowired @Autowired
private UserAccessionListService userAccessionListService; private UserAccessionListService userAccessionListService;
@RequestMapping("/") @RequestMapping(value = "/")
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER') or hasRole('USER')") public String view(ModelMap model, @RequestParam(value = "page", required = false, defaultValue = "1") int page) {
model.addAttribute("pagedData", genesysService.listAccessions(selectionBean.copy(), new PageRequest(page - 1, 50, new Sort("accessionName"))));
model.addAttribute("selection", selectionBean);
if (SecurityContextHolder.getContext().getAuthentication().isAuthenticated())
model.addAttribute("userAccessionLists", userAccessionListService.getAll());
return "/selection/index";
}
@RequestMapping(value = "/", params = { "listId" })
@PreAuthorize("isAuthenticated()")
public String view(ModelMap model, @RequestParam(value = "page", required = false, defaultValue = "1") int page, public String view(ModelMap model, @RequestParam(value = "page", required = false, defaultValue = "1") int page,
@RequestParam(value = "listId", defaultValue = "0") long listid) { @RequestParam(value = "listId", defaultValue = "0") long listid) {
if (listid != 0) { if (listid != 0) {
UserAccessionList userAccessionList = userAccessionListService.getUserAccesionListById(listid); UserAccessionList userAccessionList = userAccessionListService.getUserAccesionListById(listid);
selectionBean.clear(); selectionBean.clear();
for (Long accessionId : userAccessionList.getAccessionIds()) { for (Long accessionId : userAccessionList.getAccessionIds()) {
selectionBean.add(accessionId); selectionBean.add(accessionId);
} }
model.addAttribute("userAccessionList", userAccessionList); selectionBean.setUserAccessionList(UserAccessionList.UserAccessionList_.from(userAccessionList));
} }
model.addAttribute("pagedData", genesysService.listAccessions(selectionBean.copy(), new PageRequest(page - 1, 50, new Sort("accessionName")))); model.addAttribute("pagedData", genesysService.listAccessions(selectionBean.copy(), new PageRequest(page - 1, 50, new Sort("accessionName"))));
model.addAttribute("selection", selectionBean); model.addAttribute("selection", selectionBean);
model.addAttribute("userAccessionLists", userAccessionListService.getAll()); model.addAttribute("userAccessionLists", userAccessionListService.getAll());
...@@ -137,7 +150,7 @@ public class SelectionController extends BaseController { ...@@ -137,7 +150,7 @@ public class SelectionController extends BaseController {
/** /**
* Download DwCA of selected accessions * Download DwCA of selected accessions
* *
* @param model * @param model
* @param cropName * @param cropName
* @param jsonFilter * @param jsonFilter
...@@ -260,32 +273,27 @@ public class SelectionController extends BaseController { ...@@ -260,32 +273,27 @@ public class SelectionController extends BaseController {
} }
@RequestMapping(value = "/userList", params = { "save" }, method = RequestMethod.POST) @RequestMapping(value = "/userList", params = { "save" }, method = RequestMethod.POST)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER') or hasRole('USER')") @PreAuthorize("isAuthenticated()")
public String saveAccessionList(@RequestParam(value = "listId", defaultValue = "0") long listId, public String saveAccessionList(@RequestParam(value = "listId", defaultValue = "0") long listId,
@RequestParam(value = "page", required = false, defaultValue = "1") int page, @RequestParam String title, @RequestParam String description, @RequestParam(value = "page", required = false, defaultValue = "1") int page, @RequestParam String title, @RequestParam String description,
Model model) { Model model) {
UserAccessionList userAccessionList; UserAccessionList userAccessionList = null;
if (listId != 0) { if (listId != 0) {
userAccessionList = userAccessionListService.getUserAccesionListById(listId); userAccessionList = userAccessionListService.getUserAccesionListById(listId);
userAccessionList.setDescription(description); }
userAccessionList.setTitle(title); if (userAccessionList == null) {
userAccessionList.setAccessionIds(selectionBean.copy());
userAccessionListService.update(userAccessionList);
} else {
userAccessionList = new UserAccessionList(); userAccessionList = new UserAccessionList();
userAccessionList.setDescription(description);
userAccessionList.setTitle(title);
userAccessionList.setAccessionIds(selectionBean.copy());
userAccessionListService.save(userAccessionList);
} }
userAccessionList.setDescription(description);
userAccessionList.setTitle(title);
userAccessionList.setAccessionIds(selectionBean.copy());
userAccessionListService.save(userAccessionList);
model.addAttribute("resultFromSave", "user.accession.list.saved-updated"); model.addAttribute("resultFromSave", "user.accession.list.saved-updated");
model.addAttribute("pagedData", genesysService.listAccessions(selectionBean.copy(), new PageRequest(page - 1, 50, new Sort("accessionName")))); model.addAttribute("pagedData", genesysService.listAccessions(selectionBean.copy(), new PageRequest(page - 1, 50, new Sort("accessionName"))));
model.addAttribute("selection", selectionBean); model.addAttribute("selection", selectionBean);
model.addAttribute("userAccessionLists", userAccessionListService.getAll()); model.addAttribute("userAccessionLists", userAccessionListService.getAll());
...@@ -294,15 +302,14 @@ public class SelectionController extends BaseController { ...@@ -294,15 +302,14 @@ public class SelectionController extends BaseController {
} }
@RequestMapping(value = "/userList", params = { "delete" }, method = RequestMethod.POST) @RequestMapping(value = "/userList", params = { "delete" }, method = RequestMethod.POST)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER') or hasRole('USER')") @PreAuthorize("isAuthenticated()")
public String deleteAccessionList(@RequestParam(value = "listId", defaultValue = "0") long listId, public String deleteAccessionList(@RequestParam(value = "listId", defaultValue = "0") long listId,
@RequestParam(value = "page", required = false, defaultValue = "1") int page, Model model) { @RequestParam(value = "page", required = false, defaultValue = "1") int page, Model model) {
userAccessionListService.delete(listId);
model.addAttribute("resultFromSave", "user.accession.list.deleted"); userAccessionListService.delete(userAccessionListService.getUserAccesionListById(listId));
model.addAttribute("resultFromSave", "user.accession.list.deleted");
model.addAttribute("pagedData", genesysService.listAccessions(selectionBean.copy(), new PageRequest(page - 1, 50, new Sort("accessionName")))); model.addAttribute("pagedData", genesysService.listAccessions(selectionBean.copy(), new PageRequest(page - 1, 50, new Sort("accessionName"))));
model.addAttribute("selection", selectionBean); model.addAttribute("selection", selectionBean);
model.addAttribute("userAccessionLists", userAccessionListService.getAll()); model.addAttribute("userAccessionLists", userAccessionListService.getAll());
......
...@@ -99,10 +99,10 @@ ...@@ -99,10 +99,10 @@
class="form-control"><c:out value="${userAccessionList.description}"/></textarea> class="form-control"><c:out value="${userAccessionList.description}"/></textarea>
</div> </div>
<div class="form-group"> <div class="form-group">
<input type="submit" class="btn btn-primary" value="Save list" name="save"/> <input type="submit" class="btn btn-primary" value="<spring:message code="user.accession.list.save"/>" name="save"/>
<c:if test="${userAccessionList.id ne null}"> <c:if test="${userAccessionList.id ne null}">
<input type="hidden" name="listId" value="${userAccessionList.id}"/> <input type="hidden" name="listId" value="${userAccessionList.id}"/>
<input type="submit" class="btn" value="Delete" name="<spring:message code="user.accession.list.delete"/>"/> <input type="submit" class="btn" value="<spring:message code="user.accession.list.delete"/>" name="delete" />
</c:if> </c:if>
</div> </div>
<!-- CSRF protection --> <!-- CSRF protection -->
......
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