-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#30296: Creation of upgrade task to replace languages portlets by lo…
…cales ones. (#30340)
- Loading branch information
1 parent
167058a
commit 11a47ce
Showing
5 changed files
with
220 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...n/java/com/dotmarketing/startup/runonce/Task241015ReplaceLanguagesWithLocalesPortlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.dotmarketing.startup.runonce; | ||
|
||
import com.dotmarketing.business.CacheLocator; | ||
import com.dotmarketing.common.db.DotConnect; | ||
import com.dotmarketing.exception.DotDataException; | ||
import com.dotmarketing.exception.DotRuntimeException; | ||
import com.dotmarketing.startup.StartupTask; | ||
import com.dotmarketing.util.UUIDUtil; | ||
import io.vavr.control.Try; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Adds the dotAI portlet to all layouts which have API Playground portlet too, if it does not already exist. | ||
* @author vico | ||
*/ | ||
public class Task241015ReplaceLanguagesWithLocalesPortlet implements StartupTask { | ||
|
||
public static final String LANGUAGES_PORTLET_ID = "languages"; | ||
public static final String LOCALES_PORTLET_ID = "locales"; | ||
public static final String WORKFLOW_PORTLET_ID = "workflow-schemes"; | ||
|
||
/** | ||
* Determines if the task should be forced to run. | ||
* | ||
* @return true if no locales portlet is found in any layout, false otherwise. | ||
*/ | ||
@Override | ||
public boolean forceRun() { | ||
// if no locales found then flag it as true | ||
return getLayoutPortletsByPortletId(LOCALES_PORTLET_ID).isEmpty(); | ||
} | ||
|
||
/** | ||
* Executes the upgrade task. | ||
* Adds the locales portlet to layouts containing the languages portlet or the workflow portlet. | ||
* Replaces the languages portlet with the locales portlet. | ||
* Clears the layout cache. | ||
* | ||
* @throws DotDataException if there is an error accessing the database. | ||
* @throws DotRuntimeException if there is a runtime error during execution. | ||
*/ | ||
@Override | ||
public void executeUpgrade() throws DotDataException, DotRuntimeException { | ||
final List<Map<String, Object>> languagesLayoutPortlets = getLayoutPortletsByPortletId(LANGUAGES_PORTLET_ID); | ||
|
||
if (languagesLayoutPortlets.isEmpty()) { | ||
final List<Map<String, Object>> workflowLayoutPortlets = getLayoutPortletsByPortletId(WORKFLOW_PORTLET_ID); | ||
if (!workflowLayoutPortlets.isEmpty()) { | ||
final Map<String, Object> row = workflowLayoutPortlets.get(0); | ||
final String layoutId = (String) row.get("layout_id"); | ||
final int portletOrder = Optional.ofNullable((Integer) row.get("portlet_order")).orElse(0) + 1; | ||
insertLocalesPortlet(layoutId, portletOrder); | ||
} | ||
} else { | ||
languagesLayoutPortlets.forEach(this::replaceLanguage); | ||
} | ||
|
||
CacheLocator.getLayoutCache().clearCache(); | ||
} | ||
|
||
private void replaceLanguage(final Map<String, Object> row) { | ||
final String layoutId = (String) row.get("layout_id"); | ||
final int portletOrder = Optional.ofNullable((Integer) row.get("portlet_order")).orElse(1); | ||
insertLocalesPortlet(layoutId, portletOrder); | ||
|
||
final String id = (String) row.get("id"); | ||
deleteLanguagesPortlet(id); | ||
} | ||
|
||
private void deleteLanguagesPortlet(final String id) { | ||
Try.run(() -> new DotConnect() | ||
.executeStatement(String.format("DELETE FROM cms_layouts_portlets WHERE id = '%s'", id))) | ||
.getOrElseThrow(ex -> new RuntimeException(ex)); | ||
} | ||
|
||
private static void insertLocalesPortlet(final String layoutId, final int portletOrder) { | ||
Try.run(() -> new DotConnect() | ||
.setSQL("INSERT INTO cms_layouts_portlets(id, layout_id, portlet_id, portlet_order)" + | ||
" VALUES (?, ?, ?, ?)") | ||
.addParam(UUIDUtil.uuid()) | ||
.addParam(layoutId) | ||
.addParam(LOCALES_PORTLET_ID) | ||
.addParam(portletOrder) | ||
.loadResult()) | ||
.getOrElseThrow(ex -> new RuntimeException(ex)); | ||
} | ||
|
||
private List<Map<String, Object>> getLayoutPortletsByPortletId(final String portletId) { | ||
return Try.of( | ||
() -> new DotConnect() | ||
.setSQL("SELECT * FROM cms_layouts_portlets WHERE portlet_id = ?") | ||
.addParam(portletId) | ||
.loadObjectResults()) | ||
.getOrElse(List.of()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...va/com/dotmarketing/startup/runonce/Task241015ReplaceLanguagesWithLocalesPortletTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package com.dotmarketing.startup.runonce; | ||
|
||
import com.dotcms.util.IntegrationTestInitService; | ||
import com.dotmarketing.common.db.DotConnect; | ||
import com.dotmarketing.exception.DotDataException; | ||
import com.dotmarketing.util.UUIDUtil; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import java.sql.SQLException; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class Task241015ReplaceLanguagesWithLocalesPortletTest { | ||
|
||
@BeforeClass | ||
public static void prepare() throws Exception { | ||
// Setting web app environment | ||
IntegrationTestInitService.getInstance().init(); | ||
} | ||
|
||
/** | ||
* Given the locales portlets are inserted | ||
* When the upgrade task is executed | ||
* Then the locales portlets should be added to the layout | ||
* And the task should not need to be forced to run | ||
* | ||
* @throws SQLException if there is an SQL error | ||
* @throws DotDataException if there is a data access error | ||
*/ | ||
@Test | ||
public void test_upgradeTask_success() throws SQLException, DotDataException { | ||
final DotConnect dotConnect = new DotConnect(); | ||
final Task241015ReplaceLanguagesWithLocalesPortlet task = new Task241015ReplaceLanguagesWithLocalesPortlet(); | ||
|
||
insertLocalesPortlets(dotConnect); | ||
assertFalse(task.forceRun()); | ||
|
||
deleteAnyLocalesPortlets(dotConnect); | ||
assertTrue(task.forceRun()); | ||
|
||
task.executeUpgrade(); | ||
assertFalse(task.forceRun()); | ||
} | ||
|
||
/** | ||
* Scenario: Upgrade task execution when no languages portlets are present | ||
* | ||
* Given the locales portlets are not present | ||
* When the upgrade task is executed | ||
* Then the locales portlets should be added to the layout | ||
* And the task should not need to be forced to run | ||
* | ||
* @throws SQLException if there is an SQL error | ||
* @throws DotDataException if there is a data access error | ||
*/ | ||
@Test | ||
public void test_upgradeTask_noLanguages_success() throws SQLException, DotDataException { | ||
final DotConnect dotConnect = new DotConnect(); | ||
final Task241015ReplaceLanguagesWithLocalesPortlet task = new Task241015ReplaceLanguagesWithLocalesPortlet(); | ||
|
||
assertFalse(task.forceRun()); | ||
|
||
deleteAnyLanguagesPortlets(dotConnect); | ||
deleteAnyLocalesPortlets(dotConnect); | ||
assertTrue(task.forceRun()); | ||
|
||
task.executeUpgrade(); | ||
assertFalse(task.forceRun()); | ||
} | ||
|
||
private void insertLocalesPortlets(final DotConnect dotConnect) throws DotDataException { | ||
final List<Map<String, Object>> rows = dotConnect | ||
.setSQL("SELECT layout_id, portlet_order" + | ||
" FROM cms_layouts_portlets" + | ||
" WHERE portlet_id = ?" + | ||
" ORDER BY portlet_order" + | ||
" DESC LIMIT 1") | ||
.addParam(Task241015ReplaceLanguagesWithLocalesPortlet.LANGUAGES_PORTLET_ID) | ||
.loadObjectResults(); | ||
if (rows.isEmpty()) { | ||
return; | ||
} | ||
|
||
final Map<String, Object> row = rows.get(0); | ||
dotConnect | ||
.setSQL("INSERT INTO cms_layouts_portlets(id, layout_id, portlet_id, portlet_order)" + | ||
" VALUES(?, ?, ?, ?)") | ||
.addParam(UUIDUtil.uuid()) | ||
.addParam(row.get("layout_id")) | ||
.addParam(Task241015ReplaceLanguagesWithLocalesPortlet.LOCALES_PORTLET_ID) | ||
.addParam(((Integer) row.get("portlet_order")) + 1) | ||
.loadResult(); | ||
} | ||
|
||
private void deletePortlets(final DotConnect dotConnect, final String portletId) throws SQLException { | ||
dotConnect | ||
.executeStatement(String.format( | ||
"DELETE FROM cms_layouts_portlets WHERE portlet_id = '%s'", | ||
portletId)); | ||
} | ||
|
||
private void deleteAnyLocalesPortlets(final DotConnect dotConnect) throws SQLException { | ||
deletePortlets(dotConnect, Task241015ReplaceLanguagesWithLocalesPortlet.LOCALES_PORTLET_ID); | ||
} | ||
|
||
private void deleteAnyLanguagesPortlets(final DotConnect dotConnect) throws SQLException { | ||
deletePortlets(dotConnect, Task241015ReplaceLanguagesWithLocalesPortlet.LANGUAGES_PORTLET_ID); | ||
} | ||
|
||
} |