1   package com.frevvo.forms.cli.shell;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.IOException;
6   
7   import asg.cliche.Command;
8   
9   import com.frevvo.forms.cli.ApiHelper;
10  import com.frevvo.forms.cli.core.FeedShell;
11  import com.frevvo.forms.client.SchemaEntry;
12  import com.frevvo.forms.client.SchemaFeed;
13  import com.google.gdata.data.media.MediaStreamSource;
14  import com.google.gdata.util.ServiceException;
15  
16  /**
17   * Shell context for all commands related to schema feeds
18   */
19  public class SchemaFeedShell extends
20  		FeedShell<SchemaFeed, SchemaEntry, SchemaEntryShell> {
21  	public static final String PATH_ELEMENT = "schemas";
22  
23  	public SchemaFeedShell(SchemaFeed feed) {
24  		super(PATH_ELEMENT, feed, SchemaFeed.class);
25  	}
26  
27  	@Command(name = "up", description = "UPLOAD a schema (e.g. 'upload /var/contacts.xsd')")
28  	public String upload(String filePath) throws IOException, ServiceException {
29  		File f = new File(filePath);
30  		if (!f.exists())
31  			return "File " + filePath + " doesnt exist";
32  
33  		try {
34  			// upload
35  			FileInputStream fis = new FileInputStream(f);
36  			try {
37  				SchemaFeed schemas = getFeed();
38  				MediaStreamSource ms = new MediaStreamSource(fis,
39  						"application/zip");
40  				SchemaEntry schema = schemas.insert(ms);
41  
42  				go(createEntryShell(schema));
43  				return "Schema " + ApiHelper.getName(schema)
44  						+ " successfully uploaded";
45  			} finally {
46  				fis.close();
47  			}
48  		} catch (ServiceException ex) {
49  			return "Could not upload formtype "
50  					+ filePath
51  					+ ": are you trying to upload an formtype with an id that already exists";
52  		} catch (Exception ex) {
53  			return "Could not upload formtype " + filePath + ": "
54  					+ ex.getMessage();
55  		}
56  	}
57  
58  	@Override
59  	protected SchemaEntry createEntry(String name, String description)
60  			throws IOException, ServiceException {
61  		return null; // not supported
62  	}
63  
64  	@Override
65  	protected SchemaEntryShell createEntryShell(SchemaEntry entry) {
66  		return new SchemaEntryShell(entry);
67  	}
68  
69  	@Override
70  	protected String print(SchemaFeed feed) {
71  		return ApiHelper.print(feed);
72  	}
73  }