package no.dusken.aranea.admin.control;
import no.dusken.aranea.model.Tag;
import no.dusken.aranea.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.LinkedList;
import java.util.List;
/**
* @author Marvin B. Lillehaug <lillehau@underdusken.no>
*/
@Controller
public class TagController {
private TagService tagService;
@RequestMapping("/blank/tags/list")
public @ResponseBody List<JQueryJSONMapper> listTagsJson(@RequestParam String term){
return wrapTagInJQueryJSONMapper(tagService.getTags());
}
@RequestMapping("/blank/tags/set")
public @ResponseBody List<JQueryJSONMapper> setTagsJson(@RequestParam String term) {
List<Tag> tlist = new LinkedList<Tag>();
if (StringUtils.hasLength(term)) {
Tag t = new Tag();
t.setName(term);
tlist.add(tagService.saveOrUpdate(t));
}
return wrapTagInJQueryJSONMapper(tlist);
}
@Autowired
public void setTagService(TagService tagService) {
this.tagService = tagService;
}
private List<JQueryJSONMapper> wrapTagInJQueryJSONMapper(List<Tag> tags){
List<JQueryJSONMapper> list = new LinkedList<JQueryJSONMapper>();
for(Tag tag : tags){
list.add(new JQueryJSONMapper(tag.getName(), tag.getID().toString()));
}
return list;
}
private class JQueryJSONMapper{
public String label;
public String value;
public JQueryJSONMapper(String label, String value){
this.label = label;
this.value = value;
}
}
}