forked from openhab/openhab-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
storage MapDB: add migration handling
* Migrate from Eclipse SmartHome without rule DTOs to Eclipse SmartHome with rule DTOs * Migrate from Eclipse SmartHome rules to openHAB Core rules Signed-off-by: Markus Rathgeb <maggu2810@gmail.com>
- Loading branch information
Showing
5 changed files
with
269 additions
and
0 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
35 changes: 35 additions & 0 deletions
35
.../main/java/org/eclipse/smarthome/storage/mapdb/internal/migration/MigrationException.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,35 @@ | ||
/** | ||
* Copyright (c) 2010-2019 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.smarthome.storage.mapdb.internal.migration; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
/** | ||
* Exception to identify a migration error. | ||
* | ||
* @author Markus Rathgeb - Initial contribution and API | ||
*/ | ||
@NonNullByDefault | ||
public class MigrationException extends Exception { | ||
private static final long serialVersionUID = 1L; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param cause the cause | ||
*/ | ||
public MigrationException(final Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
...rc/main/java/org/eclipse/smarthome/storage/mapdb/internal/migration/MigrationHandler.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,47 @@ | ||
/** | ||
* Copyright (c) 2010-2019 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.smarthome.storage.mapdb.internal.migration; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
/** | ||
* Definition of the {@code MigrationHandler} interface. | ||
* | ||
* @author Markus Rathgeb - Initial contribution and API | ||
*/ | ||
@NonNullByDefault | ||
public interface MigrationHandler { | ||
|
||
/** | ||
* Gets the old type name. | ||
* | ||
* @return old type name | ||
*/ | ||
String getTypeNameOld(); | ||
|
||
/** | ||
* Gets the new type name. | ||
* | ||
* @return new type name | ||
*/ | ||
String getTypeNameNew(); | ||
|
||
/** | ||
* Migrate the old data to the new one. | ||
* | ||
* @param value the old data | ||
* @return the new data | ||
* @throws MigrationException on error | ||
*/ | ||
String migrate(String value) throws MigrationException; | ||
} |
40 changes: 40 additions & 0 deletions
40
...g/eclipse/smarthome/storage/mapdb/internal/migration/RuleFromEshToOhMigrationHandler.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,40 @@ | ||
/** | ||
* Copyright (c) 2010-2019 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.smarthome.storage.mapdb.internal.migration; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
/** | ||
* Migration handler for rules. | ||
* | ||
* @author Markus Rathgeb - Initial contribution and API | ||
*/ | ||
@NonNullByDefault | ||
public class RuleFromEshToOhMigrationHandler implements MigrationHandler { | ||
|
||
@Override | ||
public String getTypeNameOld() { | ||
return "org.eclipse.smarthome.automation.dto.RuleDTO"; | ||
} | ||
|
||
@Override | ||
public String getTypeNameNew() { | ||
return "org.openhab.core.automation.dto.RuleDTO"; | ||
} | ||
|
||
@Override | ||
public String migrate(final String value) throws MigrationException { | ||
return value; | ||
} | ||
|
||
} |
108 changes: 108 additions & 0 deletions
108
...ain/java/org/eclipse/smarthome/storage/mapdb/internal/migration/RuleMigrationHandler.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,108 @@ | ||
/** | ||
* Copyright (c) 2010-2019 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.smarthome.storage.mapdb.internal.migration; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonSyntaxException; | ||
import com.google.gson.reflect.TypeToken; | ||
|
||
/** | ||
* Migration handler for rules. | ||
* | ||
* @author Markus Rathgeb - Initial contribution and API | ||
*/ | ||
@NonNullByDefault | ||
public class RuleMigrationHandler implements MigrationHandler { | ||
|
||
private final Type type = new TypeToken<Map<String, Object>>() { | ||
}.getType(); | ||
|
||
private final Gson mapper; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param mapper the Gson mapper | ||
*/ | ||
public RuleMigrationHandler(final Gson mapper) { | ||
this.mapper = mapper; | ||
} | ||
|
||
@Override | ||
public String getTypeNameOld() { | ||
return "org.eclipse.smarthome.automation.Rule"; | ||
} | ||
|
||
@Override | ||
public String getTypeNameNew() { | ||
return "org.eclipse.smarthome.automation.dto.RuleDTO"; | ||
} | ||
|
||
@Override | ||
public String migrate(final String value) throws MigrationException { | ||
try { | ||
final Map<String, Object> myMap = mapper.fromJson(value, type); | ||
if (myMap != null) { | ||
mergePropertiesIntoConfiguration(myMap); | ||
mergePropertiesIntoConfiguration(myMap.get("triggers")); | ||
mergePropertiesIntoConfiguration(myMap.get("actions")); | ||
mergePropertiesIntoConfiguration(myMap.get("conditions")); | ||
return mapper.toJson(myMap); | ||
} else { | ||
return value; | ||
} | ||
} catch (final JsonSyntaxException ex) { | ||
throw new MigrationException(ex); | ||
} | ||
} | ||
|
||
private void mergePropertiesIntoConfiguration(final @Nullable Object parentOfConfiguration) { | ||
if (parentOfConfiguration instanceof Map) { | ||
mergePropertiesIntoConfigurationOfMap((Map<?, ?>) parentOfConfiguration); | ||
} else if (parentOfConfiguration instanceof Collection) { | ||
mergePropertiesIntoConfigurationOfCollection((Collection<?>) parentOfConfiguration); | ||
} | ||
} | ||
|
||
private void mergePropertiesIntoConfigurationOfCollection(final Collection<?> parentOfConfigurations) { | ||
for (final Object parentOfConfiguration : parentOfConfigurations) { | ||
mergePropertiesIntoConfiguration(parentOfConfiguration); | ||
} | ||
} | ||
|
||
private void mergePropertiesIntoConfigurationOfMap(final Map<?, ?> parentOfConfiguration) { | ||
Object tmp; | ||
tmp = parentOfConfiguration.get("configuration"); | ||
if (tmp instanceof Map) { | ||
final Map<?, ?> cfg = (Map<?, ?>) tmp; | ||
tmp = cfg.remove("properties"); | ||
if (tmp instanceof Map) { | ||
final Map<?, ?> props = (Map<?, ?>) tmp; | ||
putAll(props, cfg); | ||
} | ||
} | ||
} | ||
|
||
@SuppressWarnings({ "unchecked", "rawtypes" }) | ||
private static void putAll(final Map<?, ?> source, final Map<?, ?> destination) { | ||
((Map) destination).putAll(source); | ||
} | ||
|
||
} |