1   package com.frevvo.forms.cli.core;
2   
3   import java.io.IOException;
4   import java.util.ArrayList;
5   import java.util.List;
6   
7   import asg.cliche.Command;
8   
9   import com.frevvo.forms.cli.ApiHelper;
10  import com.frevvo.forms.client.FormsService;
11  import com.google.gdata.data.BaseEntry;
12  import com.google.gdata.data.BaseFeed;
13  import com.google.gdata.data.Link;
14  import com.google.gdata.util.ServiceException;
15  
16  public abstract class FeedShell<F extends BaseFeed<F, E>, E extends BaseEntry<E>, S extends EntryShell<E>>
17  		extends BaseShell {
18  	public static final int DEFAULT_MAX_RESULTS = 20;
19  
20  	private F feed;
21  	private Class<F> feedClass;
22  
23  	public FeedShell(String pathElement, F feed, Class<F> feedCls) {
24  		super(pathElement);
25  		this.feed = feed;
26  		this.feedClass = feedCls;
27  	}
28  
29  	@Override
30  	@Command(description = "PRINT current feed (e.g.: 'print')")
31  	public final String print() {
32  		return print(getFeed());
33  	}
34  
35  	@Command(description = "REFRESH current feed (e.g.: 'refresh')")
36  	public final String refresh() {
37  		try {
38  			F feed = getFeed();
39  			setFeed(feed.getSelf());
40  			return "\nFeed refreshed...\n" + print();
41  		} catch (Exception e) {
42  			return "Could not refresh feed: " + e.getMessage();
43  		}
44  	}
45  
46  	@Command(description = "CREATE a new entry (e.g.: 'create {name}')")
47  	public String create(String name) {
48  		try {
49  			E entry = createEntry(name, null);
50  			if (entry == null)
51  				return "Create entry not supported for this feed";
52  
53  			return go(createEntryShell(entry));
54  		} catch (Exception e) {
55  			return "Could not create new application : " + e.getMessage();
56  		}
57  	}
58  
59  	@Command(description = "SELECT an entry (e.g.: 'entry {entry#}')")
60  	public String select(int i) throws IOException, ServiceException {
61  		// find entry in feed by index
62  		F feed = getFeed();
63  		E entry = ApiHelper.findByIndex(feed, i);
64  		if (entry == null)
65  			return "Could not find entry with index " + i;
66  
67  		return go(createEntryShell(entry));
68  	}
69  
70  	@Command(description = "DELETE an entry (e.g. 'delete {entry#}')")
71  	public String delete(int i) {
72  		F feed = getFeed();
73  		E entry = ApiHelper.findByIndex(feed, i);
74  		if (entry == null)
75  			return "Could not find entry with index " + i;
76  
77  		try {
78  			entry.delete();
79  
80  			return "Entry " + ApiHelper.getName(entry) + " deleted";
81  		} catch (IOException e) {
82  			return "Could not delete " + ApiHelper.getName(entry) + ": "
83  					+ e.getMessage();
84  		} catch (Exception e) {
85  			return "Could not delete " + ApiHelper.getName(entry) + ": "
86  					+ e.getMessage();
87  		}
88  	}
89  
90  	@Command(description = "SHOW all feed links (e.g. 'links')")
91  	public String links() throws IOException {
92  		return ApiHelper.print(getFeed().getLinks());
93  	}
94  
95  	@Command(description = "SHOW feed links for given rel attr (e.g. 'links {relAttr}')")
96  	public String links(String rel) throws IOException {
97  		F f = getFeed();
98  		return ApiHelper.print(f.getLinks(rel, null));
99  	}
100 
101 	@Command(description = "OPEN a link in the browser (e.g. 'openLink {link#}')")
102 	public String link(int i) {
103 		if (i > getFeed().getLinks().size() - 1)
104 			return "Invalid link index " + i + ". Use index from 0 to "
105 					+ (getFeed().getLinks().size() - 1);
106 
107 		Link l = openLink(getFeed().getLinks().get(i));
108 
109 		return "Openning link: " + l.getHref();
110 	}
111 
112 	@Command(description = "NEXT feed page (e.g. 'next')")
113 	public String next() {
114 		F feed;
115 		try {
116 			feed = ApiHelper.next(getService(), getFeed(), getFeedClass());
117 			if (feed == null)
118 				return "This is already the last page";
119 			setFeed(feed);
120 			return print(feed);
121 		} catch (Exception e) {
122 			return "Could not get the next feed page: " + e.getMessage();
123 		}
124 	}
125 
126 	@Command(description = "PREVIOUS feed page (e.g. 'previous')")
127 	public String prev() {
128 		F feed;
129 		try {
130 			feed = ApiHelper.previous(getService(), getFeed(), getFeedClass());
131 			if (feed == null)
132 				return "This is already the first page";
133 			setFeed(feed);
134 			return print(feed);
135 		} catch (Exception e) {
136 			return "Could not get the next feed page: " + e.getMessage();
137 		}
138 	}
139 
140 	protected F getFeed() {
141 		return feed;
142 	}
143 
144 	protected Class<F> getFeedClass() {
145 		return feedClass;
146 	}
147 
148 	protected F setFeed(F feed) {
149 		this.feed = feed;
150 
151 		int index = cliGetShell().getPath().size() - 1;
152 		cliGetShell().getPath().set(index, getPathElement());
153 
154 		return feed;
155 	}
156 
157 	@Override
158 	protected List<String> getPathDetails() {
159 		List<String> details = new ArrayList<String>();
160 		// details.add("page=" + ApiHelper.getCurrentPage(getFeed()));
161 		// if (getQuery().getStartIndex() > 0)
162 		// details.add("start=" + getQuery().getStartIndex());
163 		// if (getQuery().getMaxResults() > 0)
164 		// details.add("max=" + getQuery().getMaxResults());
165 		// if (getQuery().getPublishedMin() != null)
166 		// details.add("publishedMin=" + getQuery().getPublishedMin());
167 		// if (getQuery().getPublishedMax() != null)
168 		// details.add("publishedMax=" + getQuery().getPublishedMax());
169 		// if (getQuery().getUpdatedMin() != null)
170 		// details.add("updatedMin=" + getQuery().getUpdatedMin());
171 		// if (getQuery().getUpdatedMax() != null)
172 		// details.add("updatedMax=" + getQuery().getUpdatedMax());
173 		// if (getQuery().getAuthor() != null)
174 		// details.add("author=" + getQuery().getAuthor());
175 		return details;
176 	}
177 
178 	protected FormsService getService() {
179 		return (FormsService) getFeed().getService();
180 	}
181 
182 	protected abstract String print(F feed);
183 
184 	protected abstract E createEntry(String name, String description)
185 			throws IOException, ServiceException;
186 
187 	protected abstract S createEntryShell(E entry);
188 }