Permalink
Browse files

Add category autocomplete.

  • Loading branch information...
1 parent e580908 commit 9580115c8bcadc9355ea7dae0798a325e5390417 @mgv mgv committed Jul 17, 2011
View
21 model/src/main/java/com/tda/model/item/Category.java
@@ -1,21 +0,0 @@
-package com.tda.model.item;
-
-
-public enum Category {
- misc("Miscelánea"), medical("Medicamentos");
-
- private String description;
-
- Category(String description) {
- this.setDescription(description);
- }
-
- public void setDescription(String description) {
- this.description = description;
- }
-
- public String getDescription() {
- return description;
- }
-
-}
View
9 model/src/main/java/com/tda/model/item/Item.java
@@ -30,7 +30,7 @@
private Long quantity;
@NotNull
- private Category category;
+ private String category;
@NotNull
private MeasureUnit measureUnit;
@@ -40,7 +40,7 @@ public Item() {
}
public Item(String name, String description, Long quantity,
- Category category, MeasureUnit measureUnit) {
+ String category, MeasureUnit measureUnit) {
this.name = name;
this.description = description;
this.quantity = quantity;
@@ -85,12 +85,11 @@ public void setQuantity(Long quantity) {
this.quantity = quantity;
}
- @Enumerated
- public Category getCategory() {
+ public String getCategory() {
return category;
}
- public void setCategory(Category category) {
+ public void setCategory(String category) {
this.category = category;
}
View
4 model/src/main/java/com/tda/model/item/ItemBuilder.java
@@ -6,7 +6,7 @@
private String name;
private String description;
private Long quantity;
- private Category category;
+ private String category;
private MeasureUnit measureUnit;
private ItemBuilder() {
@@ -35,7 +35,7 @@ public ItemBuilder withQuantity(Long quantity) {
return this;
}
- public ItemBuilder withCategory(Category category) {
+ public ItemBuilder withCategory(String category) {
this.category = category;
return this;
}
View
8 presentation/src/main/java/com/tda/presentation/controller/ItemController.java
@@ -18,7 +18,6 @@
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
-import com.tda.model.item.Category;
import com.tda.model.item.Item;
import com.tda.model.item.MeasureUnit;
import com.tda.persistence.paginator.Paginator;
@@ -57,11 +56,6 @@ public void setPaginator(Paginator paginator) {
paginator.setOrderField("id");
}
- @ModelAttribute("categories")
- public Category[] populateCategories() {
- return Category.values();
- }
-
@ModelAttribute("measureUnits")
public MeasureUnit[] populateMeasureUnits() {
return MeasureUnit.values();
@@ -215,4 +209,4 @@ private ModelAndView processRequest(ModelAndView modelAndView,
return modelAndView;
}
-}
+}
View
24 presentation/src/main/java/com/tda/presentation/controller/WelcomeController.java
@@ -3,6 +3,7 @@
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
@@ -30,6 +31,8 @@
import com.google.gson.Gson;
import com.tda.model.applicationuser.ApplicationUser;
import com.tda.model.applicationuser.OnlineUser;
+import com.tda.model.item.Item;
+import com.tda.model.item.ItemBuilder;
import com.tda.model.itinerary.Itinerary;
import com.tda.model.patient.Patient;
import com.tda.model.patient.PatientInTrain;
@@ -38,6 +41,7 @@
import com.tda.persistence.dao.ItineraryDAO;
import com.tda.persistence.paginator.Paginator;
import com.tda.presentation.params.ParamContainer;
+import com.tda.service.api.ItemService;
import com.tda.service.api.OnlineUserService;
import com.tda.service.api.PatientInTrainService;
import com.tda.service.api.PatientService;
@@ -51,6 +55,7 @@
private PatientInTrainService patientInTrainService;
private PatientService patientService;
+ private ItemService itemService;
private PediatricianDiagnosisService pediatricianDiagnosisService;
private Paginator paginator;
private ParamContainer params;
@@ -342,6 +347,20 @@ String getDiagnosis(@RequestParam String q) {
return gson.toJson(allDiagnosis);
// return resultset;
}
+
+ @RequestMapping(value = "/getCategory", method = RequestMethod.GET)
+ @ResponseStatus(HttpStatus.OK)
+ public @ResponseBody String getCategory(@RequestParam String q) {
+ List<Item> items = itemService.findByExample(ItemBuilder.createItem().withCategory(q.toLowerCase()).build());
+
+ List<String> categories = new ArrayList<String>();
+ for (Item item : items) {
+ if (! categories.contains(item.getCategory())) {
+ categories.add(item.getCategory());
+ }
+ }
+ return new Gson().toJson(categories);
+ }
@Autowired
public void setItineraryDAO(ItineraryDAO itineraryDAO) {
@@ -369,6 +388,11 @@ public void setOnlineUserService(OnlineUserService onlineUserService) {
public void setPatientService(PatientService patientService) {
this.patientService = patientService;
}
+
+ @Autowired
+ public void setItemService(ItemService itemService) {
+ this.itemService = itemService;
+ }
@Autowired
public void setPaginator(Paginator paginator) {
View
28 presentation/src/main/webapp/WEB-INF/views/item/createForm.jsp
@@ -5,6 +5,28 @@
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
+<script type="text/javascript">
+$(document).ready(function(){
+ $("#category-input").autocomplete({
+ source: function(request, response) {
+ $.get(contextPath + "/getCategory?q="+request.term, function(data) {
+ var data = eval(data);
+
+ var availableTags = $.map( data, function( item ) {
+ return item;
+ //label: item.name,
+ //value: item.name
+ });
+
+ // delegate back to autocomplete, but extract the last term
+ response( availableTags );
+ })
+ },
+ minLength: 2
+ });
+});
+</script>
+
<form:form modelAttribute="item"
action="add" method="post">
<fieldset>
@@ -23,11 +45,7 @@
<form:input path="quantity" /> <form:errors path="quantity" /></p>
<p><form:label for="category" path="category" cssErrorClass="error"><fmt:message key="item.form.category" /></form:label><br />
- <form:select path="category">
- <c:forEach var="category" items="${categories}">
- <form:option value="${category}"> ${category.description} </form:option>
- </c:forEach>
- </form:select><form:errors path="category" /></p>
+ <form:input id="category-input" path="category" /> <form:errors path="category" /></p>
<p><form:label for="measureUnit" path="measureUnit"
cssErrorClass="error"><fmt:message key="item.form.measureUnit" /></form:label><br />
View
7 presentation/src/main/webapp/WEB-INF/views/item/filter.jsp
@@ -26,12 +26,7 @@
<tr>
<td><form:label for="category" path="category" cssErrorClass="error">
<fmt:message key="item.form.category" /></form:label></td>
- <td><form:select path="category">
- <form:option value=""></form:option>
- <c:forEach var="category" items="${categories}">
- <form:option value="${category}"> ${category.description} </form:option>
- </c:forEach>
- </form:select><form:errors path="category" /></td></tr>
+ <td><form:input path="category" /><form:errors path="category" /></td></tr>
<tr>
<td><form:label for="measureUnit" path="measureUnit"
cssErrorClass="error"><fmt:message key="item.form.measureUnit" /></form:label></td>
View
2 presentation/src/main/webapp/WEB-INF/views/item/list.jsp
@@ -120,7 +120,7 @@
<td>${item.name}</td>
<td>${item.description}</td>
<td><div class='editable' id='${item.id }'>${item.quantity}</div></td>
- <td>${item.category.description}</td>
+ <td>${item.category}</td>
<td>${item.measureUnit.description}</td>
<td><form:form method="GET"
action="${editUrl}/${item.id}">
View
3 service/src/main/java/com/tda/service/api/ItemService.java
@@ -2,7 +2,6 @@
import java.util.List;
-import com.tda.model.item.Category;
import com.tda.model.item.Item;
import com.tda.model.item.MeasureUnit;
import com.tda.persistence.paginator.Paginator;
@@ -37,8 +36,6 @@
List<Item> findByDescriptionContaining(String description);
- List<Item> findByCategory(Category category);
-
List<Item> findByMeasureUnit(MeasureUnit measureUnit);
}
View
6 service/src/main/java/com/tda/service/impl/ItemServiceImpl.java
@@ -4,7 +4,6 @@
import org.springframework.transaction.annotation.Transactional;
-import com.tda.model.item.Category;
import com.tda.model.item.Item;
import com.tda.model.item.ItemBuilder;
import com.tda.model.item.MeasureUnit;
@@ -76,11 +75,6 @@ public Item findById(Long id) {
return itemDAO.findByExample(example);
}
- public List<Item> findByCategory(Category category) {
- Item example = ItemBuilder.createItem().withCategory(category).build();
- return itemDAO.findByExample(example);
- }
-
public List<Item> findByMeasureUnit(MeasureUnit measureUnit) {
Item example = ItemBuilder.createItem().withMeasureUnit(measureUnit)
.build();

0 comments on commit 9580115

Please sign in to comment.