committed by
Andrew Shvayka
8 changed files with 225 additions and 76 deletions
@ -0,0 +1,55 @@ |
|||
package org.thingsboard.client.tools.migrator; |
|||
|
|||
import org.apache.commons.io.FileUtils; |
|||
import org.apache.commons.io.LineIterator; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
|
|||
import java.io.File; |
|||
import java.io.IOException; |
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
public class DictionaryParser { |
|||
private Map<String, String> dictionaryParsed = new HashMap<>(); |
|||
|
|||
public DictionaryParser(File sourceFile) throws IOException { |
|||
parseDictionaryDump(FileUtils.lineIterator(sourceFile)); |
|||
} |
|||
|
|||
public String getKeyByKeyId(String keyId) { |
|||
return dictionaryParsed.get(keyId); |
|||
} |
|||
|
|||
private boolean isBlockFinished(String line) { |
|||
return StringUtils.isBlank(line) || line.equals("\\."); |
|||
} |
|||
|
|||
private boolean isBlockStarted(String line) { |
|||
return line.startsWith("COPY public.ts_kv_dictionary ("); |
|||
} |
|||
|
|||
private void parseDictionaryDump(LineIterator iterator) { |
|||
String tempLine; |
|||
while(iterator.hasNext()) { |
|||
tempLine = iterator.nextLine(); |
|||
|
|||
if(isBlockStarted(tempLine)) { |
|||
processBlock(iterator); |
|||
} |
|||
} |
|||
} |
|||
|
|||
private void processBlock(LineIterator lineIterator) { |
|||
String tempLine; |
|||
String[] lineSplited; |
|||
while(lineIterator.hasNext()) { |
|||
tempLine = lineIterator.nextLine(); |
|||
if(isBlockFinished(tempLine)) { |
|||
return; |
|||
} |
|||
|
|||
lineSplited = tempLine.split("\t"); |
|||
dictionaryParsed.put(lineSplited[1], lineSplited[0]); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,76 @@ |
|||
package org.thingsboard.client.tools.migrator; |
|||
|
|||
import org.apache.commons.io.FileUtils; |
|||
import org.apache.commons.io.LineIterator; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
|
|||
import java.io.File; |
|||
import java.io.IOException; |
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
public class RelatedEntitiesParser { |
|||
private final Map<String, String> allEntityIdsAndTypes = new HashMap<>(); |
|||
|
|||
public RelatedEntitiesParser(File source) throws IOException { |
|||
processAllTables(FileUtils.lineIterator(source)); |
|||
} |
|||
|
|||
public String getEntityType(String uuid) { |
|||
return this.allEntityIdsAndTypes.get(uuid); |
|||
} |
|||
|
|||
private boolean isBlockFinished(String line) { |
|||
return StringUtils.isBlank(line) || line.equals("\\."); |
|||
} |
|||
|
|||
private void processAllTables(LineIterator lineIterator) { |
|||
String currentLine; |
|||
while(lineIterator.hasNext()) { |
|||
currentLine = lineIterator.nextLine(); |
|||
if(currentLine.startsWith("COPY public.alarm")) { |
|||
processBlock(lineIterator, EntityType.ALARM); |
|||
} else if (currentLine.startsWith("COPY public.asset")) { |
|||
processBlock(lineIterator, EntityType.ASSET); |
|||
} else if (currentLine.startsWith("COPY public.customer")) { |
|||
processBlock(lineIterator, EntityType.CUSTOMER); |
|||
} else if (currentLine.startsWith("COPY public.dashboard")) { |
|||
processBlock(lineIterator, EntityType.DASHBOARD); |
|||
} else if (currentLine.startsWith("COPY public.device")) { |
|||
processBlock(lineIterator, EntityType.DEVICE); |
|||
} else if (currentLine.startsWith("COPY public.rule_chain")) { |
|||
processBlock(lineIterator, EntityType.RULE_CHAIN); |
|||
} else if (currentLine.startsWith("COPY public.rule_node")) { |
|||
processBlock(lineIterator, EntityType.RULE_NODE); |
|||
} else if (currentLine.startsWith("COPY public.tenant")) { |
|||
processBlock(lineIterator, EntityType.TENANT); |
|||
} else if (currentLine.startsWith("COPY public.tb_user")) { |
|||
processBlock(lineIterator, EntityType.USER); |
|||
} else if (currentLine.startsWith("COPY public.entity_view")) { |
|||
processBlock(lineIterator, EntityType.ENTITY_VIEW); |
|||
} else if (currentLine.startsWith("COPY public.widgets_bundle")) { |
|||
processBlock(lineIterator, EntityType.WIDGETS_BUNDLE); |
|||
} else if (currentLine.startsWith("COPY public.widget_type")) { |
|||
processBlock(lineIterator, EntityType.WIDGET_TYPE); |
|||
} else if (currentLine.startsWith("COPY public.tenant_profile")) { |
|||
processBlock(lineIterator, EntityType.TENANT_PROFILE); |
|||
} else if (currentLine.startsWith("COPY public.device_profile")) { |
|||
processBlock(lineIterator, EntityType.DEVICE_PROFILE); |
|||
} else if (currentLine.startsWith("COPY public.api_usage_state")) { |
|||
processBlock(lineIterator, EntityType.API_USAGE_STATE); |
|||
} |
|||
} |
|||
} |
|||
|
|||
private void processBlock(LineIterator lineIterator, EntityType entityType) { |
|||
String currentLine; |
|||
while(lineIterator.hasNext()) { |
|||
currentLine = lineIterator.nextLine(); |
|||
if(isBlockFinished(currentLine)) { |
|||
return; |
|||
} |
|||
allEntityIdsAndTypes.put(currentLine.split("\t")[0], entityType.name()); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue