This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
GlossaryService.java
200 lines (168 loc) · 6.04 KB
/
GlossaryService.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package org.zanata.rest.service;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.UriInfo;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;
import org.zanata.common.LocaleId;
import org.zanata.dao.GlossaryDAO;
import org.zanata.model.HGlossaryEntry;
import org.zanata.model.HGlossaryTerm;
import org.zanata.model.HTermComment;
import org.zanata.rest.MediaTypes;
import org.zanata.rest.dto.Glossary;
import org.zanata.rest.dto.GlossaryEntry;
import org.zanata.rest.dto.GlossaryTerm;
import org.zanata.seam.resteasy.IgnoreInterfacePath;
import org.zanata.service.GlossaryFileService;
@Name("glossaryService")
@Path(GlossaryService.SERVICE_PATH)
@Transactional
@IgnoreInterfacePath
public class GlossaryService implements GlossaryResource
{
@Context
private UriInfo uri;
@HeaderParam("Content-Type")
@Context
private MediaType requestContentType;
@Context
private HttpHeaders headers;
@Context
private Request request;
@In
private GlossaryDAO glossaryDAO;
@In
private GlossaryFileService glossaryFileServiceImpl;
Log log = Logging.getLog(GlossaryService.class);
@Override
public Response getEntries()
{
ResponseBuilder response = request.evaluatePreconditions();
if (response != null)
{
return response.build();
}
List<HGlossaryEntry> hGlosssaryEntries = glossaryDAO.getEntries();
Glossary glossary = new Glossary();
transferEntriesResource(hGlosssaryEntries, glossary);
return Response.ok(glossary).build();
}
@Override
public Response get(@PathParam("locale")
LocaleId locale)
{
ResponseBuilder response = request.evaluatePreconditions();
if (response != null)
{
return response.build();
}
List<HGlossaryEntry> hGlosssaryEntries = glossaryDAO.getEntriesByLocaleId(locale);
Glossary glossary = new Glossary();
transferEntriesLocaleResource(hGlosssaryEntries, glossary, locale);
return Response.ok(glossary).build();
}
@Override
@Restrict("#{s:hasPermission('', 'glossary-insert')}")
public Response put(Glossary glossary)
{
ResponseBuilder response;
// must be a create operation
response = request.evaluatePreconditions();
if (response != null)
{
return response.build();
}
response = Response.created(uri.getAbsolutePath());
glossaryFileServiceImpl.saveGlossary(glossary);
return response.build();
}
@Override
@Restrict("#{s:hasPermission('', 'glossary-delete')}")
public Response deleteGlossary(@PathParam("locale") LocaleId targetLocale)
{
ResponseBuilder response = request.evaluatePreconditions();
if (response != null)
{
return response.build();
}
int rowCount = glossaryDAO.deleteAllEntries(targetLocale);
log.info("Glossary delete (" + targetLocale + "): " + rowCount);
return Response.ok().build();
}
@Override
@Restrict("#{s:hasPermission('', 'glossary-delete')}")
public Response deleteGlossaries()
{
ResponseBuilder response = request.evaluatePreconditions();
if (response != null)
{
return response.build();
}
int rowCount = glossaryDAO.deleteAllEntries();
log.info("Glossary delete all: " + rowCount);
return Response.ok().build();
}
public void transferEntriesResource(List<HGlossaryEntry> hGlosssaryEntries, Glossary glossary)
{
for (HGlossaryEntry hGlossaryEntry : hGlosssaryEntries)
{
GlossaryEntry glossaryEntry = new GlossaryEntry();
glossaryEntry.setSourcereference(hGlossaryEntry.getSourceRef());
glossaryEntry.setSrcLang(hGlossaryEntry.getSrcLocale().getLocaleId());
for (HGlossaryTerm hGlossaryTerm : hGlossaryEntry.getGlossaryTerms().values())
{
GlossaryTerm glossaryTerm = new GlossaryTerm();
glossaryTerm.setContent(hGlossaryTerm.getContent());
glossaryTerm.setLocale(hGlossaryTerm.getLocale().getLocaleId());
for (HTermComment hTermComment : hGlossaryTerm.getComments())
{
glossaryTerm.getComments().add(hTermComment.getComment());
}
glossaryEntry.getGlossaryTerms().add(glossaryTerm);
}
glossary.getGlossaryEntries().add(glossaryEntry);
}
}
public static void transferEntriesLocaleResource(List<HGlossaryEntry> hGlosssaryEntries, Glossary glossary, LocaleId locale)
{
for (HGlossaryEntry hGlossaryEntry : hGlosssaryEntries)
{
GlossaryEntry glossaryEntry = new GlossaryEntry();
glossaryEntry.setSrcLang(hGlossaryEntry.getSrcLocale().getLocaleId());
glossaryEntry.setSourcereference(hGlossaryEntry.getSourceRef());
for (HGlossaryTerm hGlossaryTerm : hGlossaryEntry.getGlossaryTerms().values())
{
if (hGlossaryTerm.getLocale().getLocaleId().equals(locale))
{
GlossaryTerm glossaryTerm = new GlossaryTerm();
glossaryTerm.setContent(hGlossaryTerm.getContent());
glossaryTerm.setLocale(hGlossaryTerm.getLocale().getLocaleId());
for (HTermComment hTermComment : hGlossaryTerm.getComments())
{
glossaryTerm.getComments().add(hTermComment.getComment());
}
glossaryEntry.getGlossaryTerms().add(glossaryTerm);
}
}
glossary.getGlossaryEntries().add(glossaryEntry);
}
}
}