| | |
| | | package com.moon.server.service.data; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.moon.server.entity.all.StaticData; |
| | | import com.moon.server.entity.ctrl.PubEntity; |
| | | import com.moon.server.entity.data.MetaPubEntity; |
| | | import com.moon.server.entity.data.PublishEntity; |
| | | import com.moon.server.entity.shujian.CreateLayerEntity; |
| | | import com.moon.server.entity.shujian.CreateServiceEntity; |
| | | import com.moon.server.entity.shujian.DeleteEntity; |
| | | import com.moon.server.entity.shujian.*; |
| | | import com.moon.server.helper.HttpHelper; |
| | | import com.moon.server.helper.RestHelper; |
| | | import com.moon.server.helper.StringHelper; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.http.HttpMethod; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.lang.reflect.Field; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 数简.服务 |
| | |
| | | */ |
| | | @Service |
| | | public class ShuJianService { |
| | | @Value("${shujian.ak}") |
| | | private String ak; |
| | | |
| | | @Value("${shujian.url}") |
| | | private String sjUrl; |
| | | |
| | | @Resource |
| | | PublishService publishService; |
| | | |
| | | private final static Log log = LogFactory.getLog(ShuJianService.class); |
| | | |
| | | /** |
| | | * 创建数简图层 |
| | | * 分页查询数简的颜色表 |
| | | */ |
| | | public void createShuJianLayer() { |
| | | public void selectSjColorTables(Integer pageSize, Integer pageIndex, HttpServletRequest req, HttpServletResponse res) { |
| | | try { |
| | | String url = String.format("%s/sj_raster/v6/api/color_table?%s&page_count=%d&page_num=%d&key=", sjUrl, ak, pageSize, pageIndex); |
| | | |
| | | HttpHelper httpHelper = new HttpHelper(); |
| | | httpHelper.service(req, res, null, url); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 插入数简服务 |
| | | */ |
| | | public Integer insertSjService(PubEntity pub) { |
| | | CreateLayerEntity layerEntity = createLayerEntity(pub); |
| | | Integer layerId = postLayerEntity(layerEntity); |
| | | if (null == layerId) { |
| | | return 0; |
| | | } |
| | | |
| | | CreateServiceEntity serviceEntity = createServiceEntity(pub); |
| | | Integer serviceId = postServiceEntity(serviceEntity); |
| | | if (null == serviceId) { |
| | | return 0; |
| | | } |
| | | |
| | | PublishEntity publishEntity = createPublishEntity(pub, layerId, serviceId); |
| | | Integer id = publishService.insert(publishEntity); |
| | | if (null == id) { |
| | | return id; |
| | | } |
| | | |
| | | return insertMetaPub(pub, id); |
| | | } |
| | | |
| | | /** |
| | | * 创建图层实体类 |
| | | */ |
| | | public CreateLayerEntity createLayerEntity(PubEntity pubEntity) { |
| | | CreateLayerEntity entity = new CreateLayerEntity(1, 3); |
| | | entity.setCreate_service(0); |
| | | |
| | |
| | | List<CreateLayerEntity.ImageList> imageLists = entity.getImage_list(); |
| | | CreateLayerEntity.ImageList imageList = imageLists.get(0); |
| | | imageList.setPath(""); |
| | | |
| | | return entity; |
| | | } |
| | | |
| | | /** |
| | | * 创建数简服务 |
| | | * 发送图层实体类 |
| | | */ |
| | | public void createShuJianService() { |
| | | public Integer postLayerEntity(CreateLayerEntity layerEntity) { |
| | | Map<String, Object> map = getMapData(layerEntity); |
| | | String url = String.format("%s/sj_raster/sj_raster/v6/api/layer?%s", sjUrl, ak); |
| | | |
| | | LayerResultEntity rs = postForRest(url, map, LayerResultEntity.class); |
| | | boolean flag = null == rs || !StaticData.SUCCESS.equals(rs.getStatus()) || null == rs.getResult().getLayer_id(); |
| | | |
| | | return flag ? null : rs.getResult().getLayer_id(); |
| | | } |
| | | |
| | | /** |
| | | * 创建服务实体类 |
| | | */ |
| | | public CreateServiceEntity createServiceEntity(PubEntity pubEntity) { |
| | | CreateServiceEntity entity = new CreateServiceEntity(); |
| | | List<Integer> layerList = entity.getLayer_list(); |
| | | |
| | |
| | | services.setSat_id(-1); |
| | | services.setSensor_id(-1); |
| | | services.setImage_type(-1); |
| | | |
| | | return entity; |
| | | } |
| | | |
| | | /** |
| | | * 删除数简服务 |
| | | * 发送图层实体类 |
| | | */ |
| | | public void deleteServe(PublishEntity entity) { |
| | | public Integer postServiceEntity(CreateServiceEntity serviceEntity) { |
| | | Map<String, Object> map = getMapData(serviceEntity); |
| | | String url = String.format("%s/sj_raster/v6/api/service?op=create_ex&%s", sjUrl, ak); |
| | | |
| | | ServiceResultEntity rs = postForRest(url, map, ServiceResultEntity.class); |
| | | boolean flag = null == rs || !StaticData.SUCCESS.equals(rs.getStatus()) || null == rs.getResult(); |
| | | |
| | | return flag ? null : rs.getResult(); |
| | | } |
| | | |
| | | /** |
| | | * 创建发布实体类 |
| | | */ |
| | | public PublishEntity createPublishEntity(PubEntity pub, Integer layerId, Integer serviceId) { |
| | | PublishEntity entity = new PublishEntity(); |
| | | entity.setType(pub.getType()); |
| | | entity.setName(pub.getName()); |
| | | entity.setMin(pub.getMin()); |
| | | entity.setMax(pub.getMax()); |
| | | entity.setBak(getEpsg(pub)); |
| | | entity.setCreateUser(pub.getUserId()); |
| | | entity.setDirid(pub.getDircode()); |
| | | entity.setDepid(pub.getDepcode()); |
| | | entity.setStatus(3); |
| | | entity.setLayerid(layerId); |
| | | entity.setServiceid(serviceId); |
| | | entity.setJson(getColorTableJson(pub)); |
| | | |
| | | return entity; |
| | | } |
| | | |
| | | /** |
| | | * 获取EPSG编码 |
| | | */ |
| | | public String getEpsg(PubEntity pub) { |
| | | if (StaticData.I104903 == pub.getEpsgCode()) { |
| | | return "ESRI:" + pub.getEpsgCode(); |
| | | } |
| | | |
| | | return "EPSG:" + pub.getEpsgCode(); |
| | | } |
| | | |
| | | /** |
| | | * 获取颜色表的JSON字符串 |
| | | */ |
| | | public String getColorTableJson(PubEntity pub) { |
| | | if (null == pub.getColorTable() && null == pub.getGradientColorTable()) { |
| | | return null; |
| | | } |
| | | |
| | | if (null != pub.getGradientColorTable()) { |
| | | return JSON.toJSONString(pub.getGradientColorTable()); |
| | | } |
| | | |
| | | return JSON.toJSONString(pub.getColorTable()); |
| | | } |
| | | |
| | | /** |
| | | * 插入元数据发布类 |
| | | */ |
| | | private int insertMetaPub(PubEntity pub, Integer pubid) { |
| | | int count = 0; |
| | | for (Integer metaid : pub.getIds()) { |
| | | MetaPubEntity mp = new MetaPubEntity(); |
| | | mp.setMetaid(metaid); |
| | | mp.setPubid(pubid); |
| | | mp.setCreateUser(pub.getUserId()); |
| | | |
| | | count += publishService.insertMetaPub(mp); |
| | | } |
| | | |
| | | return count; |
| | | } |
| | | |
| | | /** |
| | | * 插入数简服务 |
| | | */ |
| | | public Integer updateSjService(PubEntity pub, HttpServletRequest req, HttpServletResponse res) { |
| | | PublishEntity entity = publishService.selectById(pub.getPubid()); |
| | | if (null == entity) { |
| | | return 0; |
| | | } |
| | | if (null != entity.getLayerid()) { |
| | | deleteLayer(entity.getLayerid()); |
| | | } |
| | | if (null != entity.getServiceid()) { |
| | | deleteService(entity.getServiceid()); |
| | | } |
| | | |
| | | return 1; |
| | | } |
| | | |
| | | /** |
| | | * 删除数简服务 |
| | | */ |
| | | public Integer deletesSjServices(List<Integer> ids) { |
| | | String strs = StringHelper.join(ids, ","); |
| | | List<PublishEntity> list = publishService.selectByIds(strs); |
| | | if (null == list || list.isEmpty()) { |
| | | return 0; |
| | | } |
| | | |
| | | for (PublishEntity entity : list) { |
| | | if (null != entity.getLayerid()) { |
| | | deleteLayer(entity.getLayerid()); |
| | | } |
| | | if (null != entity.getServiceid()) { |
| | | deleteService(entity.getServiceid()); |
| | | } |
| | | } |
| | | |
| | | return publishService.deletes(strs); |
| | | } |
| | | |
| | | /** |
| | | * 删除数简图层 |
| | | * 删除图层 |
| | | */ |
| | | public void deleteLayer(Integer id) { |
| | | public boolean deleteLayer(Integer id) { |
| | | try { |
| | | DeleteEntity entity = new DeleteEntity(); |
| | | entity.getId_list().add(id); |
| | | |
| | | // |
| | | Map<String, Object> map = getMapData(entity); |
| | | String url = String.format("%s/sj_raster/sj_raster/v6/api/layer?%s", sjUrl, ak); |
| | | |
| | | DeleteResultEntity rs = deleteForRest(url, map); |
| | | |
| | | return null != rs && StaticData.SUCCESS.equals(rs.getStatus()); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 删除数简图层 |
| | | * 删除服务 |
| | | */ |
| | | public void deleteService(Integer id) { |
| | | public boolean deleteService(Integer id) { |
| | | try { |
| | | DeleteEntity entity = new DeleteEntity(); |
| | | entity.getId_list().add(id); |
| | | |
| | | // |
| | | Map<String, Object> map = getMapData(entity); |
| | | String url = String.format("%s/sj_raster/sj_raster/v6/api/service?%s", sjUrl, ak); |
| | | |
| | | DeleteResultEntity rs = deleteForRest(url, map); |
| | | |
| | | return null != rs && StaticData.SUCCESS.equals(rs.getStatus()); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * post请求(Rest) |
| | | */ |
| | | public <T> T postForRest(String url, Map<String, Object> map, Class<T> clazz) { |
| | | RestTemplate rest = RestHelper.getRestTemplate(); |
| | | |
| | | return rest.postForObject(url, map, clazz); |
| | | } |
| | | |
| | | /** |
| | | * delete请求(Rest) |
| | | */ |
| | | public DeleteResultEntity deleteForRest(String url, Map<String, Object> map) { |
| | | RestTemplate rest = RestHelper.getRestTemplate(); |
| | | ResponseEntity<DeleteResultEntity> rs = rest.exchange(url, HttpMethod.DELETE, null, DeleteResultEntity.class, map); |
| | | |
| | | return rs.getBody(); |
| | | } |
| | | |
| | | /** |
| | | * 获取Map数据 |
| | | */ |
| | | public <T> Map<String, Object> getMapData(T t) { |
| | | Map<String, Object> map = new HashMap<>(1); |
| | | |
| | | Field[] fields = t.getClass().getDeclaredFields(); |
| | | for (Field field : fields) { |
| | | try { |
| | | if ("serialVersionUID".equals(field.getName())) { |
| | | continue; |
| | | } |
| | | |
| | | field.setAccessible(true); |
| | | Object obj = field.get(t); |
| | | |
| | | map.put(field.getName(), obj); |
| | | } catch (Exception ex) { |
| | | // |
| | | } |
| | | } |
| | | |
| | | return map; |
| | | } |
| | | } |