use of com.fasterxml.jackson.core.type.TypeReference in project PublicCMS-preview by sanluan.
the class AbstractCmsUpgrader method updateModelToFile.
protected void updateModelToFile(Connection connection) throws SQLException {
try (Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from cms_model")) {
while (rs.next()) {
CmsModel entity = new CmsModel();
String filePath = CommonConstants.CMS_FILEPATH + SEPARATOR + SiteComponent.TEMPLATE_PATH + SEPARATOR + SiteComponent.SITE_PATH_PREFIX + rs.getString("site_id") + SEPARATOR + SiteComponent.MODEL_FILE;
File file = new File(filePath);
file.getParentFile().mkdirs();
Map<String, CmsModel> modelMap;
try {
modelMap = objectMapper.readValue(file, new TypeReference<Map<String, CmsModel>>() {
});
} catch (IOException | ClassCastException e) {
modelMap = new HashMap<>();
}
entity.setId(rs.getString("id"));
entity.setHasChild(rs.getBoolean("has_child"));
entity.setHasFiles(rs.getBoolean("has_files"));
entity.setHasImages(rs.getBoolean("has_images"));
entity.setName(rs.getString("name"));
entity.setOnlyUrl(rs.getBoolean("only_url"));
if (null != rs.getString("parent_id")) {
entity.setParentId(String.valueOf(rs.getString("parent_id")));
}
entity.setTemplatePath((String) rs.getString("template_path"));
if (null != rs.getString("extend_id")) {
List<ExtendField> extendList = new ArrayList<>();
try (Statement extendFieldStatement = connection.createStatement();
ResultSet extendFieldRs = extendFieldStatement.executeQuery("select * from sys_extend_field where extend_id = " + rs.getString("extend_id"))) {
while (extendFieldRs.next()) {
ExtendField e = new ExtendField(extendFieldRs.getString("code"), extendFieldRs.getString("input_type"), extendFieldRs.getBoolean("required"), extendFieldRs.getString("name"), extendFieldRs.getString("description"), extendFieldRs.getString("default_value"));
extendList.add(e);
}
}
entity.setExtendList(extendList);
}
modelMap.put(entity.getId(), entity);
try (FileOutputStream outputStream = new FileOutputStream(file)) {
objectMapper.writeValue(outputStream, modelMap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
use of com.fasterxml.jackson.core.type.TypeReference in project PublicCMS-preview by sanluan.
the class ModelComponent method getMap.
/**
* @param site
* @return model map
*/
public Map<String, CmsModel> getMap(SysSite site) {
Map<String, CmsModel> modelMap = modelCache.get(site.getId());
if (CommonUtils.empty(modelMap)) {
File file = new File(siteComponent.getModelFilePath(site));
if (CommonUtils.notEmpty(file)) {
try {
modelMap = objectMapper.readValue(file, new TypeReference<Map<String, CmsModel>>() {
});
} catch (IOException | ClassCastException e) {
modelMap = new HashMap<>();
}
} else {
modelMap = new HashMap<>();
}
modelCache.put(site.getId(), modelMap);
}
return modelMap;
}
use of com.fasterxml.jackson.core.type.TypeReference in project PublicCMS-preview by sanluan.
the class MetadataComponent method getTemplateMetadataMap.
/**
* 获取目录元数据
*
* @param dirPath
* @return template metadata map
*/
public Map<String, CmsPageMetadata> getTemplateMetadataMap(String dirPath) {
Map<String, CmsPageMetadata> metadataMap = pageCache.get(dirPath);
if (null == metadataMap) {
File file = new File(dirPath + SEPARATOR + METADATA_FILE);
if (CommonUtils.notEmpty(file)) {
try {
metadataMap = objectMapper.readValue(file, new TypeReference<Map<String, CmsPageMetadata>>() {
});
} catch (IOException | ClassCastException e) {
metadataMap = new HashMap<>();
}
} else {
metadataMap = new HashMap<>();
}
pageCache.put(dirPath, metadataMap);
}
return metadataMap;
}
use of com.fasterxml.jackson.core.type.TypeReference in project thingsboard by thingsboard.
the class BaseAssetControllerTest method testFindCustomerAssetsByType.
@Test
public void testFindCustomerAssetsByType() throws Exception {
Customer customer = new Customer();
customer.setTitle("Test customer");
customer = doPost("/api/customer", customer, Customer.class);
CustomerId customerId = customer.getId();
String title1 = "Asset title 1";
String type1 = "typeC";
List<Asset> assetsType1 = new ArrayList<>();
for (int i = 0; i < 125; i++) {
Asset asset = new Asset();
String suffix = RandomStringUtils.randomAlphanumeric(15);
String name = title1 + suffix;
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
asset.setName(name);
asset.setType(type1);
asset = doPost("/api/asset", asset, Asset.class);
assetsType1.add(doPost("/api/customer/" + customerId.getId().toString() + "/asset/" + asset.getId().getId().toString(), Asset.class));
}
String title2 = "Asset title 2";
String type2 = "typeD";
List<Asset> assetsType2 = new ArrayList<>();
for (int i = 0; i < 143; i++) {
Asset asset = new Asset();
String suffix = RandomStringUtils.randomAlphanumeric(15);
String name = title2 + suffix;
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
asset.setName(name);
asset.setType(type2);
asset = doPost("/api/asset", asset, Asset.class);
assetsType2.add(doPost("/api/customer/" + customerId.getId().toString() + "/asset/" + asset.getId().getId().toString(), Asset.class));
}
List<Asset> loadedAssetsType1 = new ArrayList<>();
TextPageLink pageLink = new TextPageLink(15);
TextPageData<Asset> pageData = null;
do {
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/assets?type={type}&", new TypeReference<TextPageData<Asset>>() {
}, pageLink, type1);
loadedAssetsType1.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageData.getNextPageLink();
}
} while (pageData.hasNext());
Collections.sort(assetsType1, idComparator);
Collections.sort(loadedAssetsType1, idComparator);
Assert.assertEquals(assetsType1, loadedAssetsType1);
List<Asset> loadedAssetsType2 = new ArrayList<>();
pageLink = new TextPageLink(4);
do {
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/assets?type={type}&", new TypeReference<TextPageData<Asset>>() {
}, pageLink, type2);
loadedAssetsType2.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageData.getNextPageLink();
}
} while (pageData.hasNext());
Collections.sort(assetsType2, idComparator);
Collections.sort(loadedAssetsType2, idComparator);
Assert.assertEquals(assetsType2, loadedAssetsType2);
for (Asset asset : loadedAssetsType1) {
doDelete("/api/customer/asset/" + asset.getId().getId().toString()).andExpect(status().isOk());
}
pageLink = new TextPageLink(4);
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/assets?type={type}&", new TypeReference<TextPageData<Asset>>() {
}, pageLink, type1);
Assert.assertFalse(pageData.hasNext());
Assert.assertEquals(0, pageData.getData().size());
for (Asset asset : loadedAssetsType2) {
doDelete("/api/customer/asset/" + asset.getId().getId().toString()).andExpect(status().isOk());
}
pageLink = new TextPageLink(4);
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/assets?type={type}&", new TypeReference<TextPageData<Asset>>() {
}, pageLink, type2);
Assert.assertFalse(pageData.hasNext());
Assert.assertEquals(0, pageData.getData().size());
}
use of com.fasterxml.jackson.core.type.TypeReference in project thingsboard by thingsboard.
the class BaseAssetControllerTest method testFindTenantAssetsByName.
@Test
public void testFindTenantAssetsByName() throws Exception {
String title1 = "Asset title 1";
List<Asset> assetsTitle1 = new ArrayList<>();
for (int i = 0; i < 143; i++) {
Asset asset = new Asset();
String suffix = RandomStringUtils.randomAlphanumeric(15);
String name = title1 + suffix;
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
asset.setName(name);
asset.setType("default");
assetsTitle1.add(doPost("/api/asset", asset, Asset.class));
}
String title2 = "Asset title 2";
List<Asset> assetsTitle2 = new ArrayList<>();
for (int i = 0; i < 75; i++) {
Asset asset = new Asset();
String suffix = RandomStringUtils.randomAlphanumeric(15);
String name = title2 + suffix;
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
asset.setName(name);
asset.setType("default");
assetsTitle2.add(doPost("/api/asset", asset, Asset.class));
}
List<Asset> loadedAssetsTitle1 = new ArrayList<>();
TextPageLink pageLink = new TextPageLink(15, title1);
TextPageData<Asset> pageData = null;
do {
pageData = doGetTypedWithPageLink("/api/tenant/assets?", new TypeReference<TextPageData<Asset>>() {
}, pageLink);
loadedAssetsTitle1.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageData.getNextPageLink();
}
} while (pageData.hasNext());
Collections.sort(assetsTitle1, idComparator);
Collections.sort(loadedAssetsTitle1, idComparator);
Assert.assertEquals(assetsTitle1, loadedAssetsTitle1);
List<Asset> loadedAssetsTitle2 = new ArrayList<>();
pageLink = new TextPageLink(4, title2);
do {
pageData = doGetTypedWithPageLink("/api/tenant/assets?", new TypeReference<TextPageData<Asset>>() {
}, pageLink);
loadedAssetsTitle2.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageData.getNextPageLink();
}
} while (pageData.hasNext());
Collections.sort(assetsTitle2, idComparator);
Collections.sort(loadedAssetsTitle2, idComparator);
Assert.assertEquals(assetsTitle2, loadedAssetsTitle2);
for (Asset asset : loadedAssetsTitle1) {
doDelete("/api/asset/" + asset.getId().getId().toString()).andExpect(status().isOk());
}
pageLink = new TextPageLink(4, title1);
pageData = doGetTypedWithPageLink("/api/tenant/assets?", new TypeReference<TextPageData<Asset>>() {
}, pageLink);
Assert.assertFalse(pageData.hasNext());
Assert.assertEquals(0, pageData.getData().size());
for (Asset asset : loadedAssetsTitle2) {
doDelete("/api/asset/" + asset.getId().getId().toString()).andExpect(status().isOk());
}
pageLink = new TextPageLink(4, title2);
pageData = doGetTypedWithPageLink("/api/tenant/assets?", new TypeReference<TextPageData<Asset>>() {
}, pageLink);
Assert.assertFalse(pageData.hasNext());
Assert.assertEquals(0, pageData.getData().size());
}
Aggregations