|
|
@@ -1,242 +0,0 @@
|
|
|
-package com.uas.kanban.service.impl;
|
|
|
-
|
|
|
-import java.io.IOException;
|
|
|
-import java.sql.SQLException;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Iterator;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import javax.xml.transform.TransformerException;
|
|
|
-
|
|
|
-import org.dom4j.DocumentException;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.uas.kanban.annotation.NotEmpty;
|
|
|
-import com.uas.kanban.base.BaseDao;
|
|
|
-import com.uas.kanban.base.BaseService;
|
|
|
-import com.uas.kanban.dao.KanbanDao;
|
|
|
-import com.uas.kanban.dao.PanelDao;
|
|
|
-import com.uas.kanban.dao.PanelViewDao;
|
|
|
-import com.uas.kanban.exception.OperationException;
|
|
|
-import com.uas.kanban.model.Kanban;
|
|
|
-import com.uas.kanban.model.Panel;
|
|
|
-import com.uas.kanban.model.PanelView;
|
|
|
-import com.uas.kanban.model.Parameter;
|
|
|
-import com.uas.kanban.model.ViewConfig;
|
|
|
-import com.uas.kanban.service.PanelViewService;
|
|
|
-import com.uas.kanban.service.ParameterService;
|
|
|
-import com.uas.kanban.support.DataSourceManager;
|
|
|
-import com.uas.kanban.support.KanbanParser;
|
|
|
-import com.uas.kanban.support.SystemSession;
|
|
|
-import com.uas.kanban.util.CollectionUtils;
|
|
|
-import com.uas.kanban.util.ObjectUtils;
|
|
|
-import com.uas.kanban.util.StringUtils;
|
|
|
-
|
|
|
-import me.chyxion.jdbc.NewbieJdbcSupport;
|
|
|
-
|
|
|
-/**
|
|
|
- * 面板画面
|
|
|
- *
|
|
|
- * @author zhuth
|
|
|
- * @since 2018/5/24 14:04
|
|
|
- */
|
|
|
-@Service
|
|
|
-public class PanelViewServiceImpl extends BaseService<PanelView> implements PanelViewService {
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private PanelViewDao panelViewDao;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private PanelDao panelDao;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private KanbanDao kanbanDao;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private ParameterService parameterService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private DataSourceManager dataSourceManager;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private KanbanParser kanbanParser;
|
|
|
-
|
|
|
- @Override
|
|
|
- public PanelView save(@NotEmpty("json") String json) {
|
|
|
- PanelView panelView = panelViewDao.parse(json);
|
|
|
- Panel panel = panelDao.checkExist(panelView.getPanelCode());
|
|
|
- panelView.setPanelVersion(panel.getVersion());
|
|
|
- return panelViewDao.save(panelView);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public int update(@NotEmpty("json") String json) throws IllegalArgumentException, OperationException {
|
|
|
- PanelView newPanelView = panelViewDao.parse(json);
|
|
|
- PanelView oldPanelView = panelViewDao.checkExist(newPanelView.getCode());
|
|
|
- if(ObjectUtils.equals(newPanelView, oldPanelView)) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- return panelViewDao.update(newPanelView);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Map<String, Object> parseData(@NotEmpty("code") String code, Integer index, String kanbanCode) throws OperationException {
|
|
|
- index = index == null ? 0 : index;
|
|
|
- PanelView panelView = panelViewDao.checkExist(code);
|
|
|
- BaseDao.checkFields(panelView);
|
|
|
- Panel panel = panelDao.checkExist(panelView.getPanelCode());
|
|
|
- List<ViewConfig> areaConfigs = panelView.getAreaConfig();
|
|
|
- ViewConfig viewConfig = areaConfigs.get(index);
|
|
|
- List<String> viewKanbans = viewConfig.getViewKanbans();
|
|
|
- if(CollectionUtils.isEmpty(viewKanbans)) {
|
|
|
- throw new IllegalStateException("该画面未配置可展示看板");
|
|
|
- }
|
|
|
- if (!StringUtils.isEmpty(kanbanCode)) {
|
|
|
- if (!viewKanbans.contains(kanbanCode)) {
|
|
|
- throw new IllegalArgumentException("看板不存在或未启用:" + kanbanCode);
|
|
|
- }
|
|
|
- } else {
|
|
|
- kanbanCode = viewKanbans.get(0);
|
|
|
- }
|
|
|
- Kanban kanban = kanbanDao.checkExist(kanbanCode);
|
|
|
- BaseDao.checkFields(kanban);
|
|
|
- String content = kanban.getContent();
|
|
|
- String title = kanban.getTitle();
|
|
|
- if (StringUtils.isEmpty(content)) {
|
|
|
- throw new IllegalStateException("看板内容为空:" + kanbanCode);
|
|
|
- }
|
|
|
- List<Parameter> parameters = parameterService.getParseDataByPanelCode(panel.getCode());
|
|
|
- List<JSONObject> pvalues = panelView.getParameters();
|
|
|
- JSONObject values = pvalues.get(index);
|
|
|
- for(Parameter parameter : parameters) {
|
|
|
- String value = values.getString(parameter.getCode());
|
|
|
- parameter.setValue(value);
|
|
|
- }
|
|
|
- // 解析看板
|
|
|
- String kanbanContent;
|
|
|
- try {
|
|
|
- NewbieJdbcSupport jdbc = dataSourceManager.getJdbc(panel.getDataSourceCode());
|
|
|
- kanbanContent = kanbanParser.parseXml(content, title, parameters, jdbc);
|
|
|
- } catch (DocumentException e) {
|
|
|
- throw new IllegalStateException("xml 解析出错", e);
|
|
|
- } catch (TransformerException e) {
|
|
|
- throw new IllegalStateException("xml 映射出错", e);
|
|
|
- } catch (IOException e) {
|
|
|
- throw new IllegalStateException("xml 转换出错", e);
|
|
|
- } catch (SQLException e) {
|
|
|
- throw new IllegalStateException("SQL 查询错误", e);
|
|
|
- }
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- Map<String, Object> instance = new HashMap<>();
|
|
|
- instance.put("parameters", parameters);
|
|
|
- instance.put("enabledKanbanCodes", viewKanbans);
|
|
|
- instance.put("display", panel.getDisplay());
|
|
|
- instance.put("switchFrequency", values.get("refreshFrequency"));
|
|
|
- instance.put("refreshFrequency", values.get("refreshFrequency"));
|
|
|
- result.put("instance", instance);
|
|
|
- List<JSONObject> data = new ArrayList<>();
|
|
|
- data.add(JSONObject.parseObject(kanbanContent));
|
|
|
- result.put("data", data);
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public PanelView getByPanelCode(String panelCode) throws OperationException {
|
|
|
- PanelView panelView = panelViewDao.findByPanelCode(panelCode);
|
|
|
- Panel panel = panelDao.checkExist(panelCode);
|
|
|
- if(panelView == null || panel.getVersion() > panelView.getPanelVersion()) {
|
|
|
- if(panelView != null) {
|
|
|
- panelView = updatePanelView(panelCode, panelView);
|
|
|
- } else {
|
|
|
- panelView = generatePanelView(panelCode);
|
|
|
- }
|
|
|
- }
|
|
|
- return panelView;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 更新面板多画面配置
|
|
|
- *
|
|
|
- * @param panelCode 面板 code
|
|
|
- * @param oldPanelView 旧的画面配置
|
|
|
- * @return 新的画面配置
|
|
|
- * @throws OperationException
|
|
|
- * @throws IllegalArgumentException
|
|
|
- */
|
|
|
- private PanelView updatePanelView(@NotEmpty("panelCode") String panelCode, @NotEmpty("oldPanelView") PanelView oldPanelView) throws IllegalArgumentException, OperationException {
|
|
|
- Panel panel = panelDao.checkExist(panelCode);
|
|
|
- PanelView panelView;
|
|
|
- try{
|
|
|
- panelView = ObjectUtils.clone(oldPanelView);
|
|
|
- } catch (IOException | ClassNotFoundException e) {
|
|
|
- throw new IllegalStateException("更新画面配置时,旧画面克隆失败", e);
|
|
|
- }
|
|
|
- panelView.setPanelVersion(panel.getVersion());
|
|
|
- List<ViewConfig> viewConfigs = panelView.getAreaConfig();
|
|
|
- List<Parameter> newParameter = parameterService.getParseDataByPanelCode(panel.getCode());
|
|
|
- Iterator<Parameter> i = newParameter.iterator();
|
|
|
- while(i.hasNext()) {
|
|
|
- Parameter p = i.next();
|
|
|
- for(ViewConfig viewConfig : viewConfigs) {
|
|
|
- if(viewConfig.getViewParameters() == null) {
|
|
|
- break;
|
|
|
- }
|
|
|
- JSONObject jsonObj = JSON.parseObject(viewConfig.getViewParameters());
|
|
|
- // TODO 这里只取了String类型的值
|
|
|
- if(jsonObj.getString(p.getCode()) != null) {
|
|
|
- String value = jsonObj.getString(p.getCode());
|
|
|
- List<Object> optionalValues = p.getOptionalValues();
|
|
|
- List<String> values = new ArrayList<String>();
|
|
|
- for(Object obj : optionalValues) {
|
|
|
- values.add(obj.toString());
|
|
|
- }
|
|
|
- if(values.contains(value)) {
|
|
|
- // 使用原值
|
|
|
- } else {
|
|
|
- // 使用新值
|
|
|
- jsonObj.put(p.getCode(), values.get(0));
|
|
|
- }
|
|
|
- }
|
|
|
- viewConfig.setViewParameters(JSON.toJSONString(jsonObj));
|
|
|
- }
|
|
|
- }
|
|
|
- panelView.setAreaConfig(viewConfigs);
|
|
|
- panelViewDao.update(panelView);
|
|
|
- return panelView;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 初始化面板多画面配置
|
|
|
- *
|
|
|
- * @param panelCode 面板 code
|
|
|
- * @return 画面配置
|
|
|
- */
|
|
|
- private PanelView generatePanelView(@NotEmpty("panelCode") String panelCode) {
|
|
|
- Panel panel = panelDao.checkExist(panelCode);
|
|
|
- PanelView panelView = new PanelView();
|
|
|
- panelView.setViewType("single");
|
|
|
- panelView.setPanelCode(panel.getCode());
|
|
|
- panelView.setPanelVersion(panel.getVersion());
|
|
|
- panelView.setAreaRows(1);
|
|
|
- panelView.setAreaCols(1);
|
|
|
- panelView.setUserCode(SystemSession.getUser().codeNotEmpty());
|
|
|
- List<ViewConfig> areaConfig = new ArrayList<ViewConfig>();
|
|
|
- ViewConfig v = new ViewConfig();
|
|
|
- v.setViewIndex(0);
|
|
|
- v.setViewKanbans(new ArrayList<String>());
|
|
|
- List<Parameter> parameters = parameterService.getParseDataByPanelCode(panelCode);
|
|
|
- String parametersStr = "{";
|
|
|
- for(Parameter p: parameters) {
|
|
|
- parametersStr += ("\"" + p.getCode() + "\":\"" + p.getValue() + "\",");
|
|
|
- }
|
|
|
- parametersStr += "\"refreshFrequency\":5}";
|
|
|
- v.setViewParameters(parametersStr);
|
|
|
- areaConfig.add(v);
|
|
|
- panelView.setAreaConfig(areaConfig);
|
|
|
- return panelView;
|
|
|
- }
|
|
|
-}
|