This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
GetDocumentListHandler.java
139 lines (119 loc) · 5.26 KB
/
GetDocumentListHandler.java
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
package org.zanata.webtrans.server.rpc;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.customware.gwt.dispatch.server.ExecutionContext;
import net.customware.gwt.dispatch.shared.ActionException;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.zanata.common.ProjectType;
import org.zanata.dao.DocumentDAO;
import org.zanata.file.FilePersistService;
import org.zanata.file.GlobalDocumentId;
import org.zanata.model.HDocument;
import org.zanata.model.HPerson;
import org.zanata.model.HProjectIteration;
import org.zanata.security.ZanataIdentity;
import org.zanata.service.TranslationFileService;
import org.zanata.util.UrlUtil;
import org.zanata.webtrans.server.ActionHandlerFor;
import org.zanata.webtrans.shared.model.AuditInfo;
import org.zanata.webtrans.shared.model.DocumentId;
import org.zanata.webtrans.shared.model.DocumentInfo;
import org.zanata.webtrans.shared.model.ProjectIterationId;
import org.zanata.webtrans.shared.rpc.GetDocumentList;
import org.zanata.webtrans.shared.rpc.GetDocumentListResult;
@Name("webtrans.gwt.GetDocsListHandler")
@Scope(ScopeType.STATELESS)
@ActionHandlerFor(GetDocumentList.class)
public class GetDocumentListHandler extends
AbstractActionHandler<GetDocumentList, GetDocumentListResult> {
@In
private ZanataIdentity identity;
@In
private DocumentDAO documentDAO;
@In
private TranslationFileService translationFileServiceImpl;
@In("filePersistService")
private FilePersistService filePersistService;
@Override
public GetDocumentListResult execute(GetDocumentList action,
ExecutionContext context) throws ActionException {
identity.checkLoggedIn();
ProjectIterationId iterationId =
action.getWorkspaceId().getProjectIterationId();
List<DocumentInfo> docs = new ArrayList<DocumentInfo>();
List<HDocument> hDocs = getDocumentList(action);
for (HDocument hDoc : hDocs) {
HPerson person = hDoc.getLastModifiedBy();
String lastModifiedBy = "";
if (person != null) {
lastModifiedBy = person.getAccount().getUsername();
}
Map<String, String> downloadExtensions =
new HashMap<String, String>();
HProjectIteration projectIteration = hDoc.getProjectIteration();
ProjectType type = projectIteration.getProjectType();
if (type == null) {
type = projectIteration.getProject().getDefaultProjectType();
}
String encodedDocId = UrlUtil.encodeString(hDoc.getDocId());
if (type == null) {
// no .po download link
} else if (type == ProjectType.Gettext || type == ProjectType.Podir) {
downloadExtensions.put(".po", "po?docId=" + encodedDocId);
} else {
downloadExtensions.put("offline .po",
"offlinepo?docId=" + encodedDocId);
}
GlobalDocumentId id =
new GlobalDocumentId(iterationId.getProjectSlug(),
iterationId.getIterationSlug(), hDoc.getDocId());
if (filePersistService.hasPersistedDocument(id)) {
String extension =
"."
+ translationFileServiceImpl.getSourceFileExtension(
iterationId.getProjectSlug(),
iterationId.getIterationSlug(),
hDoc.getPath(), hDoc.getName());
downloadExtensions.put(extension,
"baked?docId=" + encodedDocId);
}
DocumentInfo doc =
new DocumentInfo(
new DocumentId(hDoc.getId(), hDoc.getDocId()),
hDoc.getName(),
hDoc.getPath(),
hDoc.getLocale().getLocaleId(),
null,
new AuditInfo(hDoc.getLastChanged(), lastModifiedBy),
downloadExtensions, null);
docs.add(doc);
}
return new GetDocumentListResult(iterationId, docs);
}
@Override
public void rollback(GetDocumentList action, GetDocumentListResult result,
ExecutionContext context) throws ActionException {
}
private List<HDocument> getDocumentList(GetDocumentList action) {
ProjectIterationId iterationId =
action.getWorkspaceId().getProjectIterationId();
if (hasDocIdFilters(action)) {
return documentDAO.getByProjectIterationAndDocIdList(
iterationId.getProjectSlug(),
iterationId.getIterationSlug(), action.getDocIdFilters());
} else {
return documentDAO.getAllByProjectIteration(
iterationId.getProjectSlug(),
iterationId.getIterationSlug());
}
}
private boolean hasDocIdFilters(GetDocumentList action) {
return (action.getDocIdFilters() != null && !action.getDocIdFilters()
.isEmpty());
}
}