Skip to content

Commit

Permalink
Simplify migration: Add AbstractDoValueMigrationHandlerByMap
Browse files Browse the repository at this point in the history
403145
  • Loading branch information
matthiaso committed Jan 31, 2025
1 parent 6118c70 commit 82a71b1
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2023 BSI Business Systems Integration AG
* Copyright (c) 2010, 2025 BSI Business Systems Integration AG
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -50,6 +50,9 @@
import org.eclipse.scout.rt.dataobject.migration.fixture.house.RoomTypeFixtureStringId;
import org.eclipse.scout.rt.dataobject.migration.fixture.house.RoomTypesCollectionFixtureDo;
import org.eclipse.scout.rt.dataobject.migration.fixture.house.RoomTypesFixture;
import org.eclipse.scout.rt.dataobject.migration.fixture.house.StreetNameStringId;
import org.eclipse.scout.rt.dataobject.migration.fixture.house.StreetNameStringIdValueMigrationHandlerByMap;
import org.eclipse.scout.rt.dataobject.migration.fixture.house.StreetNameWrapperDo;
import org.eclipse.scout.rt.dataobject.migration.fixture.version.AlfaFixtureTypeVersions.AlfaFixture_3;
import org.eclipse.scout.rt.dataobject.migration.fixture.version.CharlieFixtureTypeVersions.CharlieFixture_2;
import org.eclipse.scout.rt.platform.BEANS;
Expand Down Expand Up @@ -92,7 +95,8 @@ public static void beforeClass() {
new CustomerGenderFixtureDoValueMigrationHandler_2(),
new HouseTypeFixtureDoValueMigrationHandler_2(),
new HouseFixtureDoValueMigrationHandler_1(),
new OldHouseTypeFixtureStringIdTypeNameRenameMigrationHandler_2()));
new OldHouseTypeFixtureStringIdTypeNameRenameMigrationHandler_2(),
new StreetNameStringIdValueMigrationHandlerByMap()));

TEST_BEANS.add(BEANS.get(BeanTestingHelper.class).registerBean(new BeanMetaData(TestDataObjectMigrationInventory.class, inventory).withReplace(true)));

Expand Down Expand Up @@ -630,4 +634,23 @@ public void testValueMigrationOrder() {
assertTrue(result.getDataObject().getId() instanceof HouseTypeFixtureStringId);
assertEquals(HouseTypesFixture.DETACHED_HOUSE, result.getDataObject().getId());
}

/**
* Test for {@link AbstractDoValueMigrationHandlerByMap}
*/
@Test
public void testDoValueMigrationHandlerByMap() {
StreetNameStringId mainStreet = StreetNameStringId.of("Main Street");
DataObjectMigratorResult<StreetNameWrapperDo> result = s_migrator.applyValueMigration(s_migrationContext, BEANS.get(StreetNameWrapperDo.class).withValue(mainStreet));
assertEquals(mainStreet, result.getDataObject().getValue());

result = s_migrator.applyValueMigration(s_migrationContext, BEANS.get(StreetNameWrapperDo.class).withValue(StreetNameStringId.of("Old Street")));
assertEquals(StreetNameStringId.of("New Street"), result.getDataObject().getValue());

result = s_migrator.applyValueMigration(s_migrationContext, BEANS.get(StreetNameWrapperDo.class).withValue(StreetNameStringId.of("Evergreen Street")));
assertEquals(StreetNameStringId.of("Evergreen Street"), result.getDataObject().getValue());

result = s_migrator.applyValueMigration(s_migrationContext, BEANS.get(StreetNameWrapperDo.class).withValue(StreetNameStringId.of("Evergreen Terrace")));
assertEquals(StreetNameStringId.of("West 57th Street"), result.getDataObject().getValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2010, 2025 BSI Business Systems Integration AG
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.scout.rt.dataobject.migration.fixture.house;

import org.eclipse.scout.rt.dataobject.id.AbstractStringId;
import org.eclipse.scout.rt.dataobject.id.IdTypeName;
import org.eclipse.scout.rt.platform.util.StringUtility;

@IdTypeName("charlieFixture.StreetNameStringId")
public final class StreetNameStringId extends AbstractStringId {
private static final long serialVersionUID = 1L;

private StreetNameStringId(String id) {
super(id);
}

public static StreetNameStringId of(String id) {
if (StringUtility.isNullOrEmpty(id)) {
return null;
}
return new StreetNameStringId(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2010, 2025 BSI Business Systems Integration AG
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.scout.rt.dataobject.migration.fixture.house;

import java.util.Map;

import org.eclipse.scout.rt.dataobject.ITypeVersion;
import org.eclipse.scout.rt.dataobject.migration.AbstractDoValueMigrationHandlerByMap;
import org.eclipse.scout.rt.dataobject.migration.DoValueMigrationId;
import org.eclipse.scout.rt.dataobject.migration.fixture.version.CharlieFixtureTypeVersions.CharlieFixture_1;
import org.eclipse.scout.rt.platform.IgnoreBean;

@IgnoreBean
public class StreetNameStringIdValueMigrationHandlerByMap extends AbstractDoValueMigrationHandlerByMap<StreetNameStringId> {

@Override
public DoValueMigrationId id() {
return DoValueMigrationId.of("f2ccdfaf-e6cf-42af-963f-30aecd761d23");
}

@Override
public Class<? extends ITypeVersion> typeVersionClass() {
return CharlieFixture_1.class;
}

@Override
protected Map<StreetNameStringId, StreetNameStringId> initMigrationMap() {
return Map.of(
StreetNameStringId.of("Old Street"), StreetNameStringId.of("New Street"),
StreetNameStringId.of("Evergreen Terrace"), StreetNameStringId.of("West 57th Street")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2010, 2025 BSI Business Systems Integration AG
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.scout.rt.dataobject.migration.fixture.house;

import jakarta.annotation.Generated;

import org.eclipse.scout.rt.dataobject.DoEntity;
import org.eclipse.scout.rt.dataobject.DoValue;
import org.eclipse.scout.rt.dataobject.TypeName;

/**
* Only used to wrap a {@link StreetNameStringId} for migration
*/
@TypeName("charlieFixture.StreetNameWrapper")
public class StreetNameWrapperDo extends DoEntity {

public DoValue<StreetNameStringId> value() {
return doValue("value");
}


/* **************************************************************************
* GENERATED CONVENIENCE METHODS
* *************************************************************************/

@Generated("DoConvenienceMethodsGenerator")
public StreetNameWrapperDo withValue(StreetNameStringId value) {
value().set(value);
return this;
}

@Generated("DoConvenienceMethodsGenerator")
public StreetNameStringId getValue() {
return value().get();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2010, 2025 BSI Business Systems Integration AG
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.scout.rt.dataobject.migration;

import java.util.Collections;
import java.util.Map;

/**
* Simple {@link AbstractDoValueMigrationHandler} migrating values from old to new using a {@link Map} storing all
* possible old to new mappings. The {@link #initMigrationMap()} method is called only once during object
* initialization.
*/
public abstract class AbstractDoValueMigrationHandlerByMap<T> extends AbstractDoValueMigrationHandler<T> {

private final Map<T, T> m_migrationMap = Collections.unmodifiableMap(initMigrationMap());

protected abstract Map<T, T> initMigrationMap();

protected Map<T, T> getMigrationMap() {
return m_migrationMap;
}

@Override
public T migrate(DataObjectMigrationContext ctx, T value) {
T migratedValue = getMigrationMap().get(value);
return migratedValue != null ? migratedValue : value;
}
}

0 comments on commit 82a71b1

Please sign in to comment.