forked from fanruan/design
rinoux
4 years ago
6 changed files with 1364 additions and 291 deletions
@ -0,0 +1,154 @@
|
||||
package com.fr.design.data; |
||||
|
||||
import com.fr.base.io.IOFile; |
||||
import com.fr.design.file.HistoryTemplateListCache; |
||||
import com.fr.design.mainframe.JTemplate; |
||||
import com.fr.esd.core.strategy.config.StrategyConfig; |
||||
import com.fr.esd.core.strategy.config.StrategyConfigHelper; |
||||
import com.fr.esd.core.strategy.persistence.StrategyConfigsAttr; |
||||
import com.fr.esd.event.DSMapping; |
||||
import com.fr.esd.event.DsNameTarget; |
||||
import com.fr.esd.event.StrategyEventsNotifier; |
||||
import com.fr.esd.event.xml.XMLSavedHook; |
||||
import com.fr.file.FILE; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.workspace.WorkContext; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.HashSet; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
|
||||
/** |
||||
* @author rinoux |
||||
* @version 10.0 |
||||
* Created by rinoux on 2020/10/28 |
||||
*/ |
||||
public class DesignerStrategyConfigUtils { |
||||
|
||||
/** |
||||
* 获取当前编辑模版的数据集缓存配置属性 |
||||
* |
||||
* @return |
||||
*/ |
||||
private static StrategyConfigsAttr getStrategyConfigsAttr() { |
||||
StrategyConfigsAttr attr; |
||||
JTemplate<?, ?> jTemplate = HistoryTemplateListCache.getInstance().getCurrentEditingTemplate(); |
||||
if (jTemplate != null) { |
||||
IOFile ioFile = (IOFile) jTemplate.getTarget(); |
||||
|
||||
attr = ioFile.getAttrMark(StrategyConfigsAttr.ATTR_MARK); |
||||
if (attr == null) { |
||||
attr = new StrategyConfigsAttr(); |
||||
ioFile.addAttrMark(attr); |
||||
} |
||||
|
||||
//新建模版此时不存在,不需要注册钩子
|
||||
if (attr.getXmlSavedHook() == null && WorkContext.getWorkResource().exist(jTemplate.getPath())) { |
||||
attr.setXmlSavedHook(new StrategyConfigsAttrSavedHook(jTemplate.getPath(), attr)); |
||||
} |
||||
return attr; |
||||
} else { |
||||
throw new IllegalStateException("[ESD]No editing template found."); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 当前编辑的模版是否被批量开启 |
||||
* |
||||
* @return |
||||
*/ |
||||
public static boolean isEditingTemplateRecommended() { |
||||
JTemplate<?, ?> jTemplate = HistoryTemplateListCache.getInstance().getCurrentEditingTemplate(); |
||||
|
||||
if (jTemplate != null) { |
||||
FILE file = jTemplate.getEditingFILE(); |
||||
if (file != null) { |
||||
String path = file.getPath(); |
||||
return StrategyConfigHelper.recommended(path); |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 获取模版数据集配置 |
||||
* |
||||
* @param dsName |
||||
* @return |
||||
*/ |
||||
public static StrategyConfig getStrategyConfig(String dsName) { |
||||
|
||||
return getStrategyConfigsAttr().getStrategyConfig(dsName); |
||||
} |
||||
|
||||
/** |
||||
* 移除模版数据集配置 |
||||
* |
||||
* @param dsName |
||||
* @return |
||||
*/ |
||||
public static StrategyConfig removeStrategyConfig(String dsName) { |
||||
|
||||
return getStrategyConfigsAttr().removeStrategyConfig(dsName); |
||||
} |
||||
|
||||
/** |
||||
* 添加模版数据集配置 |
||||
* |
||||
* @param strategyConfig |
||||
*/ |
||||
public static void addStrategyConfig(StrategyConfig strategyConfig) { |
||||
|
||||
getStrategyConfigsAttr().addStrategyConfig(strategyConfig); |
||||
} |
||||
|
||||
|
||||
private static class StrategyConfigsAttrSavedHook implements XMLSavedHook<StrategyConfigsAttr> { |
||||
|
||||
private static final long serialVersionUID = -8843201977112289321L; |
||||
|
||||
private final String tplPath; |
||||
private final Map<String, StrategyConfig> origStrategyConfigs; |
||||
|
||||
public StrategyConfigsAttrSavedHook(String tplPath, StrategyConfigsAttr raw) { |
||||
this.tplPath = tplPath; |
||||
this.origStrategyConfigs = new HashMap<>(); |
||||
this.initOrigStrategyConfigs(raw); |
||||
} |
||||
|
||||
|
||||
private void initOrigStrategyConfigs(StrategyConfigsAttr raw) { |
||||
origStrategyConfigs.clear(); |
||||
raw.getStrategyConfigs().forEach((k, v) -> { |
||||
try { |
||||
origStrategyConfigs.put(k, v.clone()); |
||||
} catch (CloneNotSupportedException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterSaved(StrategyConfigsAttr saved) { |
||||
|
||||
FineLoggerFactory.getLogger().info("[ESD]Write StrategyConfigsAttr done, now check change."); |
||||
Set<String> dsNames = new HashSet<>(); |
||||
dsNames.addAll(origStrategyConfigs.keySet()); |
||||
dsNames.addAll(saved.getStrategyConfigs().keySet()); |
||||
|
||||
if (StringUtils.isNotEmpty(tplPath)) { |
||||
dsNames.forEach(dsName -> { |
||||
if (StringUtils.isNotEmpty(dsName)) { |
||||
StrategyEventsNotifier.compareAndFireConfigEvents(origStrategyConfigs.get(dsName), saved.getStrategyConfig(dsName), new DSMapping(tplPath, new DsNameTarget(dsName))); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
initOrigStrategyConfigs(saved); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,225 @@
|
||||
package com.fr.design.data.datapane; |
||||
|
||||
import com.fr.design.beans.BasicBeanPane; |
||||
import com.fr.design.gui.ibutton.UIRadioButton; |
||||
import com.fr.design.gui.icheckbox.UICheckBox; |
||||
import com.fr.design.gui.ilable.ActionLabel; |
||||
import com.fr.design.gui.ilable.UILabel; |
||||
import com.fr.design.gui.itextfield.UITextField; |
||||
import com.fr.design.layout.FRGUIPaneFactory; |
||||
import com.fr.design.utils.gui.GUICoreUtils; |
||||
import com.fr.esd.common.CacheConstants; |
||||
import com.fr.esd.core.strategy.config.StrategyConfig; |
||||
import com.fr.esd.core.strategy.config.StrategyConfigHelper; |
||||
import com.fr.esd.util.Utils; |
||||
import com.fr.locale.InterProviderFactory; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.stable.StringUtils; |
||||
|
||||
import javax.swing.AbstractAction; |
||||
import javax.swing.JPanel; |
||||
import java.awt.BorderLayout; |
||||
import java.awt.Color; |
||||
import java.awt.Component; |
||||
import java.awt.Desktop; |
||||
import java.awt.event.ActionEvent; |
||||
import java.awt.event.ActionListener; |
||||
import java.net.URI; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author rinoux |
||||
* @version 10.0 |
||||
* Created by rinoux on 2020/7/22 |
||||
*/ |
||||
public class ESDStrategyConfigPane extends BasicBeanPane<StrategyConfig> { |
||||
private UIRadioButton selectAutoUpdate; |
||||
private UIRadioButton selectBySchema; |
||||
private UICheckBox shouldEvolve; |
||||
private UILabel updateIntervalCheckTips; |
||||
private UITextField updateInterval; |
||||
private UITextField schemaTime; |
||||
private ActionLabel actionLabel; |
||||
private UILabel schemaTimeCheckTips; |
||||
private final boolean global; |
||||
private StrategyConfig strategyConfig; |
||||
|
||||
private static final String CRON_HELP_URL = "http://help.fanruan.com/finereport/doc-view-693.html"; |
||||
|
||||
public ESDStrategyConfigPane(boolean global) { |
||||
this.global = global; |
||||
init(); |
||||
} |
||||
|
||||
private void init() { |
||||
setLayout(FRGUIPaneFactory.createM_BorderLayout()); |
||||
|
||||
this.selectAutoUpdate = new UIRadioButton(InterProviderFactory.getProvider().getLocText("ESD_Config-Pane-Every-Interval")); |
||||
|
||||
this.updateInterval = new UITextField(4); |
||||
this.shouldEvolve = new UICheckBox(InterProviderFactory.getProvider().getLocText("ESD_Config-Pane-Auto-Evolved-Strategy"), false); |
||||
this.shouldEvolve.setEnabled(false); |
||||
this.updateIntervalCheckTips = new UILabel(InterProviderFactory.getProvider().getLocText("ESD_Config-Pane-Error-Interval-Format")); |
||||
this.updateIntervalCheckTips.setForeground(Color.RED); |
||||
this.updateIntervalCheckTips.setVisible(false); |
||||
|
||||
this.selectBySchema = new UIRadioButton(InterProviderFactory.getProvider().getLocText("ESD_Config-pane-Cron")); |
||||
this.schemaTime = new UITextField(10); |
||||
this.schemaTimeCheckTips = new UILabel(InterProviderFactory.getProvider().getLocText("ESD_Config-Pane-Error-Time-Format")); |
||||
this.schemaTimeCheckTips.setVisible(false); |
||||
this.schemaTimeCheckTips.setForeground(Color.RED); |
||||
|
||||
|
||||
this.selectAutoUpdate.addActionListener(new AbstractAction() { |
||||
public void actionPerformed(ActionEvent e) { |
||||
ESDStrategyConfigPane.this.selectBySchema.setSelected(!ESDStrategyConfigPane.this.selectAutoUpdate.isSelected()); |
||||
ESDStrategyConfigPane.this.schemaTime.setEnabled(false); |
||||
ESDStrategyConfigPane.this.updateInterval.setEnabled(true); |
||||
ESDStrategyConfigPane.this.shouldEvolve.setEnabled(true); |
||||
} |
||||
}); |
||||
|
||||
this.selectBySchema.addActionListener(new AbstractAction() { |
||||
public void actionPerformed(ActionEvent e) { |
||||
ESDStrategyConfigPane.this.selectAutoUpdate.setSelected(!ESDStrategyConfigPane.this.selectBySchema.isSelected()); |
||||
ESDStrategyConfigPane.this.schemaTime.setEnabled(true); |
||||
ESDStrategyConfigPane.this.updateInterval.setEnabled(false); |
||||
ESDStrategyConfigPane.this.shouldEvolve.setEnabled(false); |
||||
} |
||||
}); |
||||
|
||||
JPanel pane = FRGUIPaneFactory.createVerticalTitledBorderPane(InterProviderFactory.getProvider().getLocText("ESD_Config-Pane-Cache-Update-Strategy")); |
||||
add(pane, BorderLayout.NORTH); |
||||
|
||||
JPanel row1 = GUICoreUtils.createFlowPane(new Component[]{ |
||||
this.selectAutoUpdate, |
||||
this.updateInterval, |
||||
new UILabel(InterProviderFactory.getProvider().getLocText("ESD_Config-Pane-Minute-Update-Cache")), |
||||
this.shouldEvolve, |
||||
this.updateIntervalCheckTips |
||||
}, 0, 5); |
||||
pane.add(row1); |
||||
|
||||
ActionLabel actionLabel = new ActionLabel(InterProviderFactory.getProvider().getLocText("ESD_config-pane-Cron-Help")); |
||||
actionLabel.addActionListener(new ActionListener() { |
||||
@Override |
||||
public void actionPerformed(ActionEvent e) { |
||||
try { |
||||
Desktop.getDesktop().browse(new URI(CRON_HELP_URL)); |
||||
} catch (Exception exp) { |
||||
FineLoggerFactory.getLogger().error(exp.getMessage(), e); |
||||
} |
||||
} |
||||
}); |
||||
|
||||
JPanel row2 = GUICoreUtils.createFlowPane(new Component[]{ |
||||
this.selectBySchema, |
||||
this.schemaTime, |
||||
actionLabel, |
||||
this.schemaTimeCheckTips |
||||
}, 0, 5); |
||||
pane.add(row2); |
||||
} |
||||
|
||||
|
||||
public void populateBean(StrategyConfig ob) { |
||||
|
||||
if (ob == null && !global) { |
||||
ob = StrategyConfigHelper.createStrategyConfig(true, false, true); |
||||
} |
||||
this.strategyConfig = ob; |
||||
|
||||
this.updateInterval.setText(ob.getUpdateInterval() <= 0 ? "0" : String.valueOf(ob.getUpdateInterval() / (double) CacheConstants.MILLIS_OF_MINUTE)); |
||||
this.schemaTime.setText(StringUtils.join(",", ob.getUpdateSchema().toArray(new String[0]))); |
||||
this.shouldEvolve.setSelected(ob.shouldEvolve()); |
||||
this.selectBySchema.setSelected(ob.isScheduleBySchema()); |
||||
this.selectAutoUpdate.setSelected(!ob.isScheduleBySchema()); |
||||
|
||||
if (global) { |
||||
//使用全局配置,禁用面板编辑
|
||||
disablePane(); |
||||
} else { |
||||
setSchemaEnable(!this.selectAutoUpdate.isSelected()); |
||||
} |
||||
} |
||||
|
||||
|
||||
private void disablePane() { |
||||
this.selectAutoUpdate.setEnabled(false); |
||||
this.updateInterval.setEnabled(false); |
||||
this.shouldEvolve.setEnabled(false); |
||||
|
||||
this.selectBySchema.setEnabled(false); |
||||
this.schemaTime.setEnabled(false); |
||||
} |
||||
|
||||
private void setSchemaEnable(boolean enable) { |
||||
this.updateInterval.setEnabled(!enable); |
||||
this.shouldEvolve.setEnabled(!enable); |
||||
|
||||
this.schemaTime.setEnabled(enable); |
||||
} |
||||
|
||||
|
||||
public StrategyConfig updateBean() { |
||||
StrategyConfig config = null; |
||||
if (!this.global) { |
||||
try { |
||||
//这里是new的config
|
||||
config = this.strategyConfig.clone(); |
||||
|
||||
if (this.selectBySchema.isSelected()) { |
||||
List<String> schemaTimes = new ArrayList<>(); |
||||
String text = this.schemaTime.getText(); |
||||
|
||||
if (Utils.checkUpdateTimeSchema(text)) { |
||||
if (Utils.isCronExpression(text)) { |
||||
schemaTimes.add(text); |
||||
} else { |
||||
Collections.addAll(schemaTimes, text.split(",")); |
||||
} |
||||
} else { |
||||
this.schemaTimeCheckTips.setVisible(true); |
||||
throw new IllegalArgumentException("update schema time format error."); |
||||
} |
||||
config.setScheduleBySchema(true); |
||||
config.setUpdateSchema(schemaTimes); |
||||
} else { |
||||
String interval = this.updateInterval.getText(); |
||||
if (checkUpdateInterval(interval)) { |
||||
long intervalMillis = StringUtils.isEmpty(interval) ? 0 : (long) (Double.parseDouble(interval) * CacheConstants.MILLIS_OF_MINUTE); |
||||
config.setUpdateInterval(intervalMillis); |
||||
} else { |
||||
this.updateIntervalCheckTips.setVisible(true); |
||||
throw new IllegalArgumentException("Error update interval format."); |
||||
} |
||||
|
||||
config.setShouldEvolve(this.shouldEvolve.isSelected()); |
||||
config.setScheduleBySchema(false); |
||||
} |
||||
} catch (CloneNotSupportedException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
|
||||
return config; |
||||
} |
||||
|
||||
|
||||
private boolean checkUpdateInterval(String intervalValue) { |
||||
try { |
||||
return !StringUtils.isEmpty(intervalValue) && !(Double.parseDouble(intervalValue) <= 0); |
||||
} catch (NumberFormatException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
|
||||
protected String title4PopupWindow() { |
||||
return InterProviderFactory.getProvider().getLocText("ESD_Config-Pane-Cache-Strategy-Config-Title"); |
||||
} |
||||
} |
After Width: | Height: | Size: 1.3 KiB |
After Width: | Height: | Size: 1.2 KiB |
Loading…
Reference in new issue