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.FormTypeEntry;
13  import com.frevvo.forms.client.FormTypeFeed;
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   * Shell context for all commands related to form feeds
22   */
23  public class FormTypeFeedShell extends
24  		FeedShell<FormTypeFeed, FormTypeEntry, FormTypeEntryShell> {
25  	public static final String PATH_ELEMENT = "forms";
26  
27  	public FormTypeFeedShell(FormTypeFeed feed) {
28  		super(PATH_ELEMENT, feed, FormTypeFeed.class);
29  	}
30  
31  	@Command(name = "down", description = "DOWNLOAD ALL forms into temp folder (e.g.: 'down')")
32  	public String download() throws IOException, ServiceException {
33  
34  		FormTypeFeed forms = getFeed();
35  		StringBuilder sb = new StringBuilder();
36  		for (FormTypeEntry form : forms.getEntries()) {
37  			String name = ApiHelper.getName(form);
38  			String fileName = ApiHelper.nameToFileName(name);
39  			try {
40  				File file = File.createTempFile(fileName + "_", "_form.zip");
41  				FileOutputStream fos = new FileOutputStream(file);
42  				try {
43  					MediaContent mc = (MediaContent) form.getContent();
44  					MediaSource ms = getService().getMedia(mc);
45  
46  					Helper.readStream(ms.getInputStream(), fos);
47  
48  					sb.append("\tDownloaded formtype " + fileName + " : "
49  							+ file + "\n");
50  				} catch (Exception e) {
51  					sb.append("\tCould not download formtype " + fileName
52  							+ ": " + e.getMessage() + "\n");
53  				} finally {
54  					fos.close();
55  				}
56  			} catch (Exception e) {
57  				sb.append("\tCould not download formtype " + fileName + ": "
58  						+ e.getMessage() + "\n");
59  			}
60  		}
61  		return "Downloaded " + forms.getEntries().size() + " formtypes(s):\n"
62  				+ sb.toString();
63  	}
64  
65  	@Command(name = "up", description = "UPLOAD an application (e.g. 'upload /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  		try {
72  			// upload
73  			FileInputStream fis = new FileInputStream(f);
74  			try {
75  				FormTypeFeed forms = getFeed();
76  				MediaStreamSource ms = new MediaStreamSource(fis,
77  						"application/zip");
78  				FormTypeEntry form = forms.insert(ms);
79  
80  				go(createEntryShell(form));
81  				return "FormType " + ApiHelper.getName(form)
82  						+ " successfully uploaded";
83  			} finally {
84  				fis.close();
85  			}
86  		} catch (ServiceException ex) {
87  			return "Could not upload formtype "
88  					+ filePath
89  					+ ": are you trying to upload an formtype with an id that already exists";
90  		} catch (Exception ex) {
91  			return "Could not upload formtype " + filePath + ": "
92  					+ ex.getMessage();
93  		}
94  	}
95  
96  	@Override
97  	protected FormTypeEntry createEntry(String name, String description)
98  			throws IOException, ServiceException {
99  		return ApiHelper.createFormTypeEntry(getService(), getFeed(), name,
100 				description);
101 	}
102 
103 	@Override
104 	protected FormTypeEntryShell createEntryShell(FormTypeEntry entry) {
105 		return new FormTypeEntryShell(entry);
106 	}
107 
108 	@Override
109 	protected String print(FormTypeFeed feed) {
110 		return ApiHelper.print(feed);
111 	}
112 }