KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > admin > languages > ManageSiteLanguages


1 package org.jahia.admin.languages;
2
3 import java.io.IOException JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Enumeration JavaDoc;
6 import java.util.HashSet JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.Locale JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Set JavaDoc;
11 import java.util.TreeMap JavaDoc;
12 import java.util.Vector JavaDoc;
13
14 import javax.servlet.ServletException JavaDoc;
15 import javax.servlet.http.HttpServletRequest JavaDoc;
16 import javax.servlet.http.HttpServletResponse JavaDoc;
17 import javax.servlet.http.HttpSession JavaDoc;
18
19 import org.jahia.bin.Jahia;
20 import org.jahia.bin.JahiaAdministration;
21 import org.jahia.content.ContentPageKey;
22 import org.jahia.data.JahiaData;
23 import org.jahia.exceptions.JahiaException;
24 import org.jahia.params.ParamBean;
25 import org.jahia.registries.ServicesRegistry;
26 import org.jahia.resourcebundle.JahiaResourceBundle;
27 import org.jahia.services.pages.ContentPage;
28 import org.jahia.services.pages.JahiaPage;
29 import org.jahia.services.pages.JahiaPageService;
30 import org.jahia.services.sites.JahiaSite;
31 import org.jahia.services.sites.JahiaSiteTools;
32 import org.jahia.services.sites.SiteLanguageMapping;
33 import org.jahia.services.sites.SiteLanguageMappingPersistance;
34 import org.jahia.services.sites.SiteLanguageSettings;
35 import org.jahia.services.sites.SiteLanguagesPersistance;
36 import org.jahia.services.usermanager.JahiaUser;
37 import org.jahia.services.version.ActivationTestResults;
38 import org.jahia.services.version.EntryLoadRequest;
39 import org.jahia.services.version.JahiaSaveVersion;
40 import org.jahia.services.version.StateModificationContext;
41 import org.jahia.utils.LanguageCodeConverters;
42 import org.jahia.security.license.License;
43
44 /**
45  * <p>Title: Manage site languages</p>
46  * <p>Description: Administration web user interface to manage the language
47  * settings of a Jahia site.</p>
48  * <p>Copyright: Copyright (c) 2002</p>
49  * <p>Company: </p>
50  * @author Serge Huber
51  * @version 1.0
52  */

53
54 public class ManageSiteLanguages {
55
56     private static org.apache.log4j.Logger logger =
57             org.apache.log4j.Logger.getLogger(ManageSiteLanguages.class);
58
59     private static final String JavaDoc CLASS_NAME = JahiaAdministration.CLASS_NAME;
60     private static final String JavaDoc JSP_PATH = JahiaAdministration.JSP_PATH;
61
62     private static final String JavaDoc MSG_INTERNAL_ERROR = new String JavaDoc ("Jahia Administration internal error");
63
64     private JahiaSite site;
65     private JahiaUser user;
66     private ServicesRegistry sReg;
67
68     private License coreLicense;
69
70     /**
71      * Default constructor.
72      * @author Alexandre Kraft
73      *
74      * @param request Servlet request.
75      * @param response Servlet response.
76      * @param session Servlet session for the current user.
77      */

78     public ManageSiteLanguages ( HttpServletRequest JavaDoc request,
79                                  HttpServletResponse JavaDoc response,
80                                  HttpSession JavaDoc session )
81     throws Throwable JavaDoc
82     {
83
84       JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
85       ParamBean jParams = null;
86      if (jData != null) {
87           jParams = jData.params();
88       }
89         coreLicense = Jahia.getCoreLicense();
90         if ( coreLicense == null ){
91             // set request attributes...
92
String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.invalidLicenseKey.label",
93                                                jParams, jParams.getLocale());
94             request.setAttribute("jahiaDisplayMessage", dspMsg);
95             // redirect...
96
JahiaAdministration.doRedirect( request, response, session, JSP_PATH + "menu.jsp" );
97             return;
98         }
99
100         userRequestDispatcher( request, response, session );
101     } // end constructor
102

103
104
105     //-------------------------------------------------------------------------
106
/**
107      * This method is used like a dispatcher for user requests.
108      * @author Alexandre Kraft
109      *
110      * @param request Servlet request.
111      * @param response Servlet response.
112      * @param session Servlet session for the current user.
113      */

114     private void userRequestDispatcher( HttpServletRequest JavaDoc request,
115                                         HttpServletResponse JavaDoc response,
116                                         HttpSession JavaDoc session )
117     throws Throwable JavaDoc
118     {
119       JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
120       ParamBean jParams = null;
121      if (jData != null) {
122           jParams = jData.params();
123       }
124
125         String JavaDoc operation = request.getParameter("sub");
126
127         sReg = ServicesRegistry.getInstance();
128
129         // check if the user has really admin access to this site...
130
user = (JahiaUser) session.getAttribute( ParamBean.SESSION_USER );
131         site = (JahiaSite) session.getAttribute( ParamBean.SESSION_SITE );
132
133         if ( site != null && user != null && sReg != null){
134
135             if ( JahiaSiteTools.getAdminGroup(site).isMember(user) ){
136
137                 // set the new site id to administrate...
138
request.setAttribute( "site", site );
139
140                 if (operation.equals("display")) {
141                     displayLanguageList( request, response, session );
142                 } else if (operation.equals("commit")) {
143                     commitChanges( request, response, session );
144                 } else if (operation.equals("reallyDelete")) {
145                     reallyDelete ( request, response, session );
146                 } else if (operation.equals("displayMappings")) {
147                     displayMappings ( request, response, session );
148                 } else if (operation.equals("commitMappings")) {
149                     commitMappings ( request, response, session );
150                 } else {
151                     displayLanguageList ( request, response, session );
152                 }
153
154             } else {
155                 //System.out.println(" --> no admin access on this site <--");
156
String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
157                                                    jParams, jParams.getLocale());
158                 request.setAttribute("jahiaDisplayMessage", dspMsg);
159                    JahiaAdministration.doRedirect( request,
160                                                    response,
161                                                    session, JSP_PATH + "menu.jsp" );
162                }
163         } else {
164           String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
165                                              jParams, jParams.getLocale());
166           request.setAttribute("jahiaDisplayMessage", dspMsg);
167                JahiaAdministration.doRedirect( request,
168                                                response,
169                                                session, JSP_PATH + "menu.jsp" );
170         }
171     } // userRequestDispatcher
172

173
174
175     //-------------------------------------------------------------------------
176
private void displayLanguageList( HttpServletRequest JavaDoc request,
177                                        HttpServletResponse JavaDoc response,
178                                        HttpSession JavaDoc session )
179     throws IOException JavaDoc, ServletException JavaDoc
180     {
181       JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
182       ParamBean jParams = null;
183      if (jData != null) {
184           jParams = jData.params();
185       }
186         try {
187
188             Vector JavaDoc siteLangSettings = site.getLanguageSettings();
189             Vector JavaDoc languageMappings = site.getLanguageMappings();
190
191             // this language set is prepared for the view to be able to quickly
192
// determine which languages are already inserted in the site.
193
Set JavaDoc languageSet = new HashSet JavaDoc();
194             Enumeration JavaDoc siteLangSettingsEnum = siteLangSettings.elements();
195             while (siteLangSettingsEnum.hasMoreElements()) {
196                 SiteLanguageSettings curSetting = (SiteLanguageSettings) siteLangSettingsEnum.nextElement();
197                 languageSet.add(curSetting.getCode());
198             }
199
200             // check the site's home page language
201
ContentPage contentPage = jParams.getSite().getHomeContentPage();
202             Map JavaDoc langStates = contentPage.getLanguagesStates(false);
203             Set JavaDoc homePageLanguageSet = langStates.keySet();
204
205             Locale JavaDoc[] availableLocales = Locale.getAvailableLocales();
206
207             request.setAttribute("mixLanguages", new Boolean JavaDoc(site.isMixLanguagesActive()));
208             request.setAttribute("homePageLanguageSet", homePageLanguageSet);
209             request.setAttribute("languageSet", languageSet);
210             request.setAttribute("languageList", siteLangSettings.elements());
211             request.setAttribute("mappingList", languageMappings.elements());
212
213             JahiaAdministration.doRedirect( request,
214                                             response,
215                                             session,
216                                             JSP_PATH + "manage_languages.jsp" );
217
218         } catch ( JahiaException je ){
219           String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
220                                              jParams, jParams.getLocale());
221           request.setAttribute("jahiaDisplayMessage", dspMsg);
222             JahiaAdministration.doRedirect( request,
223                                             response,
224                                             session,
225                                             JSP_PATH + "menu.jsp" );
226         }
227
228
229     }
230
231     private void displayLanguageDeleteConfirmation(
232             Vector JavaDoc languagesToDelete,
233             HttpServletRequest JavaDoc request,
234             HttpServletResponse JavaDoc response,
235             HttpSession JavaDoc session )
236     throws IOException JavaDoc, ServletException JavaDoc {
237           JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
238           ParamBean jParams = null;
239          if (jData != null) {
240               jParams = jData.params();
241          }
242
243         try {
244
245             Vector JavaDoc siteLangSettings = site.getLanguageSettings();
246             Vector JavaDoc languageMappings = site.getLanguageMappings();
247
248             // this language set is prepared for the view to be able to quickly
249
// determine which languages are already inserted in the site.
250
Set JavaDoc languageSet = new HashSet JavaDoc();
251             Enumeration JavaDoc siteLangSettingsEnum = siteLangSettings.elements();
252             while (siteLangSettingsEnum.hasMoreElements()) {
253                 SiteLanguageSettings curSetting = (SiteLanguageSettings) siteLangSettingsEnum.nextElement();
254                 languageSet.add(curSetting.getCode());
255             }
256
257             request.setAttribute("languagesToDelete", languagesToDelete);
258
259             Locale JavaDoc[] availableLocales = Locale.getAvailableLocales();
260
261             JahiaAdministration.doRedirect( request,
262                                             response,
263                                             session,
264                                             JSP_PATH + "manage_languages_delete_confirmation.jsp" );
265
266         } catch ( JahiaException je ){
267           String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
268                                              jParams, jParams.getLocale());
269           request.setAttribute("jahiaDisplayMessage", dspMsg);
270             JahiaAdministration.doRedirect( request,
271                                             response,
272                                             session,
273                                             JSP_PATH + "menu.jsp" );
274         }
275
276     }
277
278
279
280
281     //-------------------------------------------------------------------------
282
private void commitChanges( HttpServletRequest JavaDoc request,
283                                 HttpServletResponse JavaDoc response,
284                                 HttpSession JavaDoc session )
285
286     throws IOException JavaDoc, ServletException JavaDoc
287     {
288
289       JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
290       ParamBean jParams = null;
291      if (jData != null) {
292           jParams = jData.params();
293       }
294
295         try {
296
297             SiteLanguagesPersistance langSettingsSrv = SiteLanguagesPersistance.getInstance();
298             Vector JavaDoc languageMappings = SiteLanguageMappingPersistance.getInstance().getSiteLanguageMappings(site.getID());
299
300             request.setAttribute("warningMsg","");
301
302             // first lets check if we have any operations to do on the list of
303
// currently configured languages.
304
Map JavaDoc parameterMap = request.getParameterMap();
305
306             // first let's check the language mix option
307
String JavaDoc mixLanguages = request.getParameter("mixLanguages");
308             if (mixLanguages != null) {
309                 logger.debug("Setting language mix for site to active");
310                 site.setMixLanguagesActive(true);
311             } else {
312                 logger.debug("Setting language mix for site to disabled");
313                 site.setMixLanguagesActive(false);
314             }
315
316             // second let's process the rank modifications of each language
317
Enumeration JavaDoc siteLangSettings = site.getLanguageSettings().elements();
318             Map JavaDoc languageSettingMap = new TreeMap JavaDoc();
319             while (siteLangSettings.hasMoreElements()) {
320                 SiteLanguageSettings curLangSettings = (SiteLanguageSettings) siteLangSettings.nextElement();
321                 String JavaDoc curLangRankStr = request.getParameter("rank_" + curLangSettings.getCode());
322                 int curLangRank = Integer.parseInt(curLangRankStr);
323                 logger.debug("Rank " + curLangRank + " : " + curLangSettings.getCode());
324                 languageSettingMap.put(new Integer JavaDoc(curLangRank), curLangSettings);
325             }
326
327             // now let's assign the rank in the language settings themselves
328
Iterator JavaDoc languageRanks = languageSettingMap.keySet().iterator();
329             int count = 1;
330             while (languageRanks.hasNext()) {
331                 Integer JavaDoc curRank = (Integer JavaDoc) languageRanks.next();
332                 SiteLanguageSettings curSetting = (SiteLanguageSettings) languageSettingMap.get(curRank);
333                 logger.debug("Assigning rank " + count + " to language " + curSetting.getCode() + " using treemap entry " + curRank);
334                 curSetting.setRank(count);
335                 langSettingsSrv.updateSiteLanguageSettings(curSetting);
336                 count++;
337             }
338
339             Vector JavaDoc languagesToDelete = new Vector JavaDoc();
340             // let's update the languages that need to be updated
341
Enumeration JavaDoc siteLangSettingEnum = site.getLanguageSettings().elements();
342             while (siteLangSettingEnum.hasMoreElements()) {
343                 SiteLanguageSettings curSetting = (SiteLanguageSettings) siteLangSettingEnum.nextElement();
344                 String JavaDoc activeStr = request.getParameter("active_" + curSetting.getCode());
345                 if (activeStr != null) {
346                     curSetting.setActivated(true);
347                 } else {
348                     curSetting.setActivated(false);
349                 }
350                 String JavaDoc mandatoryStr = request.getParameter("mandatory_" + curSetting.getCode());
351                 if (mandatoryStr != null) {
352                     curSetting.setMandatory(true);
353                 } else {
354                     curSetting.setMandatory(false);
355                 }
356                 String JavaDoc deleteStr = request.getParameter("delete_" + curSetting.getCode());
357                 logger.debug("lang " + curSetting.getCode() +
358                              " activeStr=[" + activeStr +
359                              "] mandatoryStr=[" + mandatoryStr +
360                              "] deleteStr=[" + deleteStr + "]");
361                 if (deleteStr != null) {
362                     languagesToDelete.add(curSetting);
363                 } else {
364                     langSettingsSrv.updateSiteLanguageSettings(curSetting);
365                 }
366             }
367
368             // now let's add the new languages
369
String JavaDoc[] newLanguages = request.getParameterValues("language_list");
370             if (newLanguages != null) {
371                 if (newLanguages.length > 0) {
372                     for (int i=0; i < newLanguages.length; i++) {
373                         SiteLanguageSettings newLanguage =
374                                new SiteLanguageSettings(site.getID(), newLanguages[i], true, count+i, false);
375                         langSettingsSrv.addSiteLanguageSettings(newLanguage);
376
377                         // now we must add the mappings if necessary.
378
Locale JavaDoc newLocale = LanguageCodeConverters.languageCodeToLocale(newLanguages[i]);
379                         if (!"".equals(newLocale.getCountry())) {
380                             // the locale has a country set, let's add a
381
// mapping for a country-less language if there
382
// isn't already one and if there isn't already
383
// a country-less language
384
if (!isLanguageCodeInSettings(newLocale.getLanguage(), site.getLanguageSettings())) {
385                                 if (!isLanguageCodeInFromMapping(newLocale.getLanguage(), languageMappings)) {
386                                     SiteLanguageMapping newMapping =
387                                             new SiteLanguageMapping(
388                                             site.getID(), newLocale.getLanguage(),
389                                             newLocale.toString());
390                                     SiteLanguageMappingPersistance.getInstance().addSiteLanguageMapping(newMapping);
391                                     languageMappings = SiteLanguageMappingPersistance.getInstance().getSiteLanguageMappings(site.getID());
392                                 }
393                             }
394                         } else {
395                             // the language has no country set, let's see if a
396
// mapping already exists, and in this case we
397
// remove it.
398
SiteLanguageMapping existingMapping = getLanguageMapping(newLocale.toString(), languageMappings);
399                             if (existingMapping != null) {
400                                 logger.debug("Removing existing mapping : " +
401                                         existingMapping.getFromLanguageCode() +
402                                         " -> " +
403                                         existingMapping.getToLanguageCode());
404                                 SiteLanguageMappingPersistance.getInstance().removeSiteLanguageMapping(existingMapping.getId());
405                                 languageMappings = SiteLanguageMappingPersistance.getInstance().getSiteLanguageMappings(site.getID());
406                             }
407                         }
408                     }
409                 }
410             }
411
412             // finally the most complicated operation, let's delete the
413
// languages. This is a two step process that requires a
414
// confirmation by the user.
415

416             if (languagesToDelete.size() == 0) {
417               String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.changeCommitted.label",
418                                                  jParams, jParams.getLocale());
419               request.setAttribute("jahiaDisplayMessage", dspMsg);
420                 displayLanguageList(request, response, session);
421             } else {
422                 session.setAttribute("languagesToDelete", languagesToDelete);
423                 displayLanguageDeleteConfirmation(languagesToDelete, request, response, session);
424             }
425         } catch ( JahiaException je ){
426           String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
427                                              jParams, jParams.getLocale());
428           request.setAttribute("jahiaDisplayMessage", dspMsg);
429             JahiaAdministration.doRedirect( request,
430                                             response,
431                                             session,
432                                             JSP_PATH + "menu.jsp" );
433         }
434
435
436     } // end addComponent
437

438     private boolean isLanguageCodeInSettings(String JavaDoc languageCode, Vector JavaDoc siteLanguageSettings) {
439         Enumeration JavaDoc languagesEnum = siteLanguageSettings.elements();
440         while (languagesEnum.hasMoreElements()) {
441             SiteLanguageSettings curSetting = (SiteLanguageSettings) languagesEnum.nextElement();
442             if (curSetting.getCode().equals(languageCode)) {
443                 return true;
444             }
445         }
446         return false;
447     }
448
449     private boolean isLanguageCodeInFromMapping(String JavaDoc fromLanguageCode, Vector JavaDoc mappings) {
450         Enumeration JavaDoc mappingEnum = mappings.elements();
451         while (mappingEnum.hasMoreElements()) {
452             SiteLanguageMapping curMapping = (SiteLanguageMapping) mappingEnum.nextElement();
453             if (curMapping.getFromLanguageCode().equals(fromLanguageCode)) {
454                 return true;
455             }
456         }
457         return false;
458     }
459
460     private SiteLanguageMapping getLanguageMapping(String JavaDoc fromLanguageCode, Vector JavaDoc mappings) {
461         Enumeration JavaDoc mappingEnum = mappings.elements();
462         while (mappingEnum.hasMoreElements()) {
463             SiteLanguageMapping curMapping = (SiteLanguageMapping) mappingEnum.nextElement();
464             if (curMapping.getFromLanguageCode().equals(fromLanguageCode)) {
465                 return curMapping;
466             }
467         }
468         return null;
469     }
470
471     private SiteLanguageMapping getReverseLanguageMapping(String JavaDoc toLanguageCode, Vector JavaDoc mappings) {
472         Enumeration JavaDoc mappingEnum = mappings.elements();
473         while (mappingEnum.hasMoreElements()) {
474             SiteLanguageMapping curMapping = (SiteLanguageMapping) mappingEnum.nextElement();
475             if (curMapping.getToLanguageCode().equals(toLanguageCode)) {
476                 return curMapping;
477             }
478         }
479         return null;
480     }
481
482
483     //-------------------------------------------------------------------------
484
private void reallyDelete ( HttpServletRequest JavaDoc request,
485                                 HttpServletResponse JavaDoc response,
486                                 HttpSession JavaDoc session )
487     throws ServletException JavaDoc, IOException JavaDoc {
488
489          JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
490          ParamBean jParams = null;
491         if (jData != null) {
492              jParams = jData.params();
493          }
494
495         try {
496
497             SiteLanguagesPersistance langSettingsSrv = SiteLanguagesPersistance.getInstance();
498             JahiaPageService pageSrv = ServicesRegistry.getInstance().getJahiaPageService();
499             Vector JavaDoc languageMappings = SiteLanguageMappingPersistance.getInstance().getSiteLanguageMappings(site.getID());
500             //JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
501

502             request.setAttribute("warningMsg","");
503
504             Vector JavaDoc languagesToDelete = (Vector JavaDoc) session.getAttribute("languagesToDelete");
505
506             if (languagesToDelete.size() == 0) {
507                 displayLanguageList(request, response, session);
508             } else {
509
510                 session.removeAttribute("languagesToDelete");
511                 logger.debug("Really deleting languages...");
512                 JahiaPage siteHomePage = site.getHomePage();
513                 ContentPage siteHomeContentPage = pageSrv.lookupContentPage(siteHomePage.getID(), true);
514                 Set JavaDoc languageCodeSet = new HashSet JavaDoc();
515                 Enumeration JavaDoc languagesToDeleteEnum = languagesToDelete.elements();
516                 ArrayList JavaDoc locales = new ArrayList JavaDoc();
517                 while (languagesToDeleteEnum.hasMoreElements()) {
518                     SiteLanguageSettings curSetting = (SiteLanguageSettings) languagesToDeleteEnum.nextElement();
519                     locales.add(LanguageCodeConverters.languageCodeToLocale(curSetting.getCode()));
520                     languageCodeSet.add(curSetting.getCode());
521                 }
522
523                 //ParamBean jParams = jData.params();
524
EntryLoadRequest entryLoadRequest = new EntryLoadRequest(
525                     EntryLoadRequest.STAGING_WORKFLOW_STATE, 0, locales, true);
526                 jParams.setSubstituteEntryLoadRequest(entryLoadRequest);
527                 StateModificationContext stateModifContext = new
528                     StateModificationContext(new ContentPageKey(
529                     siteHomeContentPage.getID()), languageCodeSet);
530                 stateModifContext.setDescendingInSubPages(true);
531                 Iterator JavaDoc languageCodeIter = languageCodeSet.iterator();
532                 while (languageCodeIter.hasNext()) {
533                     String JavaDoc curLanguageCode = (String JavaDoc) languageCodeIter.next();
534                     logger.debug("Marking language code [" + curLanguageCode + "] for deletion...");
535                     siteHomeContentPage.markLanguageForDeletion(jParams.getUser(),
536                         curLanguageCode, stateModifContext);
537                 }
538
539                 logger.debug("Now activating site in specified languages, activating the content marked for deletion...");
540
541                 ActivationTestResults activationResults = siteHomeContentPage.
542                     activeStagingEntries(languageCodeSet, true,
543                                          new JahiaSaveVersion(true, true),
544                                          jParams.getUser(),
545                                          jParams, stateModifContext);
546                 if (activationResults.getStatus() != ActivationTestResults.COMPLETED_OPERATION_STATUS) {
547                     logger.debug("Activation results=" + activationResults.toString());
548                 }
549
550                 jParams.resetSubstituteEntryLoadRequest();
551
552                 // now let's remove the languageCodeSet elements from the site
553
// settings, by also taking care of the mappings.
554
Vector JavaDoc siteLanguageSettings = langSettingsSrv.getSiteLanguages(site.getID());
555                 Enumeration JavaDoc siteLanguageEnum = siteLanguageSettings.elements();
556                 while (siteLanguageEnum.hasMoreElements()) {
557                     SiteLanguageSettings curSetting = (SiteLanguageSettings) siteLanguageEnum.nextElement();
558                     if (languageCodeSet.contains(curSetting.getCode())) {
559                         langSettingsSrv.removeSiteLanguageSettings(curSetting.getID());
560                         // we must also remove any mapping associated with this language
561
// and/or try to remap to another language.
562
SiteLanguageMapping curMapping = getReverseLanguageMapping(curSetting.getCode(), languageMappings);
563                         if (curMapping != null) {
564                             String JavaDoc fromLanguageCode = curMapping.getFromLanguageCode();
565                             SiteLanguageMappingPersistance.getInstance().removeSiteLanguageMapping(curMapping.getId());
566                             languageMappings = SiteLanguageMappingPersistance.getInstance().getSiteLanguageMappings(site.getID());
567                         }
568                     }
569                 }
570                 // ok we've removed the mappings, can we reassign them ?
571
rebuildSiteMappings(site);
572             }
573
574             displayLanguageList(request, response, session);
575         } catch ( JahiaException je ){
576             logger.debug("Exception deleting site language",je);
577             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
578                                              jParams, jParams.getLocale());
579             request.setAttribute("jahiaDisplayMessage", dspMsg);
580             JahiaAdministration.doRedirect( request,
581                                             response,
582                                             session,
583                                             JSP_PATH + "menu.jsp" );
584         }
585
586     }
587
588     private void rebuildSiteMappings (JahiaSite site) throws JahiaException {
589         SiteLanguagesPersistance langSettingsSrv = SiteLanguagesPersistance.getInstance();
590         Vector JavaDoc languageMappings = SiteLanguageMappingPersistance.getInstance().getSiteLanguageMappings(site.getID());
591         Vector JavaDoc siteLanguageSettings = langSettingsSrv.getSiteLanguages(site.getID());
592         Enumeration JavaDoc siteLanguageEnum = siteLanguageSettings.elements();
593         while (siteLanguageEnum.hasMoreElements()) {
594             SiteLanguageSettings curSetting = (SiteLanguageSettings) siteLanguageEnum.nextElement();
595             Locale JavaDoc curLocale = LanguageCodeConverters.languageCodeToLocale(curSetting.getCode());
596             if (!"".equals(curLocale.getCountry())) {
597                 SiteLanguageMapping curMapping = getLanguageMapping(curLocale.getLanguage(), languageMappings);
598                 if (curMapping == null) {
599                     // no mapping found for the ISO 639 language code, let's
600
// add one.
601
SiteLanguageMapping newMapping =
602                             new SiteLanguageMapping(
603                             site.getID(), curLocale.getLanguage(),
604                             curLocale.toString());
605                     SiteLanguageMappingPersistance.getInstance().addSiteLanguageMapping(newMapping);
606                     languageMappings = SiteLanguageMappingPersistance.getInstance().getSiteLanguageMappings(site.getID());
607                 }
608             }
609         }
610     }
611
612     //-------------------------------------------------------------------------
613
private void displayMappings ( HttpServletRequest JavaDoc request,
614                                 HttpServletResponse JavaDoc response,
615                                 HttpSession JavaDoc session )
616     throws ServletException JavaDoc, IOException JavaDoc {
617
618          JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
619          ParamBean jParams = null;
620         if (jData != null) {
621              jParams = jData.params();
622          }
623
624         try {
625
626             Vector JavaDoc siteLangSettings = site.getLanguageSettings();
627             Vector JavaDoc languageMappings = site.getLanguageMappings();
628
629             Map JavaDoc iso639ToLocale = new TreeMap JavaDoc();
630
631             // this language set is prepared for the view to be able to quickly
632
// determine which languages are already inserted in the site.
633
Set JavaDoc languageSet = new HashSet JavaDoc();
634             Enumeration JavaDoc siteLangSettingsEnum = siteLangSettings.elements();
635             while (siteLangSettingsEnum.hasMoreElements()) {
636                 SiteLanguageSettings curSetting = (SiteLanguageSettings) siteLangSettingsEnum.nextElement();
637                 languageSet.add(curSetting.getCode());
638
639                 // now we build a map that regroups all the country specific
640
// languages into a language ISO 639 code.
641
Locale JavaDoc curLocale = LanguageCodeConverters.languageCodeToLocale(curSetting.getCode());
642                 if (isLanguageCodeInFromMapping(curLocale.getLanguage(), languageMappings)) {
643                     if (!"".equals(curLocale.getCountry())) {
644                         if (!iso639ToLocale.containsKey(curLocale.getLanguage())) {
645                             iso639ToLocale.put(curLocale.getLanguage(), new Vector JavaDoc());
646                         }
647                         Vector JavaDoc localizedLanguages = (Vector JavaDoc) iso639ToLocale.get(curLocale.getLanguage());
648                         localizedLanguages.add(curLocale);
649                     }
650                 }
651             }
652
653             request.setAttribute("languageSet", languageSet);
654             request.setAttribute("languageList", siteLangSettings.elements());
655             request.setAttribute("languageMappings", languageMappings);
656             request.setAttribute("mappingList", languageMappings.elements());
657             request.setAttribute("iso639ToLocale", iso639ToLocale);
658
659             Locale JavaDoc[] availableLocales = Locale.getAvailableLocales();
660             boolean acceptsUTF8 = false;
661             String JavaDoc acceptCharset = request.getHeader("accept-charset");
662             if (acceptCharset != null) {
663                 if (acceptCharset.toLowerCase().indexOf("utf-8") != -1) {
664                     acceptsUTF8 = true;
665                 }
666             }
667             request.setAttribute("acceptsUTF8", new Boolean JavaDoc(acceptsUTF8));
668
669             JahiaAdministration.doRedirect( request,
670                                             response,
671                                             session,
672                                             JSP_PATH + "manage_languages_edit_mappings.jsp" );
673
674         } catch ( JahiaException je ){
675           String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
676                                              jParams, jParams.getLocale());
677           request.setAttribute("jahiaDisplayMessage", dspMsg);
678             JahiaAdministration.doRedirect( request,
679                                             response,
680                                             session,
681                                             JSP_PATH + "menu.jsp" );
682         }
683     }
684
685     //-------------------------------------------------------------------------
686
private void commitMappings ( HttpServletRequest JavaDoc request,
687                                   HttpServletResponse JavaDoc response,
688                                   HttpSession JavaDoc session )
689     throws ServletException JavaDoc, IOException JavaDoc {
690
691         JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
692         ParamBean jParams = null;
693        if (jData != null) {
694             jParams = jData.params();
695         }
696         try {
697
698             SiteLanguagesPersistance langSettingsSrv = SiteLanguagesPersistance.getInstance();
699             JahiaPageService pageSrv = ServicesRegistry.getInstance().getJahiaPageService();
700             Vector JavaDoc languageMappings = SiteLanguageMappingPersistance.getInstance().getSiteLanguageMappings(site.getID());
701             //JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
702

703             request.setAttribute("warningMsg","");
704
705             Enumeration JavaDoc mappingsEnum = languageMappings.elements();
706             while (mappingsEnum.hasMoreElements()) {
707                 SiteLanguageMapping curMapping = (SiteLanguageMapping) mappingsEnum.nextElement();
708                 String JavaDoc newMapping = request.getParameter("mapping_" + curMapping.getFromLanguageCode());
709                 if ((newMapping != null) && (!"".equals(newMapping))) {
710                     logger.debug("Changing mapping for " + curMapping.getFromLanguageCode() + " to " + newMapping);
711                     curMapping.setToLanguageCode(newMapping);
712                     SiteLanguageMappingPersistance.getInstance().updateSiteLanguageMapping(curMapping);
713                 }
714             }
715
716             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.mappingUpdated.label",
717                                                jParams, jParams.getLocale());
718             request.setAttribute("jahiaDisplayMessage", dspMsg);
719             displayMappings(request, response, session);
720         } catch ( JahiaException je ){
721           String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
722                                              jParams, jParams.getLocale());
723           request.setAttribute("jahiaDisplayMessage", dspMsg);
724             JahiaAdministration.doRedirect( request,
725                                             response,
726                                             session,
727                                             JSP_PATH + "menu.jsp" );
728         }
729
730
731     }
732
733 }
734
Popular Tags