-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListDocumentsCommand.java
More file actions
163 lines (134 loc) · 5.34 KB
/
ListDocumentsCommand.java
File metadata and controls
163 lines (134 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* Copyright (c) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.gtt;
import com.google.gdata.client.Query;
import com.google.gdata.client.gtt.DocumentQuery;
import com.google.gdata.client.gtt.GttService;
import sample.util.SimpleCommandLineParser;
import com.google.gdata.data.Category;
import com.google.gdata.data.ICategory;
import com.google.gdata.data.gtt.DocumentEntry;
import com.google.gdata.data.gtt.DocumentFeed;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.URL;
/**
* Lists translation documents in the user's inbox.
*
*
*/
public class ListDocumentsCommand implements Command {
public static final ListDocumentsCommand INSTANCE
= new ListDocumentsCommand();
/**
* This is a singleton.
*/
private ListDocumentsCommand() {
}
public void execute(GttService service, String[] args)
throws IOException, ServiceException {
DocumentQuery query = createQueryFromArgs(args);
System.out.print("Fetching documents....");
System.out.flush();
DocumentFeed resultFeed = service.getFeed(query, DocumentFeed.class);
printResults(resultFeed);
}
private DocumentQuery createQueryFromArgs(String[] args) throws IOException {
SimpleCommandLineParser parser = new SimpleCommandLineParser(args);
System.out.println("You asked to list documents....");
URL feedUrl = FeedUris.getDocumentsFeedUrl();
DocumentQuery query = new DocumentQuery(feedUrl);
if (parser.containsKey("onlydeleted")) {
System.out.println("...that are deleted");
query.setOnlydeleted(true);
}
if (parser.containsKey("onlyhidden")) {
System.out.println("...that are hidden");
Query.CategoryFilter filter = new Query.CategoryFilter();
filter.addCategory(new HiddenCategory());
query.addCategoryFilter(filter);
}
if (parser.containsKey("excludehidden")) {
System.out.println("...that are not hidden");
Query.CategoryFilter filter = new Query.CategoryFilter();
filter.addExcludeCategory(new HiddenCategory());
query.addCategoryFilter(filter);
}
String sharedWithEmail = parser.getValue("sharedwith");
if (sharedWithEmail != null) {
System.out.println("...that are shared with " + sharedWithEmail);
query.setSharedWithEmailId(sharedWithEmail);
}
String startIndex = parser.getValue("start-index");
if (startIndex != null) {
System.out.println("...that start from position " + startIndex);
query.setStartIndex(Integer.parseInt(startIndex));
}
String maxResults = parser.getValue("max-results");
if (maxResults != null) {
System.out.println("...and you don't want more than " + maxResults
+ " results");
query.setMaxResults(Integer.parseInt(maxResults));
}
return query;
}
private void printResults(DocumentFeed resultFeed) {
System.out.println("...done, there are " + resultFeed.getEntries().size()
+ " documents matching the query in your inbox.\n");
int i = 1;
for (DocumentEntry entry : resultFeed.getEntries()) {
String id = entry.getId().substring(entry.getId().lastIndexOf('/') + 1);
StringBuilder categories = new StringBuilder();
for (Category category : entry.getCategories()) {
categories.append(category.getLabel())
.append(';');
}
System.out.println(String.valueOf(i++) + ") "
+ "id = " + id
+ ", title = '" + entry.getTitle().getPlainText() + "'"
+ ", % complete = '" + entry.getPercentComplete().getValue() + "'"
+ ", num source words = '" + entry.getNumberOfSourceWords()
.getValue() + "'"
+ ", categories = '" + categories.toString() + "'");
}
System.out.println();
}
/**
* Utility class for hidden category filter.
*/
public static class HiddenCategory implements ICategory {
public String getLabel() {
return com.google.gdata.data.gtt.HiddenCategory.Label.HIDDEN;
}
public String getScheme() {
return com.google.gdata.data.gtt.HiddenCategory.Scheme.LABELS;
}
public String getTerm() {
return com.google.gdata.data.gtt.HiddenCategory.Term.HIDDEN;
}
};
public String helpString() {
return "Lists translation documents in user's inbox."
+ "\n\t--onlydeleted\t; Optional param to show only deleted documents"
+ "\n\t--sharedwith <email-id>\t; Optional param to show only "
+ "documents shared with given user"
+ "\n\t--start-index <number>\t; Optional param to show only "
+ "documents starting from given index"
+ "\n\t--max-results <number>\t; Optional param to show only "
+ "given number of documents"
+ "\n\t--onlyhidden\t; Optional param to show only hidden documents"
+ "\n\t--excludehidden\t; Optional param to not show hidden documents";
}
}