UpgradeGroupSiteLayoutPlugin.java

  1. /**
  2.  * Copyright (C) 2013 eXo Platform SAS.
  3.  *
  4.  * This is free software; you can redistribute it and/or modify it
  5.  * under the terms of the GNU Lesser General Public License as
  6.  * published by the Free Software Foundation; either version 2.1 of
  7.  * the License, or (at your option) any later version.
  8.  *
  9.  * This software is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12.  * Lesser General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU Lesser General Public
  15.  * License along with this software; if not, write to the Free
  16.  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  17.  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  18.  */
  19. package org.exoplatform.platform.upgrade.plugins;

  20. import org.exoplatform.commons.upgrade.UpgradeProductPlugin;
  21. import org.exoplatform.commons.utils.LazyPageList;
  22. import org.exoplatform.commons.version.util.VersionComparator;
  23. import org.exoplatform.container.PortalContainer;
  24. import org.exoplatform.container.component.RequestLifeCycle;
  25. import org.exoplatform.container.xml.InitParams;
  26. import org.exoplatform.portal.config.DataStorage;
  27. import org.exoplatform.portal.config.Query;
  28. import org.exoplatform.services.log.ExoLogger;
  29. import org.exoplatform.services.log.Log;
  30. import org.exoplatform.portal.config.model.PortalConfig;

  31. public class UpgradeGroupSiteLayoutPlugin extends UpgradeProductPlugin {

  32.     private static final Log LOG = ExoLogger.getLogger(UpgradeGroupSiteLayoutPlugin.class);

  33.     private static final String GROUP_SITE_TEMPLATE_NAME = "group.site.template.name";

  34.     private static final String GROUP_SITE_TEMPLATE_LOACTION = "group.site.template.location";

  35.     private DataStorage dataStorage_;

  36.     protected String groupSiteTemplateName;

  37.     protected String groupSiteTemplateLocation;

  38.     public UpgradeGroupSiteLayoutPlugin (DataStorage dataStorage, InitParams initParams) {
  39.         super(initParams);
  40.         dataStorage_ = dataStorage;
  41.         groupSiteTemplateName = initParams.getValueParam(GROUP_SITE_TEMPLATE_NAME).getValue();
  42.         groupSiteTemplateLocation = initParams.getValueParam(GROUP_SITE_TEMPLATE_LOACTION).getValue();
  43.     }
  44.     @Override
  45.     public void processUpgrade(String oldVersion, String newVersion) {

  46.         RequestLifeCycle.begin(PortalContainer.getInstance());
  47.         try {

  48.             Query<PortalConfig> query = new Query<PortalConfig>(groupSiteTemplateName, null, PortalConfig.class);

  49.             LazyPageList<PortalConfig> usersPortalConfig = dataStorage_.find(query);

  50.             if (usersPortalConfig == null) {
  51.                 LOG.info("No Group Site was found, no upgrade operation will be done.");
  52.                 return;

  53.             }
  54.             // Get new portal config from template
  55.             PortalConfig tempPortalConfig = PlatformUpgradeUtils.getPortalConfigFromTemplate(PortalConfig.GROUP_TYPE,groupSiteTemplateName,groupSiteTemplateLocation);

  56.             // Get old portalConfig stored in the JCR
  57.             for (PortalConfig userPortalConfig : usersPortalConfig.getAll()) {
  58.                 LOG.info("Proceed group site layout migration: " + userPortalConfig.getName());

  59.                 PortalConfig newPortalConfig = new PortalConfig(PortalConfig.GROUP_TYPE,userPortalConfig.getName(),userPortalConfig.getStorageId());
  60.                 // Merge data from Old PortalConfig to new PortalConfig (Set storage name and storage id from old to new portal config)
  61.                 newPortalConfig.setStorageName(userPortalConfig.getStorageName());
  62.                 newPortalConfig.setName(userPortalConfig.getName());
  63.                 newPortalConfig.setAccessPermissions(userPortalConfig.getAccessPermissions());
  64.                 newPortalConfig.setDescription(userPortalConfig.getDescription());
  65.                 newPortalConfig.setEditPermission(userPortalConfig.getEditPermission());
  66.                 newPortalConfig.setLabel(userPortalConfig.getLabel());
  67.                 newPortalConfig.setModifiable(userPortalConfig.isModifiable());
  68.                 newPortalConfig.setPortalLayout(tempPortalConfig.getPortalLayout());
  69.                 newPortalConfig.setPortalRedirects(userPortalConfig.getPortalRedirects());
  70.                 newPortalConfig.setType(userPortalConfig.getType());

  71.                 // datastorage.save(new built portal config)
  72.                 dataStorage_.save(newPortalConfig);
  73.             }
  74.         } catch (Exception e) {
  75.             LOG.error("Error during Group Layout migration : " + e.getMessage(), e);
  76.         } finally {
  77.             RequestLifeCycle.end();
  78.         }
  79.     }

  80.     @Override
  81.     public boolean shouldProceedToUpgrade(String newVersion, String previousVersion) {
  82.         // --- return true anly for the first version of platform
  83.         return VersionComparator.isAfter(newVersion, previousVersion);
  84.     }
  85. }