1   package com.frevvo.forms.cli.shell;
2   
3   import java.io.IOException;
4   import java.net.MalformedURLException;
5   
6   import asg.cliche.Command;
7   
8   import com.frevvo.forms.cli.ApiHelper;
9   import com.frevvo.forms.cli.core.FeedShell;
10  import com.frevvo.forms.client.ApplicationEntry;
11  import com.frevvo.forms.client.DocumentTypeEntry;
12  import com.frevvo.forms.client.DocumentTypeFeed;
13  import com.frevvo.forms.client.FormTypeEntry;
14  import com.frevvo.forms.client.SchemaEntry;
15  import com.frevvo.forms.client.SchemaFeed;
16  import com.google.gdata.util.ServiceException;
17  
18  /**
19   * Shell context for all commands related to document feeds
20   */
21  public class DocumentTypeFeedShell extends
22  		FeedShell<DocumentTypeFeed, DocumentTypeEntry, DocumentTypeEntryShell> {
23  	public static final String PATH_ELEMENT = "docs";
24  
25  	private FormTypeEntry form;
26  	
27  	public DocumentTypeFeedShell(FormTypeEntry form, DocumentTypeFeed feed) {
28  		super(PATH_ELEMENT, feed, DocumentTypeFeed.class);
29  		this.form = form;
30  	}
31  
32  	public FormTypeEntry getFormTypeEntry() {
33  		return form;
34  	}
35  	
36  	
37  	@Command(name = "add", description = "LIST all forms (e.g. 'add {elementName} {namespace}')")
38  	public String addDocType(String element, String namespace) throws IOException, ServiceException {
39  		if( element == null || element.length() == 0 )
40  			return "Missing element name.";
41  		
42  		ApplicationEntry app = form.getOnwerEntry();
43  		SchemaFeed schemas = app.getSchemaFeed();
44  	
45  		// find the DocumenType in one of the schemas
46  		DocumentTypeEntry docType = findSchemaElement(schemas, element, namespace);
47  		if( docType == null )
48  			return "Could not find schema element identified by " + namespace + "#" + element;
49  		
50  		// add the DocumentType to the form as a new data source
51  		DocumentTypeFeed docTypes = getFeed();
52  		
53  		docType = docTypes.insert(docType);
54  		
55  		return "DocumentType (" + namespace + "#" + element + ") added to form " + form.getTitle().getPlainText();
56  	}
57  
58  	private DocumentTypeEntry findSchemaElement(SchemaFeed schemas, String element, String namespace) throws MalformedURLException, IOException, ServiceException {
59  		String docRef = (namespace != null?namespace+"#":"")+ element;
60  		for(SchemaEntry schema : schemas.getEntries()){
61  			DocumentTypeFeed elems = schema.getDocumentTypeFeed();
62  			for(DocumentTypeEntry docType : elems.getEntries() ){
63  				if( docRef.equals(docType.getTitle().getPlainText()) )
64  					return docType;
65  			}
66  		}
67  		return null;
68  	}
69  
70  	@Override
71  	protected DocumentTypeEntryShell createEntryShell(DocumentTypeEntry entry) {
72  		return new DocumentTypeEntryShell(entry);
73  	}
74  
75  	@Override
76  	protected DocumentTypeEntry createEntry(String name, String description)
77  			throws IOException, ServiceException {
78  		return null; // not supported
79  	}
80  
81  	@Override
82  	protected String print(DocumentTypeFeed feed) {
83  		return ApiHelper.print(feed);
84  	}
85  }