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
/
GlossaryFileServiceImpl.java
204 lines (178 loc) · 6.78 KB
/
GlossaryFileServiceImpl.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
201
202
203
204
/*
* Copyright 2012, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.service.impl;
import static org.jboss.seam.ScopeType.STATELESS;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Transactional;
import org.zanata.adapter.glossary.GlossaryCSVReader;
import org.zanata.adapter.glossary.GlossaryPoReader;
import org.zanata.common.LocaleId;
import org.zanata.dao.GlossaryDAO;
import org.zanata.exception.ZanataServiceException;
import org.zanata.model.HGlossaryEntry;
import org.zanata.model.HGlossaryTerm;
import org.zanata.model.HLocale;
import org.zanata.model.HTermComment;
import org.zanata.rest.dto.Glossary;
import org.zanata.rest.dto.GlossaryEntry;
import org.zanata.rest.dto.GlossaryTerm;
import org.zanata.service.GlossaryFileService;
import org.zanata.service.LocaleService;
/**
*
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*
*/
@Name("glossaryFileServiceImpl")
@Scope(STATELESS)
@AutoCreate
public class GlossaryFileServiceImpl implements GlossaryFileService
{
@In
private GlossaryDAO glossaryDAO;
@In
private LocaleService localeServiceImpl;
private final static int BATCH_SIZE = 50;
@Override
public List<Glossary> parseGlossaryFile(InputStream fileContents, String fileName, LocaleId sourceLang, LocaleId transLang, boolean treatSourceCommentsAsTarget, List<String> commentsColumn)
{
try
{
if (StringUtils.endsWithIgnoreCase(fileName, ".csv"))
{
return parseCsvFile(fileContents, commentsColumn);
}
else if (StringUtils.endsWithIgnoreCase(fileName, ".po"))
{
return parsePoFile(fileContents, sourceLang, transLang, treatSourceCommentsAsTarget);
}
else
{
throw new ZanataServiceException("Unsupported Glossary file: " + fileName);
}
}
catch (Exception e)
{
throw new ZanataServiceException("Unsupported Glossary file: " + fileName);
}
}
@Override
public void saveGlossary(Glossary glossary)
{
int counter = 0;
for (int i = 0; i < glossary.getGlossaryEntries().size(); i++)
{
transferGlossaryEntry(glossary.getGlossaryEntries().get(i));
counter++;
if (counter == BATCH_SIZE || i == glossary.getGlossaryEntries().size() - 1)
{
executeCommit();
counter = 0;
}
}
}
private List<Glossary> parseCsvFile(InputStream fileContents, List<String> commentsColumn) throws IOException
{
GlossaryCSVReader csvReader = new GlossaryCSVReader(commentsColumn, BATCH_SIZE);
return csvReader.extractGlossary(new InputStreamReader(fileContents));
}
private List<Glossary> parsePoFile(InputStream fileContents, LocaleId sourceLang, LocaleId transLang, boolean treatSourceCommentsAsTarget) throws IOException
{
if (sourceLang == null || transLang == null)
{
throw new ZanataServiceException("Mandatory fields for PO file format: Source Language and Target Language");
}
GlossaryPoReader poReader = new GlossaryPoReader(sourceLang, transLang, treatSourceCommentsAsTarget, BATCH_SIZE);
return poReader.extractGlossary(new InputStreamReader(fileContents));
}
/**
* This force glossaryDAO to flush and commit every 50(BATCH_SIZE) records.
*/
@Transactional
private void executeCommit()
{
glossaryDAO.flush();
glossaryDAO.clear();
}
private void transferGlossaryEntry(GlossaryEntry from)
{
HGlossaryEntry to = getOrCreateGlossaryEntry(from.getSrcLang(), getSrcGlossaryTerm(from));
to.setSourceRef(from.getSourcereference());
for (GlossaryTerm glossaryTerm : from.getGlossaryTerms())
{
HLocale termHLocale = localeServiceImpl.validateSourceLocale(glossaryTerm.getLocale());
// check if there's existing term with same content, overrides comments
HGlossaryTerm hGlossaryTerm = getOrCreateGlossaryTerm(to, termHLocale, glossaryTerm);
hGlossaryTerm.getComments().clear();
for (String comment : glossaryTerm.getComments())
{
hGlossaryTerm.getComments().add(new HTermComment(comment));
}
to.getGlossaryTerms().put(termHLocale, hGlossaryTerm);
}
glossaryDAO.makePersistent(to);
}
public HGlossaryEntry getOrCreateGlossaryEntry(LocaleId srcLocale, String srcContent)
{
HGlossaryEntry hGlossaryEntry = glossaryDAO.getEntryBySrcLocaleAndContent(srcLocale, srcContent);
if (hGlossaryEntry == null)
{
hGlossaryEntry = new HGlossaryEntry();
HLocale srcHLocale = localeServiceImpl.getByLocaleId(srcLocale);
hGlossaryEntry.setSrcLocale(srcHLocale);
}
return hGlossaryEntry;
}
private HGlossaryTerm getOrCreateGlossaryTerm(HGlossaryEntry hGlossaryEntry, HLocale termHLocale, GlossaryTerm newTerm)
{
HGlossaryTerm hGlossaryTerm = hGlossaryEntry.getGlossaryTerms().get(termHLocale);
if (hGlossaryTerm == null)
{
hGlossaryTerm = new HGlossaryTerm(newTerm.getContent());
hGlossaryTerm.setLocale(termHLocale);
hGlossaryTerm.setGlossaryEntry(hGlossaryEntry);
}
else if(!hGlossaryTerm.getContent().equals(newTerm.getContent()))
{
hGlossaryTerm.setContent(newTerm.getContent());
}
return hGlossaryTerm;
}
private String getSrcGlossaryTerm(GlossaryEntry entry)
{
for (GlossaryTerm term : entry.getGlossaryTerms())
{
if (term.getLocale().equals(entry.getSrcLang()))
{
return term.getContent();
}
}
return null;
}
}