Skip to content

Commit

Permalink
fix: move image storage location of News & Notes- EXO-64344
Browse files Browse the repository at this point in the history
  • Loading branch information
ahamdi committed Oct 5, 2023
1 parent 1bca142 commit 30c863c
Show file tree
Hide file tree
Showing 5 changed files with 441 additions and 0 deletions.
52 changes: 52 additions & 0 deletions data-upgrade-move-folders/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!-- Copyright (C) 2023 eXo Platform SAS. 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. -->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.exoplatform.addons.upgrade</groupId>
<artifactId>upgrade</artifactId>
<version>6.5.x-maintenance-SNAPSHOT</version>
</parent>

<artifactId>data-upgrade-move-folders</artifactId>
<packaging>jar</packaging>
<name>eXo Add-on:: Data Upgrade Add-on - Move Folders</name>

<properties>
<exo.test.coverage.ratio>0.0</exo.test.coverage.ratio>
</properties>

<dependencies>
<dependency>
<groupId>org.exoplatform.commons</groupId>
<artifactId>commons-component-upgrade</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.exoplatform.jcr</groupId>
<artifactId>exo.jcr.component.core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.exoplatform.jcr</groupId>
<artifactId>exo.jcr.component.ext</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.exoplatform.ecms</groupId>
<artifactId>ecms-core-services</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright (C) 2003-2023 eXo Platform SAS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <gnu.org/licenses>.
*/
package org.exoplatform.jcr.upgrade;

import javax.jcr.Item;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.apache.commons.lang3.StringUtils;
import org.exoplatform.commons.upgrade.UpgradeProductPlugin;
import org.exoplatform.commons.utils.ListAccess;
import org.exoplatform.container.PortalContainer;
import org.exoplatform.container.component.RequestLifeCycle;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.services.jcr.RepositoryService;
import org.exoplatform.services.jcr.ext.app.SessionProviderService;
import org.exoplatform.services.jcr.ext.common.SessionProvider;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.social.core.space.model.Space;
import org.exoplatform.social.core.space.spi.SpaceService;

import java.util.ArrayList;
import java.util.List;

/**
* plugin will be executed in order to move folders under spaces drives
* from an original path to a destination path as provided in the configuration
*/
public class MoveNodesUpgradePlugin extends UpgradeProductPlugin {

private static final Log log = ExoLogger.getLogger(MoveNodesUpgradePlugin.class.getName());

private static final String ORIGIN_PATH = "origin-folder-path";

private static final String DESTINATION_PATH = "destination-folder-path";
private static final String FOLDERS_TO_REMOVE = "folders-to-remove";

private static final int SPACES_PAGE_SIZE = 2;

private final SpaceService spaceService;

private RepositoryService repositoryService;

private SessionProviderService sessionProviderService;

private String originPath;

private String destinationPath;

private List<String> foldersToRemove = new ArrayList<>();


public MoveNodesUpgradePlugin(InitParams initParams,
SpaceService spaceService,
RepositoryService repositoryService,
SessionProviderService sessionProviderService) {
super(initParams);
if(initParams.getValueParam(ORIGIN_PATH) != null && StringUtils.isNotBlank(initParams.getValueParam(ORIGIN_PATH).getValue())) {
this.originPath = initParams.getValueParam(ORIGIN_PATH).getValue();
}
if(initParams.getValueParam(ORIGIN_PATH) != null && StringUtils.isNotBlank(initParams.getValueParam(ORIGIN_PATH).getValue())) {
this.destinationPath = initParams.getValueParam(DESTINATION_PATH).getValue();
}
if(initParams.getValuesParam(FOLDERS_TO_REMOVE) != null && !initParams.getValuesParam(FOLDERS_TO_REMOVE).getValues().isEmpty()) {
this.foldersToRemove = initParams.getValuesParam(FOLDERS_TO_REMOVE).getValues();
}

this.spaceService = spaceService;
this.repositoryService = repositoryService;
this.sessionProviderService = sessionProviderService;
}

@Override
public void processUpgrade(String oldVersion, String newVersion) {
if(StringUtils.isBlank(originPath) || StringUtils.isBlank(destinationPath)) {
log.warn("Invalid parameter was provided for {}, this upgrade plugin will be ignored", StringUtils.isBlank(originPath) ? "'Origin path'":"'Destination path'");
return;
}
long startupTime = System.currentTimeMillis();
int movedFoldersCount = 0;
log.info("Start upgrade : Moving of folder from {} to {}", originPath, destinationPath);

SessionProvider sessionProvider = null;
RequestLifeCycle.begin(PortalContainer.getInstance());
try {
sessionProvider = sessionProviderService.getSystemSessionProvider(null);
Session session = sessionProvider.getSession(
repositoryService.getCurrentRepository()
.getConfiguration()
.getDefaultWorkspaceName(),
repositoryService.getCurrentRepository());
ListAccess<Space> spaces = spaceService.getAllSpacesWithListAccess();
int index = 0;
while(index <= spaces.getSize()) {
Space[] spaceArray = spaces.load(index, SPACES_PAGE_SIZE);
for (Space space : spaceArray) {
String originFolderPath = "/Groups" + space.getGroupId() + originPath;
String destinationFolderPath = "/Groups" + space.getGroupId() + destinationPath;
Item originFolderNode = session.getItem(originFolderPath);
if(originFolderNode != null) {
session.move(originFolderPath, destinationFolderPath);
movedFoldersCount ++;
}
// remove unnecessary folders if defined in init params
if(!foldersToRemove.isEmpty()) {
for(String folderToRemove : foldersToRemove) {
folderToRemove = "/Groups" + space.getGroupId() + folderToRemove;
try {
Item folderToRemoveNode = session.getItem(folderToRemove);
if (folderToRemoveNode != null) {
folderToRemoveNode.remove();
}
} catch (RepositoryException re) {
log.warn("Folder {} to delete was not found, ignoring it", folderToRemove, re);
}
}
}
}

session.save();
index = index + SPACES_PAGE_SIZE;
}

log.info("End Moving of '{}' folders. It took {} ms",
movedFoldersCount,
(System.currentTimeMillis() - startupTime));
} catch (Exception e) {
if (log.isErrorEnabled()) {
log.error("An unexpected error occurs when moving folders:", e);
}
} finally {
if (sessionProvider != null) {
sessionProvider.close();
}
RequestLifeCycle.end();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2023 eXo Platform SAS.
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.
-->


<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.exoplatform.org/xml/ns/kernel_1_2.xsd http://www.exoplatform.org/xml/ns/kernel_1_2.xsd"
xmlns="http://www.exoplatform.org/xml/ns/kernel_1_2.xsd">

<external-component-plugins>
<target-component>org.exoplatform.commons.upgrade.UpgradeProductService</target-component>
<component-plugin profiles="news">
<name>MoveNewsFolderToSpaceRoot</name>
<set-method>addUpgradePlugin</set-method>
<type>org.exoplatform.jcr.upgrade.MoveNodesUpgradePlugin</type>
<description>Move images attached to News to a new location</description>
<init-params>
<value-param>
<name>product.group.id</name>
<description>The groupId of the product</description>
<value>org.exoplatform.news</value>
</value-param>
<value-param>
<name>plugin.execution.order</name>
<description>The plugin execution order</description>
<value>1</value>
</value-param>
<value-param>
<name>plugin.upgrade.execute.once</name>
<description>Execute this upgrade plugin only once</description>
<value>true</value>
</value-param>
<value-param>
<name>plugin.upgrade.async.execution</name>
<description>Execute this upgrade asynchronously</description>
<value>true</value>
</value-param>
<value-param>
<name>plugin.upgrade.target.version</name>
<description>Target version of the plugin</description>
<value>6.5.0</value>
</value-param>
<!-- Configuration parameters specific to the current upgrade plugin -->
<value-param>
<name>origin-folder-path</name>
<description>Origin folder path</description>
<value>/Documents/news/images</value>
</value-param>
<value-param>
<name>destination-folder-path</name>
<description>Destination parent folder path</description>
<value>/News/images</value>
</value-param>
<values-param>
<name>folders-to-remove</name>
<value>/Documents/news</value>
</values-param>
</init-params>
</component-plugin>
<component-plugin profiles="news">
<name>MoveNotesFolderToSpaceRoot</name>
<set-method>addUpgradePlugin</set-method>
<type>org.exoplatform.jcr.upgrade.MoveNodesUpgradePlugin</type>
<description>Move images attached to notes to a new location</description>
<init-params>
<value-param>
<name>product.group.id</name>
<description>The groupId of the product</description>
<value>org.meeds-io.notes</value>
</value-param>
<value-param>
<name>plugin.execution.order</name>
<description>The plugin execution order</description>
<value>1</value>
</value-param>
<value-param>
<name>plugin.upgrade.execute.once</name>
<description>Execute this upgrade plugin only once</description>
<value>true</value>
</value-param>
<value-param>
<name>plugin.upgrade.async.execution</name>
<description>Execute this upgrade asynchronously</description>
<value>true</value>
</value-param>
<value-param>
<name>plugin.upgrade.target.version</name>
<description>Target version of the plugin</description>
<value>6.5.0</value>
</value-param>
<!-- Configuration parameters specific to the current upgrade plugin -->
<value-param>
<name>origin-folder-path</name>
<description>Origin folder path</description>
<value>/Documents/notes</value>
</value-param>
<value-param>
<name>destination-folder-path</name>
<description>Destination parent folder path</description>
<value>/notes</value>
</value-param>
</init-params>
</component-plugin>

</external-component-plugins>
</configuration>

Loading

0 comments on commit 30c863c

Please sign in to comment.