You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
4.7 KiB
120 lines
4.7 KiB
package com.fr.exit; |
|
|
|
import com.fr.config.dao.PropertiesConstants; |
|
import com.fr.design.DesignerEnvManager; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.stable.CommonUtils; |
|
import com.fr.stable.StableUtils; |
|
import com.fr.stable.project.ProjectConstants; |
|
import com.fr.workspace.WorkContext; |
|
import java.io.File; |
|
import java.io.FileOutputStream; |
|
import java.io.IOException; |
|
import java.io.OutputStreamWriter; |
|
import java.nio.charset.StandardCharsets; |
|
import java.sql.Blob; |
|
import java.sql.Connection; |
|
import java.sql.DriverManager; |
|
import java.sql.PreparedStatement; |
|
import java.sql.ResultSet; |
|
import java.sql.SQLException; |
|
import java.util.Properties; |
|
|
|
/** |
|
* 设计器关闭前的配置缓存一份到Properties |
|
* |
|
* @author hades |
|
* @version 11.0 |
|
* Created by hades on 2022/3/1 |
|
*/ |
|
public class ConfigToPropMigrator { |
|
|
|
private static final String SELECT_FOR_ENTITY = "select id, value from fine_conf_entity"; |
|
|
|
private static final String SELECT_FOR_CLASSNAME = "select id, classname from fine_conf_classname"; |
|
|
|
private static final String SELECT_FOR_XML_ENTITY = "select id, value from fine_conf_xmlentity"; |
|
|
|
private static final ConfigToPropMigrator INSTANCE = new ConfigToPropMigrator(); |
|
|
|
public static ConfigToPropMigrator getInstance() { |
|
return INSTANCE; |
|
} |
|
|
|
public void execute() { |
|
try { |
|
_execute(); |
|
} catch (Throwable throwable) { |
|
FineLoggerFactory.getLogger().warn(throwable.getMessage(), throwable); |
|
} |
|
} |
|
|
|
private void _execute() { |
|
if (WorkContext.getCurrent().isLocal()) { |
|
|
|
String url = "jdbc:hsqldb:file://" + WorkContext.getCurrent().getPath() + "/" + ProjectConstants.EMBED_DB_DIRECTORY + "/finedb/db;hsqldb.tx=mvcc"; |
|
|
|
try { |
|
Class.forName("com.fr.third.org.hsqldb.jdbcDriver"); |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
return ; |
|
} |
|
|
|
initDirectory(); |
|
|
|
try (Connection c = DriverManager.getConnection(url); |
|
OutputStreamWriter xmlEntityOut = new OutputStreamWriter(new FileOutputStream(PropertiesConstants.XML_ENTITY_PROP_PATH), StandardCharsets.UTF_8); |
|
OutputStreamWriter entityOut = new OutputStreamWriter(new FileOutputStream(PropertiesConstants.ENTITY_PROP_PATH), StandardCharsets.UTF_8); |
|
OutputStreamWriter classHelperOut = new OutputStreamWriter(new FileOutputStream(PropertiesConstants.CLASS_NAME_PROP_PATH), StandardCharsets.UTF_8)) { |
|
|
|
processClassOrEntity(c, new Properties(), SELECT_FOR_ENTITY, entityOut); |
|
processClassOrEntity(c, new Properties(), SELECT_FOR_CLASSNAME, classHelperOut); |
|
processXmlEntity(c, new Properties(), xmlEntityOut); |
|
DesignerEnvManager.getEnvManager().setPropertiesUsable(true); |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
deletePropertiesCache(); |
|
} |
|
} |
|
} |
|
|
|
private void initDirectory() { |
|
File directory = new File(StableUtils.pathJoin(WorkContext.getCurrent().getPath(), ProjectConstants.EMBED_DB_DIRECTORY, ProjectConstants.PROPERTIES_CACHE_FOR_CONFIG)); |
|
if (!directory.exists()) { |
|
directory.mkdir(); |
|
} |
|
} |
|
|
|
private void processClassOrEntity(Connection c, Properties map, String sql, OutputStreamWriter writer) throws SQLException, IOException { |
|
PreparedStatement query = c.prepareStatement(sql); |
|
ResultSet resultSet = query.executeQuery(); |
|
while (resultSet.next()) { |
|
String id = resultSet.getString(1); |
|
String value = resultSet.getString(2); |
|
if (id != null && value != null) { |
|
map.setProperty(id, value); |
|
} |
|
} |
|
map.store(writer, null); |
|
} |
|
|
|
private void processXmlEntity(Connection c, Properties map, OutputStreamWriter writer) throws SQLException, IOException { |
|
PreparedStatement query = c.prepareStatement(SELECT_FOR_XML_ENTITY); |
|
ResultSet resultSet = query.executeQuery(); |
|
while (resultSet.next()) { |
|
String id = resultSet.getString(1); |
|
Blob value = resultSet.getBlob(2); |
|
byte[] bytes = value.getBytes(1L, (int) value.length()); |
|
map.setProperty(id, new String(bytes, StandardCharsets.UTF_8)); |
|
} |
|
map.store(writer, null); |
|
} |
|
|
|
public void deletePropertiesCache() { |
|
CommonUtils.deleteFile(new File(PropertiesConstants.ENTITY_PROP_PATH)); |
|
CommonUtils.deleteFile(new File(PropertiesConstants.XML_ENTITY_PROP_PATH)); |
|
CommonUtils.deleteFile(new File(PropertiesConstants.CLASS_NAME_PROP_PATH)); |
|
} |
|
|
|
}
|
|
|