Skip to content
Snippets Groups Projects
Commit 71613f3f authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

Added editing of crop pests. Depending on SQL file ddl_035.sql

parent 1a30dc2f
No related branches found
No related tags found
No related merge requests found
Showing
with 344 additions and 5 deletions
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
package no.nibio.vips.logic.controller.servlet; package no.nibio.vips.logic.controller.servlet;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
...@@ -29,6 +30,7 @@ import javax.servlet.http.HttpServlet; ...@@ -29,6 +30,7 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import no.nibio.vips.logic.controller.session.UserBean; import no.nibio.vips.logic.controller.session.UserBean;
import no.nibio.vips.logic.entity.CropPest;
import no.nibio.vips.logic.entity.Organism; import no.nibio.vips.logic.entity.Organism;
import no.nibio.vips.logic.entity.OrganismExternalResource; import no.nibio.vips.logic.entity.OrganismExternalResource;
import no.nibio.vips.logic.entity.OrganismExternalResourcePK; import no.nibio.vips.logic.entity.OrganismExternalResourcePK;
...@@ -38,6 +40,7 @@ import no.nibio.vips.logic.util.SessionControllerGetter; ...@@ -38,6 +40,7 @@ import no.nibio.vips.logic.util.SessionControllerGetter;
import no.nibio.vips.util.ExceptionUtil; import no.nibio.vips.util.ExceptionUtil;
import no.nibio.vips.util.ServletUtil; import no.nibio.vips.util.ServletUtil;
import no.nibio.web.forms.FormField; import no.nibio.web.forms.FormField;
import no.nibio.web.forms.FormUtil;
import no.nibio.web.forms.FormValidation; import no.nibio.web.forms.FormValidation;
import no.nibio.web.forms.FormValidationException; import no.nibio.web.forms.FormValidationException;
import no.nibio.web.forms.FormValidator; import no.nibio.web.forms.FormValidator;
...@@ -62,8 +65,6 @@ public class OrganismController extends HttpServlet { ...@@ -62,8 +65,6 @@ public class OrganismController extends HttpServlet {
throws ServletException, IOException { throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8"); response.setContentType("text/html;charset=UTF-8");
String action = request.getParameter("action"); String action = request.getParameter("action");
VipsLogicUser user = (VipsLogicUser) request.getSession().getAttribute("user");
UserBean userBean = SessionControllerGetter.getUserBean();
// Default: View top organisms // Default: View top organisms
// for everyone // for everyone
...@@ -290,6 +291,62 @@ public class OrganismController extends HttpServlet { ...@@ -290,6 +291,62 @@ public class OrganismController extends HttpServlet {
} }
} }
} }
else if(action.equals("editCropPest"))
{
Integer cropOrganismId = null;
try
{
cropOrganismId = Integer.valueOf(request.getParameter("cropOrganismId"));
}
catch(NullPointerException | NumberFormatException ex){}
List<Organism> allPests = em.createNamedQuery("Organism.findAllPests").getResultList();
request.setAttribute("allPests", allPests);
List<Organism> allCrops = em.createNamedQuery("Organism.findAllCrops").getResultList();
request.setAttribute("allCrops", allCrops);
if(cropOrganismId != null && cropOrganismId > 0)
{
request.setAttribute("cropOrganismId", cropOrganismId);
CropPest cropPest = SessionControllerGetter.getOrganismBean().getCropPest(cropOrganismId);
request.setAttribute("cropPest", cropPest);
}
request.setAttribute("hierarchyCategories", SessionControllerGetter.getOrganismBean().getHierarchyCategoryNames(SessionLocaleUtil.getCurrentLocale(request)));
request.getRequestDispatcher("/cropPestForm.ftl").forward(request, response);
}
else if(action.equals("submitCropPest"))
{
try
{
FormValidation formValidation = FormValidator.validateForm("cropPestForm",request,getServletContext());
if(formValidation.isValid())
{
Integer cropOrganismId = formValidation.getFormField("cropOrganismId").getValueAsInteger();
boolean includeAllChildCrops = formValidation.getFormField("includeAllChildCrops").getWebValue() != null;
List<Integer> poTemp = FormUtil.getIdsFromMultipleSelect(formValidation.getFormField("pestOrganismIds").getWebValues());
Integer[] pestOrganismIds = poTemp.toArray(new Integer[poTemp.size()]);
CropPest cropPest = em.find(CropPest.class, cropOrganismId);
if(cropPest == null)
{
cropPest = new CropPest();
cropPest.setCropOrganismId(cropOrganismId);
}
cropPest.setIncludeAllChildCrops(includeAllChildCrops);
cropPest.setPestOrganismIds(pestOrganismIds);
SessionControllerGetter.getOrganismBean().storeCropPest(cropPest);
response.sendRedirect(new StringBuilder("http://").append(ServletUtil.getServerName(request)).append("/organism?action=editCropPest&cropOrganismId=").append(cropOrganismId).append("&messageKey=cropPestUpdated").toString());
}
}
catch(NullPointerException | NumberFormatException | FormValidationException ex)
{
ex.printStackTrace();
}
}
} }
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
......
...@@ -29,6 +29,7 @@ import javax.persistence.EntityManager; ...@@ -29,6 +29,7 @@ import javax.persistence.EntityManager;
import javax.persistence.NoResultException; import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
import javax.persistence.Query; import javax.persistence.Query;
import no.nibio.vips.logic.entity.CropPest;
import no.nibio.vips.logic.entity.ExternalResource; import no.nibio.vips.logic.entity.ExternalResource;
import no.nibio.vips.logic.entity.ExternalResourceType; import no.nibio.vips.logic.entity.ExternalResourceType;
import no.nibio.vips.logic.entity.HierarchyCategory; import no.nibio.vips.logic.entity.HierarchyCategory;
...@@ -252,4 +253,22 @@ public class OrganismBean { ...@@ -252,4 +253,22 @@ public class OrganismBean {
{ {
return em.createNamedQuery("Organism.findAllCrops").getResultList(); return em.createNamedQuery("Organism.findAllCrops").getResultList();
} }
public CropPest getCropPest(Integer cropOrganismId) {
try
{
return em.createNamedQuery("CropPest.findByCropOrganismId", CropPest.class)
.setParameter("cropOrganismId", cropOrganismId)
.getSingleResult();
}
catch(NoResultException ex)
{
return null;
}
}
public CropPest storeCropPest(CropPest cropPest)
{
return em.merge(cropPest);
}
} }
/*
* Copyright (c) 2016 NIBIO <http://www.nibio.no/>.
*
* This file is part of VIPSLogic.
* VIPSLogic is free software: you can redistribute it and/or modify
* it under the terms of the NIBIO Open Source License as published by
* NIBIO, either version 1 of the License, or (at your option) any
* later version.
*
* VIPSLogic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* NIBIO Open Source License for more details.
*
* You should have received a copy of the NIBIO Open Source License
* along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>.
*
*/
package no.nibio.vips.logic.entity;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;
import no.nibio.vips.logic.util.IntegerArrayUserType;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
/**
* @copyright 2016 <a href="http://www.nibio.no/">NIBIO</a>
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
@Entity
@Table(name = "crop_pest")
@TypeDefs( {@TypeDef( name= "IntegerArray", typeClass = IntegerArrayUserType.class)})
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "CropPest.findAll", query = "SELECT c FROM CropPest c"),
@NamedQuery(name = "CropPest.findByCropOrganismId", query = "SELECT c FROM CropPest c WHERE c.cropOrganismId = :cropOrganismId"),
@NamedQuery(name = "CropPest.findByPestOrganismIds", query = "SELECT c FROM CropPest c WHERE c.pestOrganismIds = :pestOrganismIds"),
@NamedQuery(name = "CropPest.findByIncludeAllChildCrops", query = "SELECT c FROM CropPest c WHERE c.includeAllChildCrops = :includeAllChildCrops")})
public class CropPest implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "crop_organism_id")
private Integer cropOrganismId;
@Type(type = "IntegerArray")
@Column(name = "pest_organism_ids")
private Integer[] pestOrganismIds;
@Column(name = "include_all_child_crops")
private Boolean includeAllChildCrops;
public CropPest() {
}
public CropPest(Integer cropOrganismId) {
this.cropOrganismId = cropOrganismId;
}
public Integer getCropOrganismId() {
return cropOrganismId;
}
public void setCropOrganismId(Integer cropOrganismId) {
this.cropOrganismId = cropOrganismId;
}
public Integer[] getPestOrganismIds() {
return pestOrganismIds;
}
public void setPestOrganismIds(Integer[] pestOrganismIds) {
this.pestOrganismIds = pestOrganismIds;
}
public Boolean getIncludeAllChildCrops() {
return includeAllChildCrops;
}
public void setIncludeAllChildCrops(Boolean includeAllChildCrops) {
this.includeAllChildCrops = includeAllChildCrops;
}
@Override
public int hashCode() {
int hash = 0;
hash += (cropOrganismId != null ? cropOrganismId.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof CropPest)) {
return false;
}
CropPest other = (CropPest) object;
if ((this.cropOrganismId == null && other.cropOrganismId != null) || (this.cropOrganismId != null && !this.cropOrganismId.equals(other.cropOrganismId))) {
return false;
}
return true;
}
@Override
public String toString() {
return "no.nibio.vips.logic.entity.CropPest[ cropOrganismId=" + cropOrganismId + " ]";
}
}
...@@ -24,8 +24,6 @@ import java.util.List; ...@@ -24,8 +24,6 @@ import java.util.List;
import javax.persistence.Basic; import javax.persistence.Basic;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Table; import javax.persistence.Table;
import no.nibio.vips.logic.util.IntegerArrayUserType; import no.nibio.vips.logic.util.IntegerArrayUserType;
......
...@@ -333,3 +333,7 @@ broadcastMessage=Broadcast the message ...@@ -333,3 +333,7 @@ broadcastMessage=Broadcast the message
approvesSmsBilling=Approves billing for SMS messages approvesSmsBilling=Approves billing for SMS messages
approvesSmsBillingDescription=By checking this option, you confirm that you accept the billing costs for receiving SMS messages from our subscription services. The price in Norway is 1 NOK/message. This consent can be terminated at any time, either by unchecking this checkbox or by sending VIPS STOP to the service number for your country. The number for Norway is 1963. To configure which notifications to receive and on which format, please select the notification subscriptions button at the top of this page. approvesSmsBillingDescription=By checking this option, you confirm that you accept the billing costs for receiving SMS messages from our subscription services. The price in Norway is 1 NOK/message. This consent can be terminated at any time, either by unchecking this checkbox or by sending VIPS STOP to the service number for your country. The number for Norway is 1963. To configure which notifications to receive and on which format, please select the notification subscriptions button at the top of this page.
observationNotifications=Observation notifications observationNotifications=Observation notifications
editCropPests=Edit pests for crop
includeAllChildCrops=Include all child crops
pestOrganismIds=Pests
cropPestUpdated=Crop pest was updated
...@@ -333,3 +333,7 @@ broadcastMessage=Broadcast the message ...@@ -333,3 +333,7 @@ broadcastMessage=Broadcast the message
approvesSmsBilling=Approves billing for SMS messages approvesSmsBilling=Approves billing for SMS messages
approvesSmsBillingDescription=By checking this option, you confirm that you accept the billing costs for receiving SMS messages from our subscription services. This consent can be terminated at any time, either by unchecking this checkbox or by sending VIPS STOP to your country's short number. Number for Norway is 1963. approvesSmsBillingDescription=By checking this option, you confirm that you accept the billing costs for receiving SMS messages from our subscription services. This consent can be terminated at any time, either by unchecking this checkbox or by sending VIPS STOP to your country's short number. Number for Norway is 1963.
observationNotifications=Observation notifications observationNotifications=Observation notifications
editCropPests=Edit pests for crop
includeAllChildCrops=Include all child crops
pestOrganismIds=Pests
cropPestUpdated=Crop pest was updated
...@@ -332,3 +332,7 @@ broadcastMessage=Broadcast the message ...@@ -332,3 +332,7 @@ broadcastMessage=Broadcast the message
approvesSmsBilling=Approves billing for SMS messages approvesSmsBilling=Approves billing for SMS messages
approvesSmsBillingDescription=By checking this option, you confirm that you accept the billing costs for receiving SMS messages from our subscription services. This consent can be terminated at any time, either by unchecking this checkbox or by sending VIPS STOP to your country's short number. Number for Norway is 1963. approvesSmsBillingDescription=By checking this option, you confirm that you accept the billing costs for receiving SMS messages from our subscription services. This consent can be terminated at any time, either by unchecking this checkbox or by sending VIPS STOP to your country's short number. Number for Norway is 1963.
observationNotifications=Observation notifications observationNotifications=Observation notifications
editCropPests=Edit pests for crop
includeAllChildCrops=Include all child crops
pestOrganismIds=Pests
cropPestUpdated=Crop pest was updated
...@@ -333,3 +333,7 @@ broadcastMessage=Kringkast denne meldingen ...@@ -333,3 +333,7 @@ broadcastMessage=Kringkast denne meldingen
approvesSmsBilling=Godtar betaling for SMS-meldinger approvesSmsBilling=Godtar betaling for SMS-meldinger
approvesSmsBillingDescription=Ved \u00e5 krysse av her aksepterer du \u00e5 bli belastet p\u00e5 din mobilregning n\u00e5r du mottar SMS-meldinger fra v\u00e5re meldingstjenester. Prisen i Norge er 1 NOK/melding. Denne aksepten kan endres n\u00e5r som helst ved \u00e5 fjerne avkrysningen eller ved \u00e5 sende VIPS STOP til ditt lands kortnummer. For Norge er nummeret 1963. For \u00e5 velge type meldinger og format som du vil abonnere p\u00e5, vennligst velg "Meldingsabonnement" \u00f8verst p\u00e5 siden. approvesSmsBillingDescription=Ved \u00e5 krysse av her aksepterer du \u00e5 bli belastet p\u00e5 din mobilregning n\u00e5r du mottar SMS-meldinger fra v\u00e5re meldingstjenester. Prisen i Norge er 1 NOK/melding. Denne aksepten kan endres n\u00e5r som helst ved \u00e5 fjerne avkrysningen eller ved \u00e5 sende VIPS STOP til ditt lands kortnummer. For Norge er nummeret 1963. For \u00e5 velge type meldinger og format som du vil abonnere p\u00e5, vennligst velg "Meldingsabonnement" \u00f8verst p\u00e5 siden.
observationNotifications=Observasjonsmeldinger observationNotifications=Observasjonsmeldinger
editCropPests=Rediger skadegj\u00f8rere for kultur
includeAllChildCrops=Inkluder alle underarter, sorter og varianter
pestOrganismIds=Skadegj\u00f8rere
cropPestUpdated=Skadegj\u00f8rere for kultur ble lagret
...@@ -333,3 +333,7 @@ broadcastMessage=Broadcast the message ...@@ -333,3 +333,7 @@ broadcastMessage=Broadcast the message
approvesSmsBilling=Approves billing for SMS messages approvesSmsBilling=Approves billing for SMS messages
approvesSmsBillingDescription=By checking this option, you confirm that you accept the billing costs for receiving SMS messages from our subscription services. This consent can be terminated at any time, either by unchecking this checkbox or by sending VIPS STOP to your country's short number. Number for Norway is 1963. approvesSmsBillingDescription=By checking this option, you confirm that you accept the billing costs for receiving SMS messages from our subscription services. This consent can be terminated at any time, either by unchecking this checkbox or by sending VIPS STOP to your country's short number. Number for Norway is 1963.
observationNotifications=Observation notifications observationNotifications=Observation notifications
editCropPests=Edit pests for crop
includeAllChildCrops=Include all child crops
pestOrganismIds=Pests
cropPestUpdated=Crop pest was updated
{
"_licenseNote": [
"Copyright (c) 2016 NIBIO <http://www.nibio.no/>. ",
"",
"This file is part of VIPSLogic. ",
"VIPSLogic is free software: you can redistribute it and/or modify ",
"it under the terms of the NIBIO Open Source License as published by ",
"NIBIO, either version 1 of the License, or (at your option) any ",
"later version. ",
"",
"VIPSLogic is distributed in the hope that it will be useful, ",
"but WITHOUT ANY WARRANTY; without even the implied warranty of ",
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ",
"NIBIO Open Source License for more details. ",
"",
"You should have received a copy of the NIBIO Open Source License ",
"along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. "
],
"_comment" : "Structure of the organismForm and how to validate it",
"fields": [
{
"name" : "cropOrganismId",
"dataType" : "INTEGER",
"required" : true
},
{
"name" : "pestOrganismIds",
"dataType" : "INTEGER",
"fieldType" : "SELECT_MULTIPLE",
"required" : false
},
{
"name" : "includeAllChildCrops",
"dataType" : "STRING",
"required" : false
}
]
}
<#--
Copyright (c) 2016 NIBIO <http://www.nibio.no/>.
This file is part of VIPSLogic.
VIPSLogic is free software: you can redistribute it and/or modify
it under the terms of the NIBIO Open Source License as published by
NIBIO, either version 1 of the License, or (at your option) any
later version.
VIPSLogic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
NIBIO Open Source License for more details.
You should have received a copy of the NIBIO Open Source License
along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>.
--><#include "master.ftl">
<#macro page_head>
<title>${i18nBundle.editCropPests}</title>
</#macro>
<#macro custom_css>
<link href="/css/3rdparty/chosen.min.css" rel="stylesheet" />
</#macro>
<#macro custom_js>
<script src="/js/resourcebundle.js"></script>
<script type="text/javascript" src="/js/3rdparty/chosen.jquery.min.js"></script>
<script src="/js/validateForm.js"></script>
<script type="text/javascript">
// Load main form definition (for validation)
loadFormDefinition("cropPestForm");
$(document).ready( function() {
// Activating chosen plugin
$(".chosen-select").chosen();
});
</script>
</#macro>
<#macro page_contents>
<div class="singleBlockContainer">
<h1>${i18nBundle.editCropPests}</h1>
<#assign formId = "cropPestForm">
<form id="${formId}" action="/organism?action=submitCropPest" method="POST" role="form" onsubmit="return validateForm(this);">
<div class="form-group">
<label for="cropOrganismId">${i18nBundle.cropOrganismId}</label>
<select name="cropOrganismId" class="form-control chosen-select" onchange="window.location.href='/organism?action=editCropPest&cropOrganismId='+this.options[this.options.selectedIndex].value;">
<option value="-1">${i18nBundle.pleaseSelect} ${i18nBundle.cropOrganismId?lower_case}</option>
<#list allCrops as cropOrganism>
<option value="${cropOrganism.organismId}"<#if cropOrganismId?has_content && cropOrganismId == cropOrganism.organismId> selected="selected"</#if>>${cropOrganism.latinName!""}/${cropOrganism.getLocalName(currentLocale.language)!""} (${hierarchyCategories.getName(cropOrganism.hierarchyCategoryId)})</option>
</#list>
</select>
</div>
<#if cropOrganismId?has_content>
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" name="includeAllChildCrops"<#if cropPest?has_content && cropPest.includeAllChildCrops == true> checked="checked"</#if>/>
</label>
${i18nBundle.includeAllChildCrops}
</div>
</div>
<div class="form-group">
<label for="pestOrganismIds">${i18nBundle.pestOrganismIds}</label>
<select name="pestOrganismIds" class="form-control chosen-select" multiple="multiple">
<#list allPests as pestOrganism>
<option value="${pestOrganism.organismId}"
<#if cropPest?has_content && cropPest.pestOrganismIds?has_content>
<#list cropPest.pestOrganismIds as pestOrganismId>
<#if pestOrganismId == pestOrganism.organismId>selected="selected"</#if>
</#list>
</#if>
>${pestOrganism.latinName!""}/${pestOrganism.getLocalName(currentLocale.language)!""} (${hierarchyCategories.getName(pestOrganism.hierarchyCategoryId)})</option>
</#list>
</select>
</div>
<button class="btn btn-default" type="submit">${i18nBundle.submit}</button>
</#if>
</form>
</div>
</#macro>
<@page_html/>
<#-- <#--
Copyright (c) 2014 NIBIO <http://www.nibio.no/>. Copyright (c) 2016 NIBIO <http://www.nibio.no/>.
This file is part of VIPSLogic. This file is part of VIPSLogic.
VIPSLogic is free software: you can redistribute it and/or modify VIPSLogic is free software: you can redistribute it and/or modify
...@@ -48,6 +48,10 @@ ...@@ -48,6 +48,10 @@
<#assign formId = "organismForm"> <#assign formId = "organismForm">
<form id="${formId}" role="form" action="/organism?action=organismFormSubmit" method="POST" onsubmit="return true;" onsubmit2="try{ return validateForm(this);}catch(err){alert(err);return false;}"> <form id="${formId}" role="form" action="/organism?action=organismFormSubmit" method="POST" onsubmit="return true;" onsubmit2="try{ return validateForm(this);}catch(err){alert(err);return false;}">
<input type="hidden" name="organismId" value="${organism.organismId!"-1"}"/> <input type="hidden" name="organismId" value="${organism.organismId!"-1"}"/>
<a href="/organism?action=listChildOrganisms&organismId=${organism.parentOrganismId!"null"}" class="btn btn-default" role="button">${i18nBundle.back}</a>
<#if (organism.isCrop?has_content && organism.isCrop == true)>
<a href="/organism?action=editCropPest&cropOrganismId=${organism.organismId!"null"}" class="btn btn-default" role="button">${i18nBundle.editCropPests}</a>
</#if>
<div class="form-group"> <div class="form-group">
<label for="parentOrganismId">${i18nBundle.parentOrganismId}</label> <label for="parentOrganismId">${i18nBundle.parentOrganismId}</label>
<select class="form-control" name="parentOrganismId" onblur="validateField(this);"> <select class="form-control" name="parentOrganismId" onblur="validateField(this);">
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment