001: /*
002: * MyGWT Widget Library
003: * Copyright(c) 2007, MyGWT.
004: * licensing@mygwt.net
005: *
006: * http://mygwt.net/license
007: */
008: package net.mygwt.samples.data.server;
009:
010: import java.net.URL;
011: import java.text.SimpleDateFormat;
012: import java.util.ArrayList;
013: import java.util.Collections;
014: import java.util.Comparator;
015: import java.util.Iterator;
016: import java.util.List;
017:
018: import net.mygwt.samples.data.client.Post;
019: import net.mygwt.samples.data.client.SampleService;
020: import net.mygwt.ui.client.Style;
021: import net.mygwt.ui.client.data.DataList;
022: import net.mygwt.ui.client.data.LoadConfig;
023: import net.mygwt.ui.client.data.LoadResult;
024:
025: import org.dom4j.Document;
026: import org.dom4j.Element;
027: import org.dom4j.io.SAXReader;
028:
029: import com.google.gwt.user.server.rpc.RemoteServiceServlet;
030:
031: public class SampleServiceImpl extends RemoteServiceServlet implements
032: SampleService {
033:
034: private List posts;
035: private int id = 0;
036:
037: public LoadResult getPosts(LoadConfig config) {
038: if (posts == null) {
039: loadPosts();
040: }
041:
042: if (config.sortField != null) {
043: final String sortField = config.sortField;
044: Collections.sort(posts, new Comparator() {
045: public int compare(Object o1, Object o2) {
046: Post p1 = (Post) o1;
047: Post p2 = (Post) o2;
048: if (sortField.equals("forum")) {
049: return p1.getForum().compareTo(p2.getForum());
050: } else if (sortField.equals("username")) {
051: return p1.getUsername().compareTo(
052: p2.getUsername());
053: } else if (sortField.equals("subject")) {
054: return p1.getSubject().compareTo(
055: p2.getSubject());
056: } else if (sortField.equals("date")) {
057: return p1.getDate().compareTo(p2.getDate());
058: }
059: return 0;
060: }
061:
062: });
063:
064: if (config.sortDir == Style.DESC) {
065: Collections.reverse(posts);
066: }
067: }
068:
069: DataList sublist = new DataList();
070: int start = config.start;
071: int limit = posts.size();
072: if (config.limit > 0) {
073: limit = Math.min(start + config.limit, limit);
074: }
075: for (int i = config.start; i < limit; i++) {
076: sublist.add(posts.get(i));
077: }
078: LoadResult result = new LoadResult(sublist);
079: result.cursor = start;
080: result.totalLength = posts.size();
081: return result;
082: }
083:
084: private String getValue(Element elem, int index) {
085: Element f = (Element) elem.elements().get(index);
086: return f.getStringValue();
087: }
088:
089: private void loadPosts() {
090: posts = new ArrayList();
091: SimpleDateFormat sf = new SimpleDateFormat(
092: "yyyy-MM-dd HH:mm:ss");
093: try {
094: URL url = getClass().getResource("posts.xml");
095: SAXReader reader = new SAXReader();
096: Document document = reader.read(url);
097:
098: Element root = document.getRootElement();
099:
100: // iterate through child elements of root
101: for (Iterator i = root.elementIterator(); i.hasNext();) {
102: Element elem = (Element) i.next();
103: Post post = new Post();
104: post.setId("" + id++);
105: post.setForum(getValue(elem, 0));
106: post.setDate(sf.parse(getValue(elem, 1)));
107: post.setSubject(getValue(elem, 2));
108: post.setUsername(getValue(elem, 4));
109: posts.add(post);
110: }
111:
112: } catch (Exception e) {
113: e.printStackTrace();
114: }
115: }
116:
117: }
|