View Javadoc

1   package org.gnomekr.potron.web.view;
2   
3   import static org.gnomekr.potron.util.NavigationCases.NAV_TRANSLATION_ADD;
4   import static org.gnomekr.potron.util.NavigationCases.NAV_TRANSLATION_UPDATE;
5   import static org.gnomekr.potron.util.NavigationCases.NAV_TRANSLATION_LIST;
6   import static org.gnomekr.potron.util.PotronConstants.DEFAULT_FILE_ENCODING;
7   
8   import java.io.IOException;
9   import java.io.InputStreamReader;
10  import java.io.Reader;
11  import java.io.UnsupportedEncodingException;
12  import java.util.ArrayList;
13  import java.util.List;
14  
15  import javax.faces.application.FacesMessage;
16  import javax.faces.model.SelectItem;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.apache.myfaces.custom.fileupload.UploadedFile;
21  import org.apache.myfaces.util.MessageUtils;
22  import org.gnomekr.potron.data.LanguageTeam;
23  import org.gnomekr.potron.data.Template;
24  import org.gnomekr.potron.data.Translation;
25  import org.gnomekr.potron.parser.ParseException;
26  import org.gnomekr.potron.service.IProjectManager;
27  import org.gnomekr.potron.service.ITranslationManager;
28  
29  /***
30   * TranslationView.java
31   * @author Xavier Cho
32   * @version $Revision 1.1 $ $Date: 2005/09/11 05:49:45 $
33   * @jsf.bean
34   *              name = "translation"
35   *              scope = "request"
36   * @jsf.navigation
37   *              from = "*"
38   *              to = "/admin/translation/add.jsf"
39   *              result = "translation.create"
40   * @jsf.navigation
41   *              from = "*"
42   *              to = "/admin/translation/update.jsf"
43   *              result = "translation.update"
44   * @jsf.navigation
45   *              from = "*"
46   *              to = "/admin/translation/list.jsf"
47   *              result = "translation.list"
48   */
49  public class TranslationView extends AbstractFacesView {
50  
51      private IProjectManager projectManager;
52      private ITranslationManager translationManager;
53  
54      private long id = -1;
55      private long templateId = -1;
56      private long languageTeamId = -1;
57      private String name;
58      private String encoding = DEFAULT_FILE_ENCODING;
59      private String comment;
60      private UploadedFile file;
61  
62      /***
63       * @return
64       * @jsf.managed-property
65       *          value = "#{projectManager}"
66       */
67      public IProjectManager getProjectManager() {
68          return projectManager;
69      }
70  
71      /***
72       * @return
73       * @jsf.managed-property
74       *          value = "#{translationManager}"
75       */
76      public ITranslationManager getTranslationManager() {
77          return translationManager;
78      }
79  
80      public List<SelectItem> getLanguageTeams() {
81          List<LanguageTeam> teams = projectManager.getLanguageTeams();
82          List<SelectItem> items = new ArrayList<SelectItem>(teams.size());
83  
84          for (LanguageTeam team : teams) {
85              SelectItem item = new SelectItem();
86  
87              item.setLabel(team.getName());
88              item.setValue(team.getId());
89              item.setDescription(team.getEncoding());
90  
91              items.add(item);
92          }
93  
94          return items;
95      }
96  
97      public String addTranslation() {
98          Template template = projectManager.getTemplate(templateId);
99  
100         LanguageTeam team = projectManager.getLanguageTeam(languageTeamId);
101 
102         Translation translation = new Translation();
103         translation.setName(name);
104         translation.setComment(comment);
105         translation.setTemplate(template);
106         translation.setLanguageTeam(team);
107 
108         if (file != null && file.getSize() > 0) {
109             Reader reader;
110 
111             try {
112                 reader = new InputStreamReader(file.getInputStream(), encoding);
113                 translationManager.addTranslation(translation, reader);
114             } catch (UnsupportedEncodingException e) {
115                 MessageUtils.addMessage(
116                         FacesMessage.SEVERITY_WARN,
117                         "error.unknown.encoding",
118                         null);
119 
120                 return NAV_TRANSLATION_ADD;
121             } catch (IOException e) {
122                 Log log = LogFactory.getLog(getClass());
123                 if (log.isErrorEnabled()) {
124                     log.error(e);
125                 }
126 
127                 MessageUtils.addMessage(
128                         FacesMessage.SEVERITY_ERROR,
129                         "error.file.upload",
130                         null);
131 
132                 return NAV_TRANSLATION_ADD;
133             } catch (ParseException e) {
134                 Log log = LogFactory.getLog(getClass());
135                 if (log.isErrorEnabled()) {
136                     log.error(e);
137                 }
138 
139                 MessageUtils.addMessage(
140                         FacesMessage.SEVERITY_ERROR,
141                         "error.template.parse",
142                         null);
143 
144                 return NAV_TRANSLATION_ADD;
145             }
146         } else {
147             translationManager.addTranslation(translation);
148         }
149 
150         MessageUtils.addMessage(
151                 FacesMessage.SEVERITY_INFO,
152                 "msg.translation.add.successful",
153                 null);
154 
155         refreshNavigationTree();
156 
157         return NAV_TRANSLATION_LIST;
158     }
159 
160     public String updateTranslation() {
161         Translation translation = translationManager.getTranslation(id);
162 
163         LanguageTeam team = projectManager.getLanguageTeam(languageTeamId);
164 
165         translation.setName(name);
166         translation.setLanguageTeam(team);
167         translation.setComment(comment);
168 
169         if (file != null && file.getSize() > 0) {
170             Reader reader;
171 
172             try {
173                 reader = new InputStreamReader(file.getInputStream(), encoding);
174                 translationManager.updateTranslation(translation, reader);
175             } catch (UnsupportedEncodingException e) {
176                 MessageUtils.addMessage(
177                         FacesMessage.SEVERITY_WARN,
178                         "error.unknown.encoding",
179                         null);
180 
181                 return NAV_TRANSLATION_UPDATE;
182             } catch (IOException e) {
183                 Log log = LogFactory.getLog(getClass());
184                 if (log.isErrorEnabled()) {
185                     log.error(e);
186                 }
187 
188                 MessageUtils.addMessage(
189                         FacesMessage.SEVERITY_ERROR,
190                         "error.file.upload",
191                         null);
192 
193                 return NAV_TRANSLATION_UPDATE;
194             } catch (ParseException e) {
195                 Log log = LogFactory.getLog(getClass());
196                 if (log.isErrorEnabled()) {
197                     log.error(e);
198                 }
199 
200                 MessageUtils.addMessage(
201                         FacesMessage.SEVERITY_ERROR,
202                         "error.template.parse",
203                         null);
204 
205                 return NAV_TRANSLATION_UPDATE;
206             }
207         } else {
208             translationManager.updateTranslation(translation);
209         }
210 
211         MessageUtils.addMessage(
212                 FacesMessage.SEVERITY_INFO,
213                 "msg.translation.update.successful",
214                 null);
215 
216         refreshNavigationTree();
217 
218         return NAV_TRANSLATION_LIST;
219     }
220 
221     public String removeTranslation() {
222         translationManager.removeTranslation(id);
223 
224         MessageUtils.addMessage(
225                 FacesMessage.SEVERITY_INFO,
226                 "msg.translation.remove.successful",
227                 null);
228 
229         refreshNavigationTree();
230 
231         return NAV_TRANSLATION_LIST;
232     }
233 
234     public List<Translation> getTranslations() {
235         Template template = projectManager.getTemplate(templateId);
236         return new ArrayList<Translation>(template.getTranslations());
237     }
238 
239     /***
240      * @param id
241      */
242     public void setId(long id) {
243         this.id = id;
244 
245         Translation translation = translationManager.getTranslation(id);
246 
247         if (translation != null) {
248             this.name = translation.getName();
249             this.comment = translation.getComment();
250             this.languageTeamId = translation.getLanguageTeam().getId();
251             this.templateId = translation.getTemplate().getId();
252         }
253     }
254 
255     /***
256      * @return Returns the comment.
257      */
258     public String getComment() {
259         return comment;
260     }
261 
262     /***
263      * @param comment The comment to set.
264      */
265     public void setComment(String comment) {
266         this.comment = comment;
267     }
268 
269     /***
270      * @return Returns the languageTeamId.
271      */
272     public long getLanguageTeamId() {
273         return languageTeamId;
274     }
275 
276     /***
277      * @param languageTeamId The languageTeamId to set.
278      */
279     public void setLanguageTeamId(long languageTeamId) {
280         this.languageTeamId = languageTeamId;
281     }
282 
283     /***
284      * @return Returns the name.
285      */
286     public String getName() {
287         return name;
288     }
289 
290     /***
291      * @param name The name to set.
292      */
293     public void setName(String name) {
294         this.name = name;
295     }
296 
297     /***
298      * @return Returns the templateId.
299      */
300     public long getTemplateId() {
301         return templateId;
302     }
303 
304     /***
305      * @param templateId The templateId to set.
306      */
307     public void setTemplateId(long templateId) {
308         this.templateId = templateId;
309     }
310 
311     /***
312      * @return Returns the id.
313      */
314     public long getId() {
315         return id;
316     }
317 
318     /***
319      * @param projectManager The projectManager to set.
320      */
321     public void setProjectManager(IProjectManager projectManager) {
322         this.projectManager = projectManager;
323     }
324 
325     /***
326      * @param translationManager The translationManager to set.
327      */
328     public void setTranslationManager(ITranslationManager translationManager) {
329         this.translationManager = translationManager;
330     }
331 
332     /***
333      * @return
334      */
335     public String getEncoding() {
336         return encoding;
337     }
338 
339     /***
340      * @param encoding
341      */
342     public void setEncoding(String encoding) {
343         this.encoding = encoding;
344     }
345 
346     public UploadedFile getFile() {
347         return file;
348     }
349 
350     public void setFile(UploadedFile file) {
351         this.file = file;
352     }
353 }