Search in sources :

Example 76 with TypeReference

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();
            }
        }
    }
}
Also used : CmsModel(com.publiccms.views.pojo.entities.CmsModel) HashMap(java.util.HashMap) Statement(java.sql.Statement) ExtendField(com.publiccms.views.pojo.entities.ExtendField) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) ResultSet(java.sql.ResultSet) TypeReference(com.fasterxml.jackson.core.type.TypeReference) File(java.io.File)

Example 77 with TypeReference

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;
}
Also used : CmsModel(com.publiccms.views.pojo.entities.CmsModel) HashMap(java.util.HashMap) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException) File(java.io.File)

Example 78 with TypeReference

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;
}
Also used : CmsPageMetadata(com.publiccms.views.pojo.entities.CmsPageMetadata) HashMap(java.util.HashMap) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException) File(java.io.File)

Example 79 with TypeReference

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());
}
Also used : TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Customer(org.thingsboard.server.common.data.Customer) ArrayList(java.util.ArrayList) Asset(org.thingsboard.server.common.data.asset.Asset) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Matchers.containsString(org.hamcrest.Matchers.containsString) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Example 80 with TypeReference

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());
}
Also used : TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) ArrayList(java.util.ArrayList) Asset(org.thingsboard.server.common.data.asset.Asset) Matchers.containsString(org.hamcrest.Matchers.containsString) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Aggregations

TypeReference (com.fasterxml.jackson.core.type.TypeReference)316 IOException (java.io.IOException)130 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)113 Test (org.junit.Test)95 ArrayList (java.util.ArrayList)74 Map (java.util.Map)74 List (java.util.List)60 HashMap (java.util.HashMap)58 File (java.io.File)34 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)27 Collectors (java.util.stream.Collectors)25 InputStream (java.io.InputStream)23 JsonNode (com.fasterxml.jackson.databind.JsonNode)19 ImmutableMap (com.google.common.collect.ImmutableMap)19 lombok.val (lombok.val)18 Matchers.containsString (org.hamcrest.Matchers.containsString)17 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)14 Collections (java.util.Collections)13 ISE (org.apache.druid.java.util.common.ISE)12 URL (java.net.URL)10