1 package com.frevvo.forms.cli.shell;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7
8 import asg.cliche.Command;
9
10 import com.frevvo.forms.cli.ApiHelper;
11 import com.frevvo.forms.cli.core.FeedShell;
12 import com.frevvo.forms.client.ApplicationEntry;
13 import com.frevvo.forms.client.ApplicationFeed;
14 import com.frevvo.forms.client.Helper;
15 import com.google.gdata.data.MediaContent;
16 import com.google.gdata.data.media.MediaSource;
17 import com.google.gdata.data.media.MediaStreamSource;
18 import com.google.gdata.util.ServiceException;
19
20
21
22
23 public class ApplicationFeedShell extends
24 FeedShell<ApplicationFeed, ApplicationEntry, ApplicationEntryShell> {
25 public static final String PATH_ELEMENT = "apps";
26
27 public ApplicationFeedShell(ApplicationFeed feed) {
28 super(PATH_ELEMENT, feed, ApplicationFeed.class);
29 }
30
31 @Command(name = "down", description = "DOWNLOAD ALL applications into temp folder (e.g.: 'download')")
32 public String downloadAll() throws IOException, ServiceException {
33 ApplicationFeed apps = getFeed();
34
35 StringBuilder sb = new StringBuilder();
36 for (ApplicationEntry app : apps.getEntries()) {
37 String name = ApiHelper.getName(app);
38 String fileName = ApiHelper.nameToFileName(name);
39 try {
40 File file = File.createTempFile(fileName + "_", "_app.zip");
41 FileOutputStream fos = new FileOutputStream(file);
42 try {
43 MediaContent mc = (MediaContent) app.getContent();
44 MediaSource ms = getService().getMedia(mc);
45
46 Helper.readStream(ms.getInputStream(), fos);
47
48 sb.append("\tDownloaded application " + name + ": " + file
49 + "\n");
50 } catch (Exception e) {
51 sb.append("\tCould not download application " + fileName
52 + ": " + e.getMessage() + "\n");
53 } finally {
54 fos.close();
55 }
56 } catch (Exception e) {
57 sb.append("\tCould not download application " + fileName + ": "
58 + e.getMessage() + "\n");
59 }
60 }
61 return "Downloaded " + apps.getEntries().size() + " application(s):\n"
62 + sb.toString();
63 }
64
65 @Command(name = "up", description = "UPLOAD an application (e.g. 'up /var/contacts_apps.zip')")
66 public String upload(String filePath) throws IOException, ServiceException {
67 File f = new File(filePath);
68 if (!f.exists())
69 return "File " + filePath + " doesnt exist";
70
71 ApplicationFeed apps = getFeed();
72
73 try {
74
75 FileInputStream fis = new FileInputStream(f);
76 try {
77 MediaStreamSource ms = new MediaStreamSource(fis,
78 "application/zip");
79 ApplicationEntry entry = apps.insert(ms);
80
81 go(createEntryShell(entry));
82 return "Application " + ApiHelper.getName(entry)
83 + " successfully uploaded";
84 } finally {
85 fis.close();
86 }
87 } catch (ServiceException ex) {
88 return "Could not upload application "
89 + filePath
90 + ": are you trying to upload an app with an id that already exists";
91 } catch (Exception ex) {
92 return "Could not upload application " + filePath + ": "
93 + ex.getMessage();
94 }
95 }
96
97 @Override
98 protected ApplicationEntryShell createEntryShell(ApplicationEntry entry) {
99 return new ApplicationEntryShell(entry);
100 }
101
102 @Override
103 protected ApplicationEntry createEntry(String name, String description)
104 throws ServiceException, IOException {
105 return ApiHelper.createApplicationEntry(getService(), getFeed(), name,
106 description);
107 }
108
109 @Override
110 protected String print(ApplicationFeed feed) {
111 return ApiHelper.print(feed);
112 }
113 }