Commit 16972979 authored by Mahdi Sellami's avatar Mahdi Sellami
Browse files

Bug/Fix update idea

parent e70a8a78
/**
* This class represents an input Object for the Idea API. It contains the ID,
* the title, the description, the sub-category, the geographic-position, the problem, the target group, the contact person
* as well as the an conversation schema ID of a new Idea . This class is used by {@link IdeaResourcesource}.
*
* @author Mahdi Sellami
*/
package org.tmms.ideadb.api;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
@ApiModel(description = "Input Idea with ID Model")
public class InputIdeaWithID {
/**
* The ID of the idea.
*/
@JsonProperty
long ideaId;
/**
* The title of the idea.
*/
@JsonProperty
String title;
/**
* The description/content of the idea.
*/
@JsonProperty
String description;
/**
* The Sub-Category to which the idea belongs.
*/
@JsonProperty
String subcategory;
/**
* The keywords of the idea.
*/
@JsonProperty
List<String> keywords;
/**
* The latitude of the place related to the idea.
*/
@JsonProperty
float lat;
/**
* The longitude of the place related to the idea.
*/
@JsonProperty
float lon;
/**
* The problem that the idea is talking about/trying to solve.
*/
@JsonProperty
String problem;
/**
* The target group of the idea.
*/
@JsonProperty
String target_group;
/**
* The contact person considered with the idea.
*/
@JsonProperty
String contact_preson;
/**
* The ID of the conversation schema, from which the idea has been generated.
*/
@JsonProperty
int conversation_schema_id;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((contact_preson == null) ? 0 : contact_preson.hashCode());
result = prime * result + conversation_schema_id;
result = prime * result + ((description == null) ? 0 : description.hashCode());
result = prime * result + (int) (ideaId ^ (ideaId >>> 32));
result = prime * result + ((keywords == null) ? 0 : keywords.hashCode());
result = prime * result + Float.floatToIntBits(lat);
result = prime * result + Float.floatToIntBits(lon);
result = prime * result + ((problem == null) ? 0 : problem.hashCode());
result = prime * result + ((subcategory == null) ? 0 : subcategory.hashCode());
result = prime * result + ((target_group == null) ? 0 : target_group.hashCode());
result = prime * result + ((title == null) ? 0 : title.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
InputIdeaWithID other = (InputIdeaWithID) obj;
if (contact_preson == null) {
if (other.contact_preson != null)
return false;
} else if (!contact_preson.equals(other.contact_preson))
return false;
if (conversation_schema_id != other.conversation_schema_id)
return false;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (ideaId != other.ideaId)
return false;
if (keywords == null) {
if (other.keywords != null)
return false;
} else if (!keywords.equals(other.keywords))
return false;
if (Float.floatToIntBits(lat) != Float.floatToIntBits(other.lat))
return false;
if (Float.floatToIntBits(lon) != Float.floatToIntBits(other.lon))
return false;
if (problem == null) {
if (other.problem != null)
return false;
} else if (!problem.equals(other.problem))
return false;
if (subcategory == null) {
if (other.subcategory != null)
return false;
} else if (!subcategory.equals(other.subcategory))
return false;
if (target_group == null) {
if (other.target_group != null)
return false;
} else if (!target_group.equals(other.target_group))
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}
public long getIdeaId() {
return ideaId;
}
public void setIdeaId(long ideaId) {
this.ideaId = ideaId;
}
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 getSubcategory() {
return subcategory;
}
public void setSubcategory(String subcategory) {
this.subcategory = subcategory;
}
public List<String> getKeywords() {
return keywords;
}
public void setKeywords(List<String> keywords) {
this.keywords = keywords;
}
public float getLat() {
return lat;
}
public void setLat(float lat) {
this.lat = lat;
}
public float getLon() {
return lon;
}
public void setLon(float lon) {
this.lon = lon;
}
public String getProblem() {
return problem;
}
public void setProblem(String problem) {
this.problem = problem;
}
public String getTarget_group() {
return target_group;
}
public void setTarget_group(String target_group) {
this.target_group = target_group;
}
public String getContact_preson() {
return contact_preson;
}
public void setContact_preson(String contact_preson) {
this.contact_preson = contact_preson;
}
public int getConversation_schema_id() {
return conversation_schema_id;
}
public void setConversation_schema_id(int conversation_schema_id) {
this.conversation_schema_id = conversation_schema_id;
}
}
......@@ -16,8 +16,10 @@
package org.tmms.ideadb.resources;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.StringTokenizer;
import javax.ws.rs.Consumes;
......@@ -31,6 +33,7 @@ import javax.ws.rs.core.MediaType;
import org.tmms.ideadb.api.Idea;
import org.tmms.ideadb.api.InputIdea;
import org.tmms.ideadb.api.InputIdeaWithID;
import org.tmms.ideadb.api.KeyWord;
import org.tmms.ideadb.api.Subcategory;
import org.tmms.ideadb.api.TagMatch;
......@@ -169,15 +172,57 @@ public class IdeaResource {
/**
* This method updates an existing Idea.
*
* @param idea
* the {@link Idea} object containing the updated values
* @param input
* the {@link InputIdeaWithID} object containing the updated
* values
* @return the updated idea as an {@link Idea} Object
*/
@PUT
@Path("/update")
@UnitOfWork
@ApiOperation("Update an Idea")
public Idea updateIdea(Idea idea) {
public Idea updateIdea(InputIdeaWithID input) {
Idea idea = ideaDAO.findById(input.getIdeaId());
if (input.getTitle() != null)
idea.setTitle(input.getTitle());
if (input.getDescription() != null)
idea.setDescription(input.getDescription());
if (input.getSubcategory() != null) {
List<Subcategory> subCat = subCatDAO.findByName(input.getSubcategory());
if (subCat != null && subCat.size() > 0)
idea.setSubcategory(subCat.get(0));
}
if (input.getKeywords() != null && input.getKeywords().size() > 0) {
Set<KeyWord> newList = new HashSet<>();
for (String keyword : input.getKeywords()) {
List<KeyWord> dbKeyWord = keywordDAO.findByName(keyword);
if (dbKeyWord.size() > 0) {
newList.add(dbKeyWord.get(0));
if (idea.getSubcategory() != null) {
idea.getSubcategory().getKeyWords().add(dbKeyWord.get(0));
}
} else {
KeyWord newKeyWord = new KeyWord(keyword);
newList.add(newKeyWord);
if (idea.getSubcategory() != null) {
idea.getSubcategory().getKeyWords().add(newKeyWord);
}
}
}
idea.setKeywords(newList);
}
if (input.getLat() != 0)
idea.setLat(input.getLat());
if (input.getLon() != 0)
idea.setLon(input.getLon());
if (input.getProblem() != null)
idea.setProblem(input.getProblem());
if (input.getTarget_group() != null)
idea.setTarget_group(input.getTarget_group());
if (input.getContact_preson() != null)
idea.setContact_preson(input.getContact_preson());
if (input.getConversation_schema_id() != 0)
idea.setConversation_schema_id(input.getConversation_schema_id());
return ideaDAO.update(idea);
}
......
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