View Javadoc

1   package org.gnomekr.potron.web.view;
2   
3   import static org.gnomekr.potron.util.NavigationCases.NAV_TRANSLATOR_CHECKOUT_LIST;
4   import static org.gnomekr.potron.util.NavigationCases.NAV_TRANSLATOR_LIST;
5   
6   import java.util.ArrayList;
7   import java.util.List;
8   
9   import javax.faces.application.FacesMessage;
10  import javax.faces.component.UIParameter;
11  import javax.faces.event.ValueChangeEvent;
12  
13  import org.apache.commons.lang.StringUtils;
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  import org.apache.myfaces.util.MessageUtils;
17  import org.gnomekr.potron.data.PluralForms;
18  import org.gnomekr.potron.data.TranslatedEntry;
19  import org.gnomekr.potron.data.Translation;
20  import org.gnomekr.potron.service.AlreadyCheckedOutException;
21  import org.gnomekr.potron.service.IStatisticsManager;
22  import org.gnomekr.potron.service.ITranslationManager;
23  import org.gnomekr.potron.util.SelectableTableEntry;
24  
25  /***
26   * TranslatorView.java
27   * @author Xavier Cho
28   * @version $Revision 1.1 $ $Date: 2005/08/28 11:47:15 $
29   * @jsf.bean
30   *              name = "translator"
31   *              scope = "request"
32   * @jsf.navigation
33   *              from = "*"
34   *              to = "/user/translation.jsf"
35   *              result = "translator.list"
36   * @jsf.navigation
37   *              from = "*"
38   *              to = "/user/entries.jsf"
39   *              result = "translator.checkout.list"
40   * @jsf.navigation
41   *              from = "*"
42   *              to = "/user/checkIn.jsf"
43   *              result = "translator.checkin"
44   * @jsf.navigation
45   *              from = "*"
46   *              to = "/user/detail.jsf"
47   *              result = "translator.detail"
48   */
49  public class TranslatorView extends AbstractFacesView {
50  
51      private static final long serialVersionUID = 5120647410889206611L;
52      private static Log log = LogFactory.getLog(TranslatorView.class);
53  
54      private long entryId = -1;
55      private long translationId = -1;
56      private boolean startWorkingNow = true;
57  
58      private Translation translation;
59      private TranslatedEntry entry;
60      private ITranslationManager translationManager;
61      private IStatisticsManager statisticsManager;
62  
63      private List<SelectableTableEntry<TranslatedEntry>> translatedEntries;
64      private List<SelectableTableEntry<TranslatedEntry>> untranslatedEntries;
65  
66      /***
67       * @return
68       * @jsf.managed-property
69       *          value = "#{translationManager}"
70       */
71      public ITranslationManager getTranslationManager() {
72          return translationManager;
73      }
74  
75      /***
76       * @param translationManager The translationManager to set.
77       */
78      public void setTranslationManager(ITranslationManager translationManager) {
79          this.translationManager = translationManager;
80      }
81  
82      /***
83       * @return
84       * @jsf.managed-property
85       *          value = "#{statisticsManager}"
86       */
87      public IStatisticsManager getStatisticsManager() {
88          return statisticsManager;
89      }
90  
91      /***
92       * @param statisticsManager The statisticsManager to set.
93       */
94      public void setStatisticsManager(IStatisticsManager statisticsManager) {
95          this.statisticsManager = statisticsManager;
96      }
97  
98      public String checkOutSelectedEntries() {
99          List<TranslatedEntry> entriesToCheckOut = new ArrayList<TranslatedEntry>();
100 
101         if (untranslatedEntries != null) {
102             for (SelectableTableEntry<TranslatedEntry> entry : untranslatedEntries) {
103                 if (entry.isSelected()) {
104                     entriesToCheckOut.add(entry.getData());
105                 }
106             }
107         }
108 
109         if (translatedEntries != null) {
110             for (SelectableTableEntry<TranslatedEntry> entry : translatedEntries) {
111                 if (entry.isSelected()) {
112                     entriesToCheckOut.add(entry.getData());
113                 }
114             }
115         }
116 
117         int failedCount = 0;
118         for (TranslatedEntry entry : entriesToCheckOut) {
119             try {
120                 translationManager.checkOutEntry(entry.getId());
121             } catch (AlreadyCheckedOutException e) {
122                 if (log.isWarnEnabled()) {
123                     log
124                             .warn("The specified entry has been already checked out : "
125                                     + entry);
126                 }
127 
128                 failedCount++;
129             }
130         }
131 
132         if (entriesToCheckOut.size() > failedCount) {
133             MessageUtils.addMessage(
134                     FacesMessage.SEVERITY_INFO,
135                     "msg.translator.checkout.successful",
136                     null);
137         }
138 
139         if (failedCount > 0) {
140             MessageUtils.addMessage(
141                     FacesMessage.SEVERITY_WARN,
142                     "error.already.checkedout",
143                     new Integer[] {
144                         failedCount
145                     });
146         }
147 
148         if (startWorkingNow) {
149             return NAV_TRANSLATOR_CHECKOUT_LIST;
150         } else {
151             return NAV_TRANSLATOR_LIST;
152         }
153     }
154 
155     public void updatePluralString(ValueChangeEvent vce) {
156         String pluralMessage = (String) vce.getNewValue();
157 
158         UIParameter param = (UIParameter) vce.getComponent().getChildren().get(
159                 0);
160         Integer index = (Integer) param.getValue();
161 
162         entry.getPluralStrings().set(index, pluralMessage);
163     }
164 
165     public String checkInEntry() {
166         long id = entry.getId();
167         String translatedString = entry.getTranslatedString();
168         boolean fuzzy = entry.isFuzzy();
169 
170         translationManager.checkInEntry(id, translatedString, entry
171                 .getPluralStrings(), fuzzy);
172 
173         MessageUtils.addMessage(
174                 FacesMessage.SEVERITY_INFO,
175                 "msg.translator.checkin.successful",
176                 null);
177 
178         return NAV_TRANSLATOR_CHECKOUT_LIST;
179     }
180 
181     public String abandonCheckedOutEntry() {
182         translationManager.abandonCheckedOutEntry(entryId);
183 
184         MessageUtils.addMessage(
185                 FacesMessage.SEVERITY_INFO,
186                 "msg.translator.abandon.successful",
187                 null);
188 
189         return NAV_TRANSLATOR_CHECKOUT_LIST;
190     }
191 
192     /***
193      * @return
194      */
195     public List<TranslatedEntry> getCheckedOutEntries() {
196         List<TranslatedEntry> entries = translationManager
197                 .getCheckedOutEntries(translationId);
198 
199         return entries;
200     }
201 
202     /***
203      * @return
204      */
205     public List<SelectableTableEntry<TranslatedEntry>> getTranslatedEntries() {
206         if (translatedEntries == null) {
207             List<TranslatedEntry> entries = translationManager
208                     .getTranslatedEntries(translationId);
209 
210             this.translatedEntries = new ArrayList<SelectableTableEntry<TranslatedEntry>>();
211 
212             for (TranslatedEntry entry : entries) {
213                 translatedEntries
214                         .add(new SelectableTableEntry<TranslatedEntry>(entry));
215             }
216         }
217 
218         return translatedEntries;
219     }
220 
221     /***
222      * @return
223      */
224     public List<SelectableTableEntry<TranslatedEntry>> getUntranslatedEntries() {
225         if (untranslatedEntries == null) {
226             List<TranslatedEntry> entries = translationManager
227                     .getUntranslatedEntries(translationId, false);
228 
229             this.untranslatedEntries = new ArrayList<SelectableTableEntry<TranslatedEntry>>();
230 
231             for (TranslatedEntry entry : entries) {
232                 untranslatedEntries
233                         .add(new SelectableTableEntry<TranslatedEntry>(entry));
234             }
235         }
236 
237         return untranslatedEntries;
238     }
239 
240     /***
241      * @return
242      */
243     public List<TranslatedEntry> getCheckedOutEntriesForCurrentUser() {
244         List<TranslatedEntry> entries = translationManager
245                 .getCheckedOutEntries(getCurrentUserName());
246 
247         return entries;
248     }
249 
250     /***
251      * @return Returns the entry.
252      */
253     public TranslatedEntry getEntry() {
254         if (entry == null) {
255             this.entry = translationManager.getEntry(entryId);
256 
257             if (StringUtils.isNotEmpty(entry.getEntry().getPluralKey())
258                     && entry.getPluralStrings().isEmpty()) {
259                 PluralForms plForms = entry.getTranslation().getPluralForms();
260 
261                 List<String> emptyMessages = new ArrayList<String>();
262                 for (int i = 0; i < plForms.getCount() - 1; i++) {
263                     emptyMessages.add(new String());
264                 }
265 
266                 entry.setPluralStrings(emptyMessages);
267             }
268         }
269 
270         return entry;
271     }
272 
273     /***
274      * @return Returns the translation.
275      */
276     public Translation getTranslation() {
277         if (translation == null) {
278             this.translation = translationManager.getTranslation(translationId);
279         }
280 
281         return translation;
282     }
283 
284     /***
285      * @return Returns the translationId.
286      */
287     public long getTranslationId() {
288         return translationId;
289     }
290 
291     /***
292      * @param translationId The translationId to set.
293      */
294     public void setTranslationId(long translationId) {
295         this.translationId = translationId;
296     }
297 
298     /***
299      * @return
300      */
301     public long getEntryId() {
302         return entryId;
303     }
304 
305     /***
306      * @param entryId
307      */
308     public void setEntryId(long entryId) {
309         this.entryId = entryId;
310     }
311 
312     /***
313      * @return
314      */
315     public boolean isStartWorkingNow() {
316         return startWorkingNow;
317     }
318 
319     /***
320      * @param startWorkingNow
321      */
322     public void setStartWorkingNow(boolean startWorkingNow) {
323         this.startWorkingNow = startWorkingNow;
324     }
325 }