From 7eff901f2c27b7971ef00a0c22fcf2cb757e705b Mon Sep 17 00:00:00 2001 From: rinoux Date: Fri, 8 Dec 2017 14:03:35 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-5942=209.0=E6=80=A7=E8=83=BD=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E5=86=85=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../write/submit/SubmitVisitorListPane.java | 2 + .../batch/BatchCommit2DBJavaScript.java | 170 +++ .../batch/BatchCommit2DBJavaScriptPane.java | 186 +++ .../submit/batch/BatchCommitTabbedPane.java | 529 ++++++++ .../batch/BatchJavaScriptActionProvider.java | 23 + .../write/submit/batch/BatchSubmitPane.java | 1089 +++++++++++++++++ .../submit/batch/BatchSubmitProvider.java | 42 + .../batch/SmartInsertBatchSubmitPane.java | 591 +++++++++ .../batch/service/BatchDBCommitService.java | 288 +++++ 9 files changed, 2920 insertions(+) create mode 100644 designer/src/com/fr/design/write/submit/batch/BatchCommit2DBJavaScript.java create mode 100644 designer/src/com/fr/design/write/submit/batch/BatchCommit2DBJavaScriptPane.java create mode 100644 designer/src/com/fr/design/write/submit/batch/BatchCommitTabbedPane.java create mode 100644 designer/src/com/fr/design/write/submit/batch/BatchJavaScriptActionProvider.java create mode 100644 designer/src/com/fr/design/write/submit/batch/BatchSubmitPane.java create mode 100644 designer/src/com/fr/design/write/submit/batch/BatchSubmitProvider.java create mode 100644 designer/src/com/fr/design/write/submit/batch/SmartInsertBatchSubmitPane.java create mode 100644 designer/src/com/fr/design/write/submit/batch/service/BatchDBCommitService.java diff --git a/designer/src/com/fr/design/write/submit/SubmitVisitorListPane.java b/designer/src/com/fr/design/write/submit/SubmitVisitorListPane.java index 3dfea6c15b..2b18427bdc 100644 --- a/designer/src/com/fr/design/write/submit/SubmitVisitorListPane.java +++ b/designer/src/com/fr/design/write/submit/SubmitVisitorListPane.java @@ -13,6 +13,7 @@ import com.fr.design.layout.FRGUIPaneFactory; import com.fr.design.mainframe.ElementCasePane; import com.fr.design.scrollruler.ModLineBorder; import com.fr.design.utils.gui.GUICoreUtils; +import com.fr.design.write.submit.batch.BatchSubmitProvider; import com.fr.general.ComparatorUtils; import com.fr.general.FRLogger; import com.fr.general.Inter; @@ -177,6 +178,7 @@ public class SubmitVisitorListPane extends ObjectJControlPane { for (SubmitProvider provider : providers) { addSubmitPane(provider); } + addSubmitPane(BatchSubmitProvider.getInstance()); configTypes = new ArrayList<>(); for (Map.Entry entry : customSubmitPanes.entrySet()) { diff --git a/designer/src/com/fr/design/write/submit/batch/BatchCommit2DBJavaScript.java b/designer/src/com/fr/design/write/submit/batch/BatchCommit2DBJavaScript.java new file mode 100644 index 0000000000..d44a43722d --- /dev/null +++ b/designer/src/com/fr/design/write/submit/batch/BatchCommit2DBJavaScript.java @@ -0,0 +1,170 @@ +package com.fr.design.write.submit.batch; + +import com.fr.general.ComparatorUtils; +import com.fr.general.xml.GeneralXMLTools; +import com.fr.js.AbstractJavaScript; +import com.fr.js.Callback; +import com.fr.js.JavaScript; +import com.fr.js.JavaScriptXMLUtils; +import com.fr.json.JSONObject; +import com.fr.stable.ParameterProvider; +import com.fr.stable.StringUtils; +import com.fr.stable.web.Repository; +import com.fr.stable.xml.XMLPrintWriter; +import com.fr.stable.xml.XMLableReader; +import com.fr.write.batch.SubmitMain; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by loy on 16/8/22. + */ +public class BatchCommit2DBJavaScript extends AbstractJavaScript implements Callback { + + public static String RECALCULATE_TAG = "shouldRecalculate"; + + /** + * 回调函数标识 + */ + public static final String CALLBACK = "callback"; + + /** + * 回调参数标识 + */ + public static final String FEEDBACKMAP = "feedbackMap"; + + /** + * javascript所使用的参数 + */ + protected ParameterProvider[] parameters; + //这个MAP里放的是提交过程生成的参数,比如提交入库,在所有回调事件里,都会将其传递进去,以供解析。 + protected Map paraMap = new HashMap(); + + private boolean recalculate; + + private List dbManipulationList = new ArrayList(); + //回调函数 + private JavaScript callBack; + + /** + * 获取数据入库配置信息 + */ + public List getDBManipulation() { + return dbManipulationList; + } + + /** + * 设置数据入库配置信息 + * + * @param dbManipulationList 数据库配置信息 + */ + public void setDBManipulation(List dbManipulationList) { + this.dbManipulationList = dbManipulationList; + } + + /** + * 回调函数,该函数将在主函数执行完毕以后开始执行 + */ + public JavaScript getCallBack() { + return callBack; + } + + /** + * 设置回调函数 + * + * @param callback 回调函数 + */ + public void setCallBack(JavaScript callback) { + this.callBack = callback; + } + + /** + * JS响应 + * + * @param repo 环境 + * @return 返回生成的JS字符串 + */ + public String actionJS(Repository repo) { + String dmlconf = GeneralXMLTools.writeXMLableAsString(this); + if (!this.paraMap.isEmpty() && callBack != null) { + callBack.addParameterMap(paraMap); + } + String js = "var fm = this.options.form;if(fm == null) {fm = new FR.BatchForm()};fm.batchCommit({" + + "xmlconf" + ":" + JSONObject.quote(dmlconf) + + (callBack != null ? "," + CALLBACK + ":" + JSONObject.quote(GeneralXMLTools.writeXMLableAsString(callBack)) : "") + + (this.paraMap.isEmpty() ? "" : "," + FEEDBACKMAP + ":" + new JSONObject(paraMap).toString()) + + "},this)"; + return js; + } + + /** + * 转化为字符串 + * + * @return 返回字符串形式 + */ + public String toString() { + return (dbManipulationList == null) ? StringUtils.EMPTY : dbManipulationList.toString(); + } + + public void writeXML(XMLPrintWriter writer) { + super.writeXML(writer); + + if (this.dbManipulationList != null) { + for (int i = 0; i < dbManipulationList.size(); i++) { + ((SubmitMain) this.dbManipulationList.get(i)).writeXML(writer); + } + + } + + if (this.callBack != null) { + GeneralXMLTools.writeXMLable(writer, this.callBack, JavaScript.XML_TAG); + } + } + + public void readXML(XMLableReader reader) { + super.readXML(reader); + + if (reader.isAttr()) { + dbManipulationList = new ArrayList(); + } else if (reader.isChildNode()) { + String tagName = reader.getTagName(); + if (JavaScript.XML_TAG.equals(tagName)) { + this.callBack = JavaScriptXMLUtils.readJavaScript(reader); + } else { + if ("Attributes".equals(tagName)) { + dbManipulationList.add(new SubmitMain()); + } + if (dbManipulationList.size() > 0) { + ((SubmitMain) dbManipulationList.get(dbManipulationList.size() - 1)).readXML(reader); + } + } + } + + } + + public boolean equals(Object obj) { + return obj instanceof BatchCommit2DBJavaScript + && super.equals(obj) + && ComparatorUtils.equals(((BatchCommit2DBJavaScript) obj).callBack, this.callBack) + && ComparatorUtils.equals(((BatchCommit2DBJavaScript) obj).dbManipulationList, this.dbManipulationList); + } + + public Object clone() throws CloneNotSupportedException { + BatchCommit2DBJavaScript cloned = (BatchCommit2DBJavaScript) super.clone(); + if (this.dbManipulationList != null) { + cloned.dbManipulationList = new ArrayList(); + for (int i = 0; i < this.dbManipulationList.size(); i++) { + cloned.dbManipulationList.add(((SubmitMain) this.dbManipulationList.get(i)).clone()); + } + } + + if (this.callBack != null) { + cloned.callBack = (JavaScript) this.callBack.clone(); + } + + return cloned; + } +} diff --git a/designer/src/com/fr/design/write/submit/batch/BatchCommit2DBJavaScriptPane.java b/designer/src/com/fr/design/write/submit/batch/BatchCommit2DBJavaScriptPane.java new file mode 100644 index 0000000000..f451d09078 --- /dev/null +++ b/designer/src/com/fr/design/write/submit/batch/BatchCommit2DBJavaScriptPane.java @@ -0,0 +1,186 @@ +package com.fr.design.write.submit.batch; + +import com.fr.design.beans.FurtherBasicBeanPane; +import com.fr.design.gui.ibutton.UIButton; +import com.fr.design.javascript.JavaScriptActionPane; +import com.fr.design.layout.FRGUIPaneFactory; +import com.fr.general.Inter; +import com.fr.write.batch.SubmitMain; + +import javax.swing.*; +import java.awt.*; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by loy on 16/8/22. + */ +public class BatchCommit2DBJavaScriptPane extends FurtherBasicBeanPane { + private List dbmPaneList = new ArrayList(); + private BatchCommitTabbedPane commitTabbedPane; + private JavaScriptActionPane javaScriptActionPane; + private UIButton addCallbackButton; + + private JPanel cardPane; + private String[] cardNames; + + /** + * 构造函数,控件事件的提交入库面板 + */ + public BatchCommit2DBJavaScriptPane() { + init(null); + } + + // public BatchCommit2DBJavaScriptPane(final JavaScriptActionPane javaScriptActionPane, List dbManipulationPaneList) { + public BatchCommit2DBJavaScriptPane(final JavaScriptActionPane javaScriptActionPane) { +// this.dbmPaneList=dbManipulationPaneList; + init(javaScriptActionPane); + } + + private void init(final JavaScriptActionPane javaScriptActionPane) { + this.dbmPaneList.add(new BatchSubmitPane()); + this.javaScriptActionPane = javaScriptActionPane; + this.setLayout(FRGUIPaneFactory.createBorderLayout()); + commitTabbedPane = new BatchCommitTabbedPane(this, dbmPaneList); + commitTabbedPane.setPreferredSize(new Dimension(commitTabbedPane.getWidth(), 20)); + this.add(commitTabbedPane, BorderLayout.NORTH); + + cardPane = new JPanel(new CardLayout()); + cardNames = new String[dbmPaneList.size()]; + for (int i = 0; i < this.dbmPaneList.size(); i++) { + if (((BatchSubmitPane) this.dbmPaneList.get(i)).getSubMitName() == null) { + cardNames[i] = ""; + } else { + cardNames[i] = ((BatchSubmitPane) this.dbmPaneList.get(i)).getSubMitName(); + } + cardPane.add((BatchSubmitPane) this.dbmPaneList.get(i), cardNames[i]); + } + this.add(cardPane, BorderLayout.CENTER); + + JPanel btPane = FRGUIPaneFactory.createNormalFlowInnerContainer_S_Pane(); + this.add(btPane, BorderLayout.SOUTH); + + if (javaScriptActionPane != null) { + addCallbackButton = javaScriptActionPane.createCallButton(); + btPane.add(addCallbackButton); + } + } + + + /** + * 更新DBManipulationPane + */ + public void updateCardPane() { + cardNames = new String[dbmPaneList.size()]; + for (int i = 0; i < this.dbmPaneList.size(); i++) { + if (((BatchSubmitPane) this.dbmPaneList.get(i)).getSubMitName() == null) { + cardNames[i] = ""; + } else { + cardNames[i] = ((BatchSubmitPane) this.dbmPaneList.get(i)).getSubMitName(); + } + cardPane.add((BatchSubmitPane) this.dbmPaneList.get(i), cardNames[i]); + } + CardLayout cardLayout = (CardLayout) cardPane.getLayout(); + cardLayout.show(cardPane, cardNames[commitTabbedPane.getSelectedIndex()]); + } + + public void setList(List list) { + this.dbmPaneList = list; + } + + /** + * 新建DBManipulationPane + * + * @return 新建的DBManipulationPane + */ + public BatchSubmitPane createDBManipulationPane() { + BatchSubmitPane db = new BatchSubmitPane(); +// BatchSubmitPane db = javaScriptActionPane.createDBManipulationPane(); + db.populateBean(null); + dbmPaneList.add(db); + return db; + + } + + /** + * 窗口名称 + * + * @return 返回窗口名称 + */ + public String title4PopupWindow() { + return Inter.getLocText("Performance-plugin_submitbatch_name"); + } + + /** + * 界面重置 + */ + public void reset() { + if (javaScriptActionPane != null) { + this.javaScriptActionPane.setCall(null); + } + //重置后只保留,只留第一个tab + while (dbmPaneList.size() > 1) { + dbmPaneList.remove(1); + } + ((BatchSubmitPane) dbmPaneList.get(0)).populateBean(null); + } + + @Override + /** + * 将JavaBean内的数据输出至界面上 + */ + public void populateBean(BatchCommit2DBJavaScript commit2db) { + if (commit2db == null) { + reset(); + return; + } + //先把原来的list清除,然后再根据传入参数重新add + dbmPaneList.clear(); + if (javaScriptActionPane != null) { + this.javaScriptActionPane.setCall(commit2db.getCallBack()); + } + for (int i = 0; i < commit2db.getDBManipulation().size(); i++) { + BatchSubmitPane dbmp = new BatchSubmitPane(); +// BatchSubmitPane dbmp = javaScriptActionPane.createDBManipulationPane(); + dbmp.populateBean((SubmitMain) commit2db.getDBManipulation().get(i)); + dbmPaneList.add(dbmp); + } + commitTabbedPane.refreshTab(); + } + + /** + * 更新数据层JavaBean + * + * @return 返回JavaBean + */ + public BatchCommit2DBJavaScript updateBean() { + BatchCommit2DBJavaScript commit2dbJavaScript = new BatchCommit2DBJavaScript(); + + List dbmaniList = new ArrayList(); + for (int i = 0; i < this.dbmPaneList.size(); i++) { + BatchSubmitPane dbmpane = (BatchSubmitPane) this.dbmPaneList.get(i); + if (i > dbmPaneList.size() - 1) { + dbmPaneList.add(dbmpane); + } + SubmitMain dbManipulation = dbmpane.updateBean(); + dbmaniList.add(dbManipulation); + } + commit2dbJavaScript.setDBManipulation(dbmaniList); + + if (javaScriptActionPane != null) { + commit2dbJavaScript.setCallBack(this.javaScriptActionPane.getCall()); + } + + return commit2dbJavaScript; + } + + /** + * 判断是否是能接受的数据类型 + * + * @param ob 对象 + * @return 返回是否是能接受的数据类型 + */ + public boolean accept(Object ob) { + return ob instanceof BatchCommit2DBJavaScript; + } +} diff --git a/designer/src/com/fr/design/write/submit/batch/BatchCommitTabbedPane.java b/designer/src/com/fr/design/write/submit/batch/BatchCommitTabbedPane.java new file mode 100644 index 0000000000..4c1bcaddae --- /dev/null +++ b/designer/src/com/fr/design/write/submit/batch/BatchCommitTabbedPane.java @@ -0,0 +1,529 @@ +package com.fr.design.write.submit.batch; + +import com.fr.base.BaseUtils; +import com.fr.design.constants.UIConstants; +import com.fr.design.gui.ibutton.UIButton; +import com.fr.general.ComparatorUtils; +import com.fr.general.Inter; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.awt.geom.*; + +/** + * Created by loy on 16/8/22. + */ +public class BatchCommitTabbedPane extends JComponent implements MouseListener, MouseMotionListener { + private Icon closeIcon = BaseUtils.readIcon("com/fr/design/images/gui/tab_delete.png"); + private static final Icon ADD_NORMAL = BaseUtils.readIcon("com/fr/design/images/gui/tab_add_normal.png"); + private static final Icon ADD_OVER = BaseUtils.readIcon("com/fr/design/images/gui/tab_add_hover.png"); + private static final Icon ADD_CLICK = BaseUtils.readIcon("com/fr/design/images/gui/tab_add_click.png"); + private static final Image DESIGN_IMAGE = BaseUtils.readImage("com/fr/design/images/sheet/left_right_btn.png"); + private static final Icon LEFT_ICON = BaseUtils.createIcon(DESIGN_IMAGE, 0, 0, 14, 14); + private static final Icon RIGHT_ICON = BaseUtils.createIcon(DESIGN_IMAGE, 14, 0, 14, 14); + private static final Icon DISABLED_LEFT_ICON = BaseUtils.createIcon(DESIGN_IMAGE, 0, 14, 14, 14); + private static final Icon DISABLED_RIGHT_ICON = BaseUtils.createIcon(DESIGN_IMAGE, 14, 14, 14, 14); + private Icon addIcon = ADD_NORMAL; + + private static final int TOOLBAR_HEIGHT = 16; // 按钮高度 + private static final int GAP = 5; //间隔 + private static final int SMALLGAP = 3; + + private static final int FIRST_TAB_POSITION = 20; + + + // 左移和右移按钮 + private UIButton leftButton; + private UIButton rightButton; + + private JPanel buttonPane; + + private java.util.List dbManipulationPaneList ; + + private BatchCommit2DBJavaScriptPane commit2DBJavaScriptPane; + + // 能显示的tab个数 + private int showCount = 0; + + //选了30度和60度的特殊角度的x,y作为经过的两个点的坐标 + private double specialLocation1 = 2.5; + private double specialLocation2 = 4.330127; + + private int mouseOveredIndex = -1; + + private int selectedIndex = -1; + + private static final double CORNOR_RADIUS = 5.0; + + + //tab栏可以放下的每个tab的实际宽度 + private int tabWidth = 70; + + + //当前标签页栏存放的所有标签页的index + private int scrollIndex = 0; + private int lastOneIndex = 0; + + //添加标签位置 + private int addX = -1; + private int addY = -1; + + + //各删除标签位置 + private int[] closeIconStartX; + + + public BatchCommitTabbedPane(BatchCommit2DBJavaScriptPane commit2DBJavaScriptPane, java.util.List dbManipulationPaneList ){ + this.commit2DBJavaScriptPane = commit2DBJavaScriptPane; + this.dbManipulationPaneList = dbManipulationPaneList; + this.setLayout(new BorderLayout(0, 0)); + this.addMouseListener(this); + this.addMouseMotionListener(this); + this.setBorder(null); + this.setForeground(new Color(99, 99, 99)); + leftButton = new UIButton(LEFT_ICON) { + public Dimension getPreferredSize() { + return new Dimension(super.getPreferredSize().width, TOOLBAR_HEIGHT); + } + }; + leftButton.set4ToolbarButton(); + leftButton.setDisabledIcon(DISABLED_LEFT_ICON); + rightButton = new UIButton(RIGHT_ICON) { + public Dimension getPreferredSize() { + return new Dimension(super.getPreferredSize().width, TOOLBAR_HEIGHT); + } + }; + rightButton.set4ToolbarButton(); + rightButton.setDisabledIcon(DISABLED_RIGHT_ICON); + buttonPane = new JPanel(new BorderLayout(3, 0)); + buttonPane.add(rightButton, BorderLayout.EAST); + buttonPane.add(leftButton, BorderLayout.CENTER); + this.add(buttonPane, BorderLayout.EAST); + leftButton.addActionListener(createLeftButtonActionListener()); + rightButton.addActionListener(createRightButtonActionListener()); + + } + + private ActionListener createRightButtonActionListener(){ + return new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + int tabCount = getTabCount(); + if (lastOneIndex < tabCount && lastOneIndex + showCount <= tabCount) { + scrollIndex += showCount; + lastOneIndex += showCount; + selectedIndex = lastOneIndex; + } else if(lastOneIndex < tabCount && lastOneIndex + showCount > tabCount){ + lastOneIndex = tabCount -1; + scrollIndex = lastOneIndex - showCount; + selectedIndex = lastOneIndex; + } + repaint(); + } + }; + } + + private ActionListener createLeftButtonActionListener() { + return new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + if(scrollIndex >= showCount) { + scrollIndex -= showCount; + selectedIndex = scrollIndex; + lastOneIndex -= showCount; + } else if (scrollIndex > 0 && scrollIndex< showCount){ + scrollIndex =0; + selectedIndex = 0; + lastOneIndex = showCount; + + } + repaint(); + } + }; + } + + private void checkButton(boolean buttonEnabled) { + leftButton.setEnabled(buttonEnabled); + rightButton.setEnabled(buttonEnabled); + } + + public int getSelectedIndex(){ + return selectedIndex; + } + + public void paintComponent(Graphics g){ + super.paintComponent(g); + double maxWidth = getWidth() - buttonPane.getWidth(); + Graphics2D g2d = (Graphics2D) g; + paintBackgroundAndLine(g2d, maxWidth); + + } + + private void paintBackgroundAndLine(Graphics2D g2d, double maxWidth) { + //能画的个数 + showCount = (int) (maxWidth) / tabWidth; + //计算开始画的最小模板index和最大模板index + if (selectedIndex >= dbManipulationPaneList.size()) { + selectedIndex = dbManipulationPaneList.size() - 1; + } + if (selectedIndex < 0) { + selectedIndex = 0; + } + calMinAndMaxIndex(); + closeIconStartX = new int[lastOneIndex - scrollIndex + 1]; + + int startX = 0; + //从可以开始展示在tab面板上的tab开始画 + for (int i = scrollIndex; i <= lastOneIndex; i++) { + BatchSubmitPane dbManipulationPane = (BatchSubmitPane)dbManipulationPaneList.get(i); + String name ; + if (dbManipulationPane.getSubMitName() != null){ + name = dbManipulationPane.getSubMitName(); + } else { + name = createName(); + dbManipulationPane.setSubMitName(name); + } + if (i == selectedIndex) { + closeIconStartX[i - scrollIndex] = paintSelectedTab(g2d, startX,name, i); + } else { + closeIconStartX[i - scrollIndex] = paintUnSelectedTab(g2d,startX,name,i); + } + startX += tabWidth; + } + paintUnderLine(startX, maxWidth, g2d); + addX = startX + GAP; + addIcon.paintIcon(this,g2d,addX,0); + checkButton(getTabCount() > showCount); + } + + public int getTabCount(){ + return dbManipulationPaneList.size(); + } + + private String createName(){ + String prefix = Inter.getLocText("FR-Designer-CommitTab_Submit"); + int count = getTabCount(); + while (true) { + //从提交1开始 + count = count == 0 ? 1 : count; + String newName = prefix + count; + boolean repeated = false; + for (int i= 0;i < getTabCount();i++) { + if (ComparatorUtils.equals( ((BatchSubmitPane)dbManipulationPaneList.get(i)).getSubMitName(), newName)) { + repeated = true; + break; + } + } + + if (!repeated) { + return newName; + } + + count++; + } + } + + + + private void paintUnderLine(double startX, double maxWidth, Graphics2D g2d) { + //画下面的那条线 + if (startX < maxWidth) { + GeneralPath generalPath = new GeneralPath(Path2D.WIND_EVEN_ODD, 2); + generalPath.moveTo((float) startX, (float) (getHeight() - 1)); + generalPath.lineTo((float) maxWidth, (float) (getHeight() - 1)); + g2d.fill(generalPath); + g2d.setPaint(UIConstants.LINE_COLOR); + g2d.draw(new Line2D.Double((float) startX, (float) (getHeight() - 1), (float) maxWidth , (float) (getHeight() - 1))); + } + } + + + private void calMinAndMaxIndex() { //如果个数大于最多能容纳的个数,则多余的进行处理 + + if (dbManipulationPaneList.size() > showCount) { + + if (selectedIndex >= lastOneIndex) { //所点击列表中的标签页处在标签页栏最后一个标签页之后,则标签页栏左移至所点击标签页出现 + scrollIndex = selectedIndex - showCount + 1; + lastOneIndex = selectedIndex; + if (scrollIndex <= 0) { + scrollIndex = 0; + lastOneIndex = showCount - 1; + } + } else if (selectedIndex <= scrollIndex) { //所点击列表中的标签页处在标签页栏第一个标签页之前,则标签页栏右移至所点击标签页出现 + + scrollIndex = selectedIndex; + lastOneIndex = scrollIndex + showCount - 1; + if (lastOneIndex > dbManipulationPaneList.size() - 1) { + lastOneIndex = dbManipulationPaneList.size() - 1; + } + } else { + if (selectedIndex >= dbManipulationPaneList.size() - 1) { + selectedIndex = dbManipulationPaneList.size() - 1; + lastOneIndex = selectedIndex; + scrollIndex = selectedIndex - showCount + 1; + } else { + lastOneIndex = scrollIndex + showCount - 1; + if (lastOneIndex > dbManipulationPaneList.size() - 1) { + lastOneIndex = dbManipulationPaneList.size() - 1; + } + } + } + } else { + scrollIndex = 0; + lastOneIndex = dbManipulationPaneList.size() - 1; + } + } + + + /** + * 画选中的tab + * + * @param g2d + * @param sheetName + * @return + */ + private int paintSelectedTab(Graphics2D g2d,int startX, String sheetName, int selfIndex) { + double[] x = {startX, startX, startX + tabWidth, startX + tabWidth, startX}; + double[] y = {-1, getHeight(), getHeight(), -1, -1}; + RoundRectangle2D.Double rect1 = new RoundRectangle2D.Double(startX, 1, this.getWidth(), this.getHeight(), 7, 7); + g2d.setPaint(new GradientPaint(1, 1, new Color(255, 255, 255), 1, getHeight() - 1, UIConstants.NORMAL_BACKGROUND)); + + GeneralPath generalPath = new GeneralPath(Path2D.WIND_EVEN_ODD, x.length); + generalPath.moveTo((float) x[0] + CORNOR_RADIUS, (float) y[0]); + generalPath.curveTo(((float) x[0] + CORNOR_RADIUS - specialLocation1), (y[0] + CORNOR_RADIUS - specialLocation2), ((float) x[0] + CORNOR_RADIUS - specialLocation2), (y[0] + CORNOR_RADIUS - specialLocation1), (double) x[0], y[0] + CORNOR_RADIUS); + + for (int index = 1; index <= 2; index++) { + generalPath.lineTo((float) x[index], (float) y[index]); + } + + generalPath.lineTo((float) x[3], (float) y[3] + CORNOR_RADIUS); + generalPath.curveTo(((float) x[3] - CORNOR_RADIUS + specialLocation1), ((float) y[3] + CORNOR_RADIUS - specialLocation2), ((float) x[3] - CORNOR_RADIUS + specialLocation2), ((float) y[3] + CORNOR_RADIUS - specialLocation1), (float) x[3] - CORNOR_RADIUS, (float) y[3]); + generalPath.lineTo((float) x[0] + CORNOR_RADIUS, (float) y[0]); + + generalPath.closePath(); + g2d.fill(generalPath); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g2d.setPaint(UIConstants.LINE_COLOR); + g2d.draw(new Arc2D.Double(x[0], y[0], CORNOR_RADIUS * 2, CORNOR_RADIUS * 2, 90, 90, 0)); + g2d.draw(new Line2D.Double(x[0], y[0] + CORNOR_RADIUS, x[1], y[1])); + g2d.draw(new Line2D.Double(x[1], y[1], x[2], y[2])); + g2d.draw(new Line2D.Double(x[2], y[2], x[3], y[3] + CORNOR_RADIUS)); + g2d.draw(new Line2D.Double(x[0] + 3 ,0,x[2] - 3,0)); + g2d.draw(new Arc2D.Double(x[3] - CORNOR_RADIUS * 2, y[3], CORNOR_RADIUS * 2, CORNOR_RADIUS * 2, 90, -90, 0)); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); + // 画字符 + g2d.setPaint(getForeground()); + g2d.drawString(sheetName, startX + 2 * GAP, getHeight()-GAP); + int closePosition = startX + tabWidth - closeIcon.getIconWidth() - SMALLGAP; + int closeY = (getHeight() - closeIcon.getIconHeight()) / 2; + if (canClose() && mouseOveredIndex == selfIndex){ + closeIcon.paintIcon(this, g2d, closePosition, closeY); + } + return closePosition; + + } + + /** + * 画没有选中的tab + * + * @param g2d + * @param startX + * @param sheetName + * @return + */ + private int paintUnSelectedTab(Graphics2D g2d, int startX, String sheetName, int selfIndex) { + double[] x = {startX, startX, startX + tabWidth, startX + tabWidth, startX}; + double[] y = {-1, getHeight() - 1, getHeight() - 1, -1, -1}; + if (selfIndex == mouseOveredIndex) { + g2d.setPaint(new GradientPaint(1, 1, new Color(255, 255, 255), 1, getHeight() - 1, UIConstants.NORMAL_BACKGROUND)); + } else { + g2d.setPaint(new GradientPaint(1, 1, UIConstants.NORMAL_BACKGROUND, 1, getHeight() - 1, UIConstants.NORMAL_BACKGROUND)); + } + + + GeneralPath generalPath = new GeneralPath(Path2D.WIND_EVEN_ODD, x.length); + generalPath.moveTo((float) x[0] + CORNOR_RADIUS, (float) y[0]); + generalPath.curveTo(((float) x[0] + CORNOR_RADIUS - specialLocation1), (y[0] + CORNOR_RADIUS - specialLocation2), ((float) x[0] + CORNOR_RADIUS - specialLocation2), (y[0] + CORNOR_RADIUS - specialLocation1), (double) x[0], y[0] + CORNOR_RADIUS); + + for (int index = 1; index <= 2; index++) { + generalPath.lineTo((float) x[index], (float) y[index]); + } + + generalPath.lineTo((float) x[3], (float) y[3] + CORNOR_RADIUS); + generalPath.curveTo(((float) x[3] - CORNOR_RADIUS + specialLocation1), ((float) y[3] + CORNOR_RADIUS - specialLocation2), ((float) x[3] - CORNOR_RADIUS + specialLocation2), ((float) y[3] + CORNOR_RADIUS - specialLocation1), (float) x[3] - CORNOR_RADIUS, (float) y[3]); + generalPath.lineTo((float) x[0] + CORNOR_RADIUS, (float) y[0]); + + generalPath.closePath(); + + g2d.fill(generalPath); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g2d.setPaint(UIConstants.LINE_COLOR); + + g2d.draw(new Arc2D.Double(x[0], y[0], CORNOR_RADIUS * 2, CORNOR_RADIUS * 2, 90, 90, 0)); + g2d.draw(new Line2D.Double(x[0], y[0] + CORNOR_RADIUS, x[1], y[1])); + g2d.draw(new Line2D.Double(x[1], y[1], x[2], y[2])); + g2d.draw(new Line2D.Double(x[2], y[2], x[3], y[3] + CORNOR_RADIUS)); + g2d.draw(new Line2D.Double(x[0] + 3 ,0,x[2] - 3,0)); + g2d.draw(new Arc2D.Double(x[3] - CORNOR_RADIUS * 2, y[3], CORNOR_RADIUS * 2, CORNOR_RADIUS * 2, 90, -90, 0)); + + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); + // 画字符 + g2d.setPaint(getForeground()); + g2d.drawString(sheetName, startX + 2 * GAP, getHeight() - GAP ); + int closeY = (getHeight() - closeIcon.getIconHeight()) / 2; + int closePosition = startX + tabWidth - closeIcon.getIconWidth() - SMALLGAP; + if (canClose() && mouseOveredIndex == selfIndex){ + closeIcon.paintIcon(this, g2d, closePosition, closeY); + } + return closePosition; + } + + + /** + * 鼠标按下 + * @param e 事件 + */ + public void mouseClicked(MouseEvent e) { + } + + /** + * 鼠标按下 + * @param e 事件 + */ + public void mousePressed(MouseEvent e) { + int x = e.getX(), y = e.getY(); + if (addX!= -1 && isOverAddIcon(x, y)){ + addIcon = ADD_CLICK; + commit2DBJavaScriptPane.createDBManipulationPane(); + selectedIndex = dbManipulationPaneList.size()-1; + commit2DBJavaScriptPane.updateCardPane(); + } else if (isOverCloseIcon(x)){ + int re = JOptionPane.showConfirmDialog(SwingUtilities.getWindowAncestor(this), Inter.getLocText("FR-Designer-CommitTab_SureToDelete")+ "?", Inter.getLocText("FR-Designer-CommitTab_Remove") + , JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); + if (re == JOptionPane.OK_OPTION) { + dbManipulationPaneList.remove(getTabIndex(x)); + commit2DBJavaScriptPane.setList(dbManipulationPaneList); + // 删除tab以后,获得第一个tab,再刷新一下,否则画面会停留在已删除的tab上,第一个tab是不可删除的 + selectedIndex = getTabIndex(FIRST_TAB_POSITION); + commit2DBJavaScriptPane.updateCardPane(); + + } + } else if (selectedIndex != getTabIndex(x)){ + selectedIndex = getTabIndex(x); + commit2DBJavaScriptPane.updateCardPane(); + } + repaint(); + } + + /** + * 鼠标离开 + * @param e 事件 + */ + public void mouseReleased(MouseEvent e) { + if(addX != -1 && isOverAddIcon(e.getX(), e.getY())){ + addIcon = ADD_NORMAL; + } + } + + /** + * 鼠标进入 + * @param e 事件 + */ + public void mouseEntered(MouseEvent e) { + } + + /** + * 鼠标离开 + * @param e 事件 + */ + public void mouseExited(MouseEvent e) { + mouseOveredIndex = -1; + repaint(); + } + + /** + * 鼠标拖动 + * @param e 事件 + */ + public void mouseDragged(MouseEvent e) { + } + + /** + * 鼠标移动 + * @param e 事件 + */ + public void mouseMoved(MouseEvent e) { + if(addX!= -1 && isOverAddIcon(e.getX(), e.getY())){ + addIcon = ADD_OVER; + } else { + mouseOveredIndex = getTabIndex(e.getX()); + addIcon = ADD_NORMAL; + } + repaint(); + } + + + /** + * 判断鼠标所在Tab + * @param evtX + * @return index + */ + private int getTabIndex ( int evtX ){ + int x = 0; + for( int i = scrollIndex;i <= lastOneIndex;i++){ + if(evtX > x && evtX <= x + tabWidth ){ + return i; + } + x = x + tabWidth; + } + return -1; + } + + /** + * 判断鼠标是否在添加按钮上 + * @param x 鼠标坐标x + * @param y 鼠标坐标y + * @return 返回鼠标是否在添加按钮上 + */ + private boolean isOverAddIcon(int x, int y){ + int addWidth = addIcon.getIconWidth(),addHeight = addIcon.getIconHeight(); + return x >= addX && x <= addX + addWidth && y > addY && y <= addY + addHeight; + } + + + /** + * 判断鼠标是否在关闭按钮上 + * @param evtX x + * @return 返回鼠标是否在关闭按钮上 + */ + private boolean isOverCloseIcon(int evtX) { + boolean isOverCloseIcon = false; + if( canClose()){ + for (int i = 0; i < closeIconStartX.length; i++) { + if (evtX >= closeIconStartX[i] && evtX <= closeIconStartX[i] + closeIcon.getIconWidth()) { + isOverCloseIcon = true; + break; + } + } + } + return isOverCloseIcon; + } + + /** + * 如果tab只剩下最后一个,则不画删除按钮 + * @return 返回当前tab还可否关闭 + */ + private boolean canClose(){ + return closeIconStartX.length > 1; + } + + /** + * 刷新tab,停留在第一个tab上面 + */ + public void refreshTab(){ + selectedIndex = getTabIndex(FIRST_TAB_POSITION); + commit2DBJavaScriptPane.updateCardPane(); + } +} diff --git a/designer/src/com/fr/design/write/submit/batch/BatchJavaScriptActionProvider.java b/designer/src/com/fr/design/write/submit/batch/BatchJavaScriptActionProvider.java new file mode 100644 index 0000000000..b0d9a2003e --- /dev/null +++ b/designer/src/com/fr/design/write/submit/batch/BatchJavaScriptActionProvider.java @@ -0,0 +1,23 @@ +package com.fr.design.write.submit.batch; + +import com.fr.design.beans.FurtherBasicBeanPane; +import com.fr.design.fun.impl.AbstractJavaScriptActionProvider; +import com.fr.design.javascript.JavaScriptActionPane; +import com.fr.js.JavaScript; + +/** + * Created by loy on 16/8/22. + */ +public class BatchJavaScriptActionProvider extends AbstractJavaScriptActionProvider { + + @Override + public FurtherBasicBeanPane getJavaScriptActionPane() { + return new BatchCommit2DBJavaScriptPane(); + } + + @Override + public FurtherBasicBeanPane getJavaScriptActionPane(JavaScriptActionPane pane) { + return new BatchCommit2DBJavaScriptPane(pane); + } + +} diff --git a/designer/src/com/fr/design/write/submit/batch/BatchSubmitPane.java b/designer/src/com/fr/design/write/submit/batch/BatchSubmitPane.java new file mode 100644 index 0000000000..1d944194a3 --- /dev/null +++ b/designer/src/com/fr/design/write/submit/batch/BatchSubmitPane.java @@ -0,0 +1,1089 @@ +package com.fr.design.write.submit.batch; + +import com.fr.base.BaseUtils; +import com.fr.base.Formula; +import com.fr.base.Parameter; +import com.fr.data.DataConstants; +import com.fr.data.condition.JoinCondition; +import com.fr.data.condition.ListCondition; +import com.fr.data.core.db.dml.Table; +import com.fr.design.actions.UpdateAction; +import com.fr.design.beans.BasicBeanPane; +import com.fr.design.condition.DSColumnLiteConditionPane; +import com.fr.design.constants.UIConstants; +import com.fr.design.data.datapane.ChoosePaneSupportFormula; +import com.fr.design.data.datapane.DataBaseItems; +import com.fr.design.data.tabledata.tabledatapane.FormatExplanationPane; +import com.fr.design.dialog.BasicDialog; +import com.fr.design.dialog.BasicPane; +import com.fr.design.dialog.DialogActionAdapter; +import com.fr.design.editor.ValueEditorPane; +import com.fr.design.editor.ValueEditorPaneFactory; +import com.fr.design.editor.editor.Editor; +import com.fr.design.gui.ibutton.UIButton; +import com.fr.design.gui.icheckbox.UICheckBox; +import com.fr.design.gui.icombobox.UIComboBox; +import com.fr.design.gui.icombobox.UIComboBoxRenderer; +import com.fr.design.gui.ilable.UILabel; +import com.fr.design.gui.ilist.CheckBoxList; +import com.fr.design.gui.itree.refreshabletree.ExpandMutableTreeNode; +import com.fr.design.javascript.JavaScriptActionPane; +import com.fr.design.layout.FRGUIPaneFactory; +import com.fr.design.mainframe.DesignerContext; +import com.fr.design.scrollruler.ModLineBorder; +import com.fr.design.utils.gui.GUICoreUtils; +import com.fr.design.write.submit.SubmitJobListPane; +import com.fr.general.ComparatorUtils; +import com.fr.general.DateUtils; +import com.fr.general.Inter; +import com.fr.general.data.Condition; +import com.fr.stable.ColumnRow; +import com.fr.stable.ColumnRowGroup; +import com.fr.stable.fun.Level; +import com.fr.write.NameSubmitJob; +import com.fr.write.batch.SubmitMain; +import com.fr.write.config.*; + +import javax.swing.*; +import javax.swing.table.AbstractTableModel; +import javax.swing.table.DefaultTableCellRenderer; +import javax.swing.table.TableCellEditor; +import javax.swing.table.TableColumn; +import javax.swing.tree.DefaultTreeCellRenderer; +import javax.swing.tree.DefaultTreeModel; +import java.awt.*; +import java.awt.event.*; +import java.util.ArrayList; +import java.util.Date; +import java.util.EventObject; + +/** + * Created by loy on 16/8/12. + */ +public class BatchSubmitPane extends BasicBeanPane implements Level { + private static final Image ICODS_IMAGE = BaseUtils.readImage("/com/fr/web/core/css/images/icons.png"); + private static final Icon HEIP_ICON = BaseUtils.createIcon(ICODS_IMAGE, 193, 1, 14, 14); + private static final int DEFAULT_RETURN_VALUE = 4; + public BatchSubmitPane.KeyColumnNameValueTable keyColumnValuesTable; + private UIComboBox columnsComboBox; // 用于编辑ColumnName的Editor + private UICheckBox UpdateCheckBox; + private JPanel checkBoxUpdatePane; + /* + * 记录当前选取的DS & Table对应的ColumnName[] + * alex:以前每当tableNameComboBox改变就刷新columnsComboBox,这样每输入一个文字就会刷一遍,很不好 + */ + private BatchSubmitPane.ColumnName[] currentColumnNames = null; + + // 支持公式输入的数据表选择面板 + private ChoosePaneSupportFormula chooseTable; + + private UIComboBox dmlConfigComboBox = null; + + // 提交事件 + private NameSubmitJob[] jobs = null; + + // 提交条件 + private Condition condition = null; + + private JTree conditionsTree; + + private Editor[] v_Types; + + protected JavaScriptActionPane parentPane; + + private int keyColumnWidth = 100; + private int resizeColumnCount = 4; + private int btnWidth = 110; + private int btnHeight = 20; + private String subMitName; + + private static final String[] DML_CONFIG_TYPES = new String[]{ + Inter.getLocText(new String[]{"Smart", "Submit"}), + Inter.getLocText(new String[]{"Delete", "Submit"}), + Inter.getLocText(new String[]{"Insert", "Submit"}), + Inter.getLocText(new String[]{"Update", "Submit"})}; + + /** + * 无单元格。没有智能添加单元格等按钮 + * 有单元格的参见其子类SmartInsertDBManipulationPane + */ + public BatchSubmitPane() { + this(ValueEditorPaneFactory.extendedCellGroupEditors()); + } + + public BatchSubmitPane(Editor[] v_Types) { + this.setLayout(FRGUIPaneFactory.createBorderLayout()); + this.v_Types = v_Types; + + JPanel northPane = FRGUIPaneFactory.createBorderLayout_S_Pane(); + this.add(northPane, BorderLayout.NORTH); + + dmlConfigComboBox = new UIComboBox(DML_CONFIG_TYPES); + + JPanel typePane = GUICoreUtils.createFlowPane(new Component[]{new UILabel(Inter.getLocText(new String[]{"Choose", "Type"}) + ":"), dmlConfigComboBox}, + FlowLayout.LEFT, 10); + typePane.setBorder(BorderFactory.createTitledBorder(new ModLineBorder(ModLineBorder.TOP), Inter.getLocText(new String[]{"Submit", "Type"}))); + northPane.add(typePane, BorderLayout.NORTH); + + chooseTable = new ChoosePaneSupportFormula(); + chooseTable.setBorder(BorderFactory.createTitledBorder(new ModLineBorder(ModLineBorder.TOP), Inter.getLocText("FR-Base_Table"))); + chooseTable.setTableNameComboBoxPopSize(160, 320); + + northPane.add(chooseTable, BorderLayout.CENTER); + + // peter:编辑的TablePane + JPanel editTablePane = FRGUIPaneFactory.createBorderLayout_S_Pane(); + this.add(editTablePane, BorderLayout.CENTER); + editTablePane.setBorder(BorderFactory.createTitledBorder(new ModLineBorder(ModLineBorder.TOP), Inter.getLocText("FR-Base_Value"))); + + keyColumnValuesTable = new BatchSubmitPane.KeyColumnNameValueTable(); + editTablePane.add(new JScrollPane(this.keyColumnValuesTable), BorderLayout.CENTER); + keyColumnValuesTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); + keyColumnValuesTable.setPreferredScrollableViewportSize(new Dimension(280, 180)); + keyColumnValuesTable.setShowHorizontalLines(true); + + initJTableColumn(); + + addButtons(editTablePane); + + addBottomPane(); + + addListeners(); + } + + public void setSubMitName(String subMitName) { + this.subMitName = subMitName; + } + + public String getSubMitName() { + return this.subMitName; + } + + private void addButtons(JPanel editTablePane) { + // alex:添加操作按钮 + UpdateAction[] actions = this.getActions(); + if (actions != null && actions.length > 0) { + JPanel controlBtnPane = new JPanel(new GridLayout(actions.length + 1, 1, 4, 4)); + editTablePane.add(GUICoreUtils.createBorderPane(controlBtnPane, BorderLayout.NORTH), BorderLayout.EAST); + + for (int i = 0; i < actions.length; i++) { + controlBtnPane.add(new UIButton(actions[i])); + } + checkBoxUpdatePane = new JPanel(new BorderLayout(0, 0)); + checkBoxUpdatePane.setPreferredSize(new Dimension(120, 20)); + controlBtnPane.add(checkBoxUpdatePane); + + UpdateCheckBox = new UICheckBox(Inter.getLocText("RWA-NotChange_Unmodified")); + UIButton helpButton = new UIButton(HEIP_ICON); + helpButton.setToolTipText(Inter.getLocText("FR-Base_Help")); + helpButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + FormatExplanationPane formatExplanation = new FormatExplanationPane(Inter.getLocText("FR-Base_Help"), Inter.getLocText("FR-Designer_RWA-Help"), 12f); + BasicDialog dlg = formatExplanation.showMediumWindow(SwingUtilities.getWindowAncestor(BatchSubmitPane.this), + new DialogActionAdapter() { + }); + dlg.setVisible(true); + } + }); + helpButton.set4ToolbarButton(); + checkBoxUpdatePane.add(UpdateCheckBox, BorderLayout.WEST); + checkBoxUpdatePane.add(helpButton, BorderLayout.EAST); + } + } + + protected void updateUpdateCheckBoxEnable() { + BatchSubmitPane.KeyColumnTableModel model = (BatchSubmitPane.KeyColumnTableModel) keyColumnValuesTable.getModel(); + if (model.getRowCount() == 0) { + setUpdateCheckBoxEnable(false); + UpdateCheckBox.setSelected(true); + return; + } + ArrayList columnObjects = new ArrayList(); + for (int i = 0; i < model.getRowCount(); i++) { + columnObjects.add(model.getKeyColumnNameValue(i).cv.getObj()); + } + for (int i = 0; i < columnObjects.size(); i++) { + Object ob = columnObjects.get(i); + if (!(ob instanceof ColumnRow || ob instanceof ColumnRowGroup)) { + setUpdateCheckBoxEnable(false); + UpdateCheckBox.setSelected(false); + return; + } + } + setUpdateCheckBoxEnable(true); + } + + private void setUpdateCheckBoxEnable(boolean b) { + UpdateCheckBox.setEnabled(b); + } + + private void addBottomPane() { + JPanel eventPane = FRGUIPaneFactory.createNormalFlowInnerContainer_S_Pane(); + eventPane.add(addEventButton()); + + JPanel conditionPane = this.createConditionPane(); + + JPanel btPane = new JPanel(FRGUIPaneFactory.createBorderLayout()); + btPane.add(eventPane, BorderLayout.CENTER); + btPane.add(conditionPane, BorderLayout.NORTH); + this.add(btPane, BorderLayout.SOUTH); + } + + private UIButton addEventButton() { + UIButton addSubmitEventButton = new UIButton(Inter.getLocText("FR-Designer_Set_Submit_Event")); + addSubmitEventButton.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + final SubmitJobListPane jobsPane = createSubmitJobListPane(); + + jobsPane.populate(jobs); + BasicDialog dialog = jobsPane.showWindow(SwingUtilities.getWindowAncestor(BatchSubmitPane.this), new DialogActionAdapter() { + @Override + public void doOk() { + super.doOk(); + jobs = jobsPane.updateDBManipulation(); + } + }); + dialog.setVisible(true); + } + + }); + return addSubmitEventButton; + } + + private JPanel createConditionPane() { + JPanel conditionPane = new JPanel(); + conditionPane.setPreferredSize(createConditionPanePreferredSize()); + + setBorderAndLayout(conditionPane); + + conditionsTree = new JTree(new DefaultTreeModel(new ExpandMutableTreeNode())); + conditionsTree.setRootVisible(false); + conditionsTree.setShowsRootHandles(true); + conditionsTree.setBackground(UIConstants.NORMAL_BACKGROUND); + conditionsTree.setForeground(UIConstants.NORMAL_BACKGROUND); + DefaultTreeCellRenderer cr = (DefaultTreeCellRenderer) conditionsTree.getCellRenderer(); + cr.setForeground(UIConstants.NORMAL_BACKGROUND); + JScrollPane jp = new JScrollPane(conditionsTree); + addComponent(conditionPane, jp); + + UIButton addSubmitConditionButton = new UIButton(Inter.getLocText("FR-Designer_Set_Submit_Condition")); + addSubmitConditionButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + final DSColumnLiteConditionPane conditionPane = new DSColumnLiteConditionPane(); + String[] columns = chooseTable.currentColumnNames(); + if (columns != null && columns.length > 0) { + conditionPane.populateColumns(chooseTable.currentColumnNames()); + } + + conditionPane.populateBean(condition); + BasicDialog dialog = conditionPane.showWindow(DesignerContext.getDesignerFrame(), new DialogActionAdapter() { + @Override + public void doOk() { + super.doOk(); + condition = conditionPane.updateBean(); + refreshConditionList(); + } + }); + dialog.setVisible(true); + } + }); + + JPanel controlBtnPane = new JPanel(new GridLayout(1, 1, 4, 4)); + controlBtnPane.setPreferredSize(createControlBtnPanePreferredSize()); + conditionPane.add(GUICoreUtils.createBorderPane(controlBtnPane, BorderLayout.NORTH), setControlBtnPanePosition()); + controlBtnPane.add(addSubmitConditionButton); + + return conditionPane; + } + + protected void setBorderAndLayout(JPanel jPanel) { + jPanel.setLayout(FRGUIPaneFactory.createBorderLayout()); + jPanel.setBorder(BorderFactory.createTitledBorder( + new ModLineBorder(ModLineBorder.TOP), Inter.getLocText(new String[]{"Submit", "Condition"}))); + } + + protected void addComponent(JPanel mainPane, JScrollPane addPane) { + mainPane.add(addPane, BorderLayout.CENTER); + } + + + protected Dimension createConditionPanePreferredSize() { + return new Dimension(454, 80); + } + + protected Dimension createControlBtnPanePreferredSize() { + return new Dimension(110, 20); + } + + protected String setControlBtnPanePosition() { + return BorderLayout.EAST; + } + + private void refreshConditionList() { + DefaultTreeModel defaultTreeModel = (DefaultTreeModel) conditionsTree.getModel(); + ExpandMutableTreeNode rootTreeNode = (ExpandMutableTreeNode) defaultTreeModel.getRoot(); + rootTreeNode.setUserObject(new JoinCondition(DataConstants.AND, new ListCondition())); + rootTreeNode.removeAllChildren(); + Condition liteCondition = this.condition == null ? new ListCondition() : this.condition; + if (liteCondition instanceof ListCondition) { + ListCondition listCondition = (ListCondition) liteCondition; + int joinConditionCount = listCondition.getJoinConditionCount(); + for (int i = 0; i < joinConditionCount; i++) { + addLiteConditionToListCondition(rootTreeNode, listCondition.getJoinCondition(i)); + } + } else { + ExpandMutableTreeNode newTreeNode = new ExpandMutableTreeNode(new JoinCondition(DataConstants.AND, liteCondition)); + rootTreeNode.add(newTreeNode); + } + defaultTreeModel.reload(rootTreeNode); + rootTreeNode.expandCurrentTreeNode(conditionsTree); + } + + private void addLiteConditionToListCondition(ExpandMutableTreeNode parentTreeNode, JoinCondition joinCondition) { + ExpandMutableTreeNode newTreeNode = new ExpandMutableTreeNode(joinCondition); + parentTreeNode.add(newTreeNode); + Condition liteCondition = joinCondition.getCondition(); + if (liteCondition instanceof ListCondition) { + ListCondition listCondition = (ListCondition) liteCondition; + int joinConditionCount = listCondition.getJoinConditionCount(); + for (int i = 0; i < joinConditionCount; i++) { + addLiteConditionToListCondition(newTreeNode, listCondition.getJoinCondition(i)); + } + } + } + + private void addListeners() { + dmlConfigComboBox.addItemListener(new ItemListener() { + @Override + public void itemStateChanged(ItemEvent e) { + if (e.getStateChange() == ItemEvent.SELECTED) { + Object ob = e.getItem(); + if ((ComparatorUtils.equals(ob, DML_CONFIG_TYPES[0])) || ComparatorUtils.equals(ob, DML_CONFIG_TYPES[3])) { + checkBoxUpdatePane.setVisible(true); + } else if ((ComparatorUtils.equals(ob, DML_CONFIG_TYPES[1])) || ComparatorUtils.equals(ob, DML_CONFIG_TYPES[2])) { + checkBoxUpdatePane.setVisible(false); + } + } + } + }); + + /* + * 当DS & Table改变时要刷新ColumnsComboBox.model,不能用ItemListener, + * 因为tableNameComboBox是可以编辑的,每写一个文字就要连,太频繁了 + * 也不能在tableNameComboBox.focusLost事件时,好像没用 + */ + keyColumnValuesTable.addFocusListener(new FocusAdapter() { + + public void focusGained(FocusEvent e) { + refreshColumnsComboBox(); + } + + }); + } + + protected UpdateAction[] getActions() { + return new UpdateAction[]{new BatchSubmitPane.SmartAddFieldsAction(), new BatchSubmitPane.AddFieldAction(), new BatchSubmitPane.RemoveFieldAction()}; + } + + protected SubmitJobListPane createSubmitJobListPane() { + return new SubmitJobListPane(); + } + + public void setParentJavaScriptActionPane(JavaScriptActionPane jsPane) { + this.parentPane = jsPane; + } + + @Override + protected String title4PopupWindow() { + return "DB"; + } + + @Override + public int currentAPILevel() { + return 1; + } + + protected class SmartAddFieldsAction extends UpdateAction { + public SmartAddFieldsAction() { + this.setName(Inter.getLocText("RWA-Smart_Add_Fields")); + } + + public void actionPerformed(ActionEvent evt) { + + BasicPane bPane = new BasicPane() { + @Override + protected String title4PopupWindow() { + return Inter.getLocText("RWA-Smart_Add_Fields"); + } + }; + bPane.setLayout(FRGUIPaneFactory.createBorderLayout()); + + final CheckBoxList list = new CheckBoxList(currentColumnNames(), CheckBoxList.SelectedState.ALL, Inter.getLocText("FR-Designer_Chart_Field_Name")) { + public String value2Text(Object value) { + if (value instanceof BatchSubmitPane.ColumnName) { + return ((BatchSubmitPane.ColumnName) value).name; + } + + return super.value2Text(value); + } + }; + bPane.add(new JScrollPane(list), BorderLayout.CENTER); + + BasicDialog dlg = bPane.showSmallWindow(SwingUtilities.getWindowAncestor(BatchSubmitPane.this), new DialogActionAdapter() { + public void doOk() { + addFields(list); + updateUpdateCheckBoxEnable(); + } + }); + dlg.setVisible(true); + } + } + + private void addFields(CheckBoxList list) { + BatchSubmitPane.KeyColumnTableModel model = (BatchSubmitPane.KeyColumnTableModel) keyColumnValuesTable.getModel(); + java.util.List keyColumnNameValueList = new ArrayList(); + keyColumnNameValueList.clear(); + for (int i = 0; i < model.getRowCount(); i++) { + keyColumnNameValueList.add(model.getKeyColumnNameValue(i)); + } + model.removeAllKeyColumnNameValue(); + Object[] selected = list.getSelectedValues(); + // Richie:用了存储新的KeyColumnNameValue的List. + java.util.List newKeyColumnNameValueList = new ArrayList(); + if (!keyColumnNameValueList.isEmpty()) { + for (int i = 0; i < selected.length; i++) { + // Richie:先填上空的. + newKeyColumnNameValueList.add(new BatchSubmitPane.KeyColumnNameValue(false, (BatchSubmitPane.ColumnName) selected[i], new BatchSubmitPane.ColumnValue(""), false)); + } + } + // Richie:初始化 + int returnValue = DEFAULT_RETURN_VALUE; + int coverNumber = 0; + for (int i = 0; i < selected.length; i++) { + if (returnValue == 0 || returnValue == 3) { + break; + } + for (int j = 0; j < keyColumnNameValueList.size(); j++) { + if (ComparatorUtils.equals(selected[i], keyColumnNameValueList.get(j).cn)) { + Object[] options = {Inter.getLocText("FR-Designer_Covered_All"), Inter.getLocText("FR-Base_Yes"), Inter.getLocText("FR-Base_No"), Inter.getLocText("FR-Designer_Cover_None")}; + returnValue = JOptionPane.showOptionDialog(BatchSubmitPane.this, keyColumnNameValueList.get(j).cn.name + + Inter.getLocText(new String[]{"Has_Existed", "Want_To_Cover_It"}, new String[]{",", "?"}), + "", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]); + // Richie:全部覆盖 + if (returnValue == 0) { + break; + // Richie:覆盖指定行 + } else if (returnValue == 1) { + coverNumber = i; + // Richie:不覆盖制定行 + } else if (returnValue == 2) { + coverNumber = i; + newKeyColumnNameValueList.remove(i); + newKeyColumnNameValueList.add(i, keyColumnNameValueList.get(j)); + // Richie:全部不覆盖 + } else if (returnValue == 3) { + coverNumber = i; + break; + } + } + } + } + checkTableModel(returnValue, coverNumber, model, selected, keyColumnNameValueList, newKeyColumnNameValueList); + } + + private void checkTableModel(int returnValue, int coverNumber, BatchSubmitPane.KeyColumnTableModel model, Object[] selected, java.util.List keyColumnNameValueList, java.util.List newKeyColumnNameValueList) { + if (returnValue == 0) { + model.removeAllKeyColumnNameValue(); + // Richie:全部覆盖,按selected的长度添加默认的行 + for (int i = 0; i < selected.length; i++) { + model.addKeyColumnNameValue(newKeyColumnNameValueList.get(i)); + } + + } else if (returnValue == 3) { + model.removeAllKeyColumnNameValue(); + // Richie:全部不覆盖,已经存在的就保留,不存在的添加默认行 + for (int i = coverNumber; i < selected.length; i++) { + for (int j = 0; j < keyColumnNameValueList.size(); j++) { + if (ComparatorUtils.equals(selected[i], keyColumnNameValueList.get(j).cn)) { + newKeyColumnNameValueList.remove(i); + newKeyColumnNameValueList.add(i, keyColumnNameValueList.get(j)); + } + } + + } + + for (int i = 0; i < selected.length; i++) { + model.addKeyColumnNameValue(newKeyColumnNameValueList.get(i)); + } + + } else if (returnValue == 1 || returnValue == 2) { + for (int i = 0; i < selected.length; i++) { + model.addKeyColumnNameValue(newKeyColumnNameValueList.get(i)); + } + } + + // Richie:初始化 + if (keyColumnNameValueList.isEmpty()) { + model.removeAllKeyColumnNameValue(); + for (int i = 0; i < selected.length; i++) { + model.addKeyColumnNameValue(new BatchSubmitPane.KeyColumnNameValue(false, (BatchSubmitPane.ColumnName) selected[i], new BatchSubmitPane.ColumnValue(""), false)); + } + } + model.fireTableDataChanged(); + keyColumnValuesTable.validate(); + } + + protected class AddFieldAction extends UpdateAction { + public AddFieldAction() { + this.setName(Inter.getLocText("RWA-Add_Field")); + } + + public void actionPerformed(ActionEvent e) { + + BatchSubmitPane.KeyColumnTableModel model = (BatchSubmitPane.KeyColumnTableModel) keyColumnValuesTable.getModel(); + + model.addKeyColumnNameValue(new BatchSubmitPane.KeyColumnNameValue(false, new BatchSubmitPane.ColumnName(""), new BatchSubmitPane.ColumnValue(""), false)); + updateUpdateCheckBoxEnable(); + model.fireTableDataChanged(); + + keyColumnValuesTable.getSelectionModel().setSelectionInterval(model.getRowCount() - 1, model.getRowCount() - 1); + } + } + + protected class RemoveFieldAction extends UpdateAction { + public RemoveFieldAction() { + this.setName(Inter.getLocText("RWA-Remove_Field")); + } + + public void actionPerformed(ActionEvent evt) { + // DBManipulationPane target = this.getDBManipulationPane(); + + int[] selectedRows = keyColumnValuesTable.getSelectedRows(); + if (selectedRows == null || selectedRows.length == 0) { + return; + } + + int returnVal = JOptionPane.showConfirmDialog(SwingUtilities.getWindowAncestor(BatchSubmitPane.this), + Inter.getLocText("FR-Base_sure_remove_item") + "?", Inter.getLocText("FR-Base_Remove"), JOptionPane.OK_CANCEL_OPTION, + JOptionPane.QUESTION_MESSAGE); + if (returnVal == JOptionPane.OK_OPTION) { + BatchSubmitPane.KeyColumnTableModel keyColumnNameValueTableModel = (BatchSubmitPane.KeyColumnTableModel) keyColumnValuesTable.getModel(); + + // 把selectedRows从小到大排个序,先删大的再删小的 + java.util.Arrays.sort(selectedRows); + for (int i = selectedRows.length - 1; i >= 0; i--) { + keyColumnNameValueTableModel.removeKeyColumnNameValue(selectedRows[i]); + } + updateUpdateCheckBoxEnable(); + keyColumnNameValueTableModel.fireTableDataChanged(); + + // select other one. + if (keyColumnNameValueTableModel.getRowCount() > selectedRows[0]) { + keyColumnValuesTable.getSelectionModel().setSelectionInterval(selectedRows[0], selectedRows[0]); + } else if (keyColumnNameValueTableModel.getRowCount() > 0) { + keyColumnValuesTable.getSelectionModel().setSelectionInterval(0, 0); + } + } + } + } + + /* + * 刷新columnsComboBox + */ + private void refreshColumnsComboBox() { + DefaultComboBoxModel model = (DefaultComboBoxModel) this.columnsComboBox.getModel(); + model.removeAllElements(); + + BatchSubmitPane.ColumnName[] columnNames = currentColumnNames(); + for (int i = 0; i < columnNames.length; i++) { + model.addElement(columnNames[i]); + } + } + + // 得到当前的ColumnName[] + private BatchSubmitPane.ColumnName[] currentColumnNames() { + // ben:清除原有的 + if (currentColumnNames != null) { + currentColumnNames = null; + } + String[] colNames = this.chooseTable.currentColumnNames(); + int len = colNames.length; + currentColumnNames = new BatchSubmitPane.ColumnName[len]; + for (int i = 0; i < len; i++) { + currentColumnNames[i] = new BatchSubmitPane.ColumnName(colNames[i]); + } + if (currentColumnNames == null) { + currentColumnNames = new BatchSubmitPane.ColumnName[0]; + } + return currentColumnNames; + } + + /* + * 设置JTable的Column + */ + private void initJTableColumn() { + TableColumn column0 = this.keyColumnValuesTable.getColumnModel().getColumn(0); + column0.setMaxWidth(50); + + TableColumn column1 = this.keyColumnValuesTable.getColumnModel().getColumn(1); + column1.setCellRenderer(new BatchSubmitPane.ColumnNameTableCellRenderer()); + + TableColumn column2 = this.keyColumnValuesTable.getColumnModel().getColumn(2); + column2.setCellRenderer(new BatchSubmitPane.ColumnValueTableCellRenderer()); + + // 设置column1的editor + columnsComboBox = new UIComboBox(new DefaultComboBoxModel()); + columnsComboBox.setRenderer(new UIComboBoxRenderer() { + + public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { + super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); + + if (value instanceof BatchSubmitPane.ColumnName) { + this.setText(((BatchSubmitPane.ColumnName) value).name); + } + + return this; + } + + }); + column1.setCellEditor(new DefaultCellEditor(columnsComboBox) { + public boolean stopCellEditing() { + if (super.stopCellEditing()) { + return true; + } + + return false; + } + }); + ((DefaultCellEditor) column1.getCellEditor()).setClickCountToStart(2); + + //设置Column 2的Editor + column2.setCellEditor(new BatchSubmitPane.ColumnValueEditor()); + } + + @Override + public void populateBean(SubmitMain submitMain) { + if (submitMain == null) { + submitMain = new SubmitMain(); + } + subMitName = submitMain.getName(); + DMLConfig dmlConfig = submitMain.getDmlConfig(); + + if (dmlConfig != null) { + if (dmlConfig instanceof IntelliDMLConfig) { + dmlConfigComboBox.setSelectedIndex(0); + } else if (dmlConfig instanceof DeleteConfig) { + dmlConfigComboBox.setSelectedIndex(1); + } else if (dmlConfig instanceof InsertConfig) { + dmlConfigComboBox.setSelectedIndex(2); + } else if (dmlConfig instanceof UpdateConfig) { + dmlConfigComboBox.setSelectedIndex(3); + } else { + dmlConfigComboBox.setSelectedIndex(0); + } + jobs = new NameSubmitJob[dmlConfig == null ? 0 : dmlConfig.getSubmitJobCount()]; + for (int i = 0; i < jobs.length; i++) { + jobs[i] = dmlConfig.getSubmitJob(i); + } + condition = dmlConfig.getCondition(); + UpdateCheckBox.setSelected(dmlConfig.isUpdateSelected()); + } else { + jobs = null; + condition = null; + dmlConfigComboBox.setSelectedIndex(0); + UpdateCheckBox.setSelected(false); + } + + String schema = null; + String tableName = null; + Table table = null; + if (dmlConfig != null && dmlConfig.getOriTable() != null) { + table = dmlConfig.getOriTable(); + if (table != null) { + schema = table.getSchema(); + tableName = table.getName(); + } + } + chooseTable.populateBean(new DataBaseItems(submitMain.getDBName(), schema, tableName)); + + populateKeyColumnValueTable(dmlConfig); + + updateUpdateCheckBoxEnable(); + refreshConditionList(); + } + + private void populateKeyColumnValueTable(DMLConfig dmlConfig) { + BatchSubmitPane.KeyColumnTableModel keyColumnNameValueTableModel = (BatchSubmitPane.KeyColumnTableModel) this.keyColumnValuesTable.getModel(); + keyColumnNameValueTableModel.removeAllKeyColumnNameValue(); + + if (dmlConfig != null) { + boolean acceptPara = false; + for (int i = 0; i < v_Types.length; i++) { + if (v_Types[i].accept(new Parameter())) { + acceptPara = true; + break; + } + } + for (int i = 0; i < dmlConfig.getColumnConfigCount(); i++) { + ColumnConfig column = dmlConfig.getColumnConfig(i); + String columnName = column.getColumnName(); + if (!acceptPara && column.getColumnValue() instanceof Parameter) { + // 表单中,将以前的参数转换为公式 + column.setColumnValue(new Formula(((Parameter) column.getColumnValue()).getName())); + } + + BatchSubmitPane.KeyColumnNameValue newColumnNameValue = new BatchSubmitPane.KeyColumnNameValue(column.isKey(), new BatchSubmitPane.ColumnName(columnName), new BatchSubmitPane.ColumnValue(column.getColumnValue()), + column.isSkipUnmodified()); + keyColumnNameValueTableModel.addKeyColumnNameValue(newColumnNameValue); + } + } + keyColumnNameValueTableModel.fireTableDataChanged(); + + // 选择第一个 + if (keyColumnNameValueTableModel.getRowCount() > 0) { + keyColumnValuesTable.getSelectionModel().setSelectionInterval(0, 0); + } + } + + @Override + public SubmitMain updateBean() { + DataBaseItems para = chooseTable.updateBean(true); + SubmitMain submitMain = new SubmitMain(); + submitMain.setName(subMitName); + submitMain.setDBName(para.getDatabaseName()); + + DMLConfig dmlConfig = new IntelliDMLConfig(); + if (dmlConfigComboBox.getSelectedIndex() == 1) { + dmlConfig = new DeleteConfig(); + } else if (dmlConfigComboBox.getSelectedIndex() == 2) { + dmlConfig = new InsertConfig(); + } else if (dmlConfigComboBox.getSelectedIndex() == 3) { + dmlConfig = new UpdateConfig(); + } + + submitMain.setDmlConfig(dmlConfig); + + dmlConfig.setTable(new Table(para.getSchemaName(), para.getTableName())); + + BatchSubmitPane.KeyColumnTableModel keyColumnNameValueTableModel = (BatchSubmitPane.KeyColumnTableModel) this.keyColumnValuesTable.getModel(); + int rowCount = keyColumnNameValueTableModel.keyColumnNameValueList.size(); + for (int i = 0; i < rowCount; i++) { + BatchSubmitPane.KeyColumnNameValue newKeyColumnNameValue = keyColumnNameValueTableModel.keyColumnNameValueList.get(i); + // peter:先添加key column name. + + dmlConfig.addColumnConfig(new ColumnConfig(newKeyColumnNameValue.cn.name, newKeyColumnNameValue.cv.obj, newKeyColumnNameValue.isKey, false)); + } + dmlConfig.setUpdateSelected(UpdateCheckBox.isSelected()); + + if (jobs != null) { + for (int i = 0; i < jobs.length; i++) { + dmlConfig.addSubmitJob(jobs[i]); + } + } + dmlConfig.setCondition(condition); + + return submitMain; + } + + protected class ValuePane extends BasicBeanPane { + ValueEditorPane vPane; + + public ValuePane() { + this(v_Types); + } + + public ValuePane(Editor[] types) { + vPane = new ValueEditorPane(types); + this.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 45)); + this.add(vPane); + vPane.setPreferredSize(new Dimension(220, 25)); + } + + @Override + protected String title4PopupWindow() { + return Inter.getLocText("FR-Designer_Values-Editor"); + } + + public void populateBean(Object object) { + vPane.populate(object); + } + + @Override + public Object updateBean() { + return vPane.update(); + } + + } + + /* + * alex:ColumnValue的编辑器,弹出对话框来编辑...个人觉得不大好 + */ + protected class ColumnValueEditor extends AbstractCellEditor implements TableCellEditor { + /** + * The Swing component being edited. + */ + private UILabel textLabel; + private BatchSubmitPane.ValuePane vPane; + private BasicDialog vPaneDLG; + + protected ColumnValueEditor() { + this(v_Types); + } + + protected ColumnValueEditor(Editor[] types) { + textLabel = new UILabel(); + textLabel.addMouseListener(new MouseAdapter() { + public void mousePressed(MouseEvent e) { + vPaneDLG.setVisible(true); + + } + }); + + vPane = new BatchSubmitPane.ValuePane(types); + vPaneDLG = vPane.showSmallWindow(DesignerContext.getDesignerFrame(), new DialogActionAdapter() { + public void doOk() { + fireEditingStopped(); // Make the renderer + // reappear. + updateUpdateCheckBoxEnable(); + } + + public void doCancel() { + fireEditingCanceled(); + } + }); + } + + /* + * 双击以编辑 + */ + public boolean isCellEditable(EventObject anEvent) { + if (anEvent instanceof MouseEvent) { + return ((MouseEvent) anEvent).getClickCount() >= 2; + } + return true; + } + + public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { + if (value instanceof BatchSubmitPane.ColumnValue) { + vPane.populateBean(((BatchSubmitPane.ColumnValue) value).obj); + + if (((BatchSubmitPane.ColumnValue) value).obj != null) { + textLabel.setText(((BatchSubmitPane.ColumnValue) value).obj.toString()); + } else { + textLabel.setText(""); + } + } + + return textLabel; + } + + public Object getCellEditorValue() { + return new BatchSubmitPane.ColumnValue(vPane.updateBean()); + } + } + + public static class ColumnName { + public String name; + + public ColumnName(String name) { + this.name = name; + } + + public boolean equals(Object obj) { + if (!(obj instanceof BatchSubmitPane.ColumnName)) { + return false; + } + + return ComparatorUtils.equals(this.name, ((BatchSubmitPane.ColumnName) obj).name); + } + } + + public static class ColumnValue { + public Object obj; + + public ColumnValue(Object obj) { + this.obj = obj; + } + + public Object getObj() { + return obj; + } + + public boolean equals(Object obj) { + if (!(obj instanceof BatchSubmitPane.ColumnValue)) { + return false; + } + + return ComparatorUtils.equals(this.obj, ((BatchSubmitPane.ColumnValue) obj).obj); + } + } + + protected static class KeyColumnNameValueTable extends JTable { + + public KeyColumnNameValueTable() { + super(new BatchSubmitPane.KeyColumnValueTableModel(null)); + } + + + public BatchSubmitPane.KeyColumnTableModel getTableModel4SmartAddCell() { + return new BatchSubmitPane.KeyColumnValueTableModel((BatchSubmitPane.KeyColumnTableModel) this.getModel()); + } + + } + + protected abstract static class KeyColumnTableModel extends AbstractTableModel { + public static final String RAW_KEY = Inter.getLocText("FR-Base_RWA-Key"); + public static final String COLUMN = Inter.getLocText("FR-Base_Column"); + public static final String VALUE = Inter.getLocText("FR-Base_Value"); + + public static final String[] COLUMN_NAMES = new String[]{RAW_KEY, COLUMN, VALUE}; + + protected java.util.List keyColumnNameValueList = new ArrayList(); + + public KeyColumnTableModel(BatchSubmitPane.KeyColumnTableModel model) { + if (model != null) { + this.keyColumnNameValueList.addAll(model.keyColumnNameValueList); + } + } + + public String getColumnName(int col) { + return COLUMN_NAMES[col]; + } + + public int getColumnCount() { + return COLUMN_NAMES.length; + } + + public int getRowCount() { + return keyColumnNameValueList.size(); + } + + public void addKeyColumnNameValue(BatchSubmitPane.KeyColumnNameValue keyColumnNameValue) { + this.keyColumnNameValueList.add(keyColumnNameValue); + } + + public void removeKeyColumnNameValue(int index) { + this.keyColumnNameValueList.remove(index); + } + + public BatchSubmitPane.KeyColumnNameValue getKeyColumnNameValue(int index) { + return this.keyColumnNameValueList.get(index); + } + + public void removeAllKeyColumnNameValue() { + this.keyColumnNameValueList.clear(); + } + } + + protected static class KeyColumnValueTableModel extends BatchSubmitPane.KeyColumnTableModel { + + public KeyColumnValueTableModel(BatchSubmitPane.KeyColumnTableModel model) { + super(model); + } + + public Object getValueAt(int row, int col) { + BatchSubmitPane.KeyColumnNameValue knv = keyColumnNameValueList.get(row); + + switch (col) { + case 0: + return knv.isKey; + case 1: + return knv.cn; + case 2: + return knv.cv; + } + return null; + } + + public void setValueAt(Object value, int row, int col) { + BatchSubmitPane.KeyColumnNameValue knv = keyColumnNameValueList.get(row); + + if (col == 0 && value instanceof Boolean) { + knv.isKey = ((Boolean) value).booleanValue(); + } else if (col == 1 && value instanceof BatchSubmitPane.ColumnName) { + knv.cn = (BatchSubmitPane.ColumnName) value; + } else if (col == 2 && value instanceof BatchSubmitPane.ColumnValue) { + knv.cv = (BatchSubmitPane.ColumnValue) value; + } + } + + public Class getColumnClass(int c) { + switch (c) { + case 0: + return Boolean.class; + case 1: + return BatchSubmitPane.ColumnName.class; + case 2: + return BatchSubmitPane.ColumnValue.class; + } + return String.class; + } + + public boolean isCellEditable(int row, int col) { + return true; + } + } + + + public static class KeyColumnNameValue { + private boolean isKey = false; + private BatchSubmitPane.ColumnName cn; + public BatchSubmitPane.ColumnValue cv; + + public KeyColumnNameValue(boolean isKey, BatchSubmitPane.ColumnName cn, BatchSubmitPane.ColumnValue cv, boolean skip) { + this.isKey = isKey; + this.cn = cn; + this.cv = cv; + } + + /** + * 字符串 + * + * @return 字符串z + */ + public String toString() { + return (isKey ? "* " : "") + cn + ":" + cv; + } + } + + /* + * ColumnNameTableCellRenderer + */ + public class ColumnNameTableCellRenderer extends DefaultTableCellRenderer { + public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { + super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); + + if (value instanceof BatchSubmitPane.ColumnName) { + this.setText(((BatchSubmitPane.ColumnName) value).name); + } + + return this; + } + } + + /* + * ColumnValueTableCellRenderer + */ + private class ColumnValueTableCellRenderer extends DefaultTableCellRenderer { + public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { + super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); + + if (value instanceof BatchSubmitPane.ColumnValue) { + if (((BatchSubmitPane.ColumnValue) value).obj != null) { + if (((BatchSubmitPane.ColumnValue) value).obj instanceof Date) { + this.setText(DateUtils.DATEFORMAT2.format(((BatchSubmitPane.ColumnValue) value).obj)); + } else { + this.setText(((BatchSubmitPane.ColumnValue) value).obj.toString()); + } + } else { + this.setText(""); + } + } + + return this; + } + } +} diff --git a/designer/src/com/fr/design/write/submit/batch/BatchSubmitProvider.java b/designer/src/com/fr/design/write/submit/batch/BatchSubmitProvider.java new file mode 100644 index 0000000000..4044626645 --- /dev/null +++ b/designer/src/com/fr/design/write/submit/batch/BatchSubmitProvider.java @@ -0,0 +1,42 @@ +package com.fr.design.write.submit.batch; + +import com.fr.design.beans.BasicBeanPane; +import com.fr.design.fun.impl.AbstractSubmitProvider; +import com.fr.general.Inter; + +/** + * Created by loy on 16/8/13. + */ +public class BatchSubmitProvider extends AbstractSubmitProvider { + private volatile static BatchSubmitProvider instance; + + public static BatchSubmitProvider getInstance() { + if (instance == null) { + synchronized (BatchSubmitProvider.class) { + if (instance == null) { + instance = new BatchSubmitProvider(); + } + } + } + return instance; + } + @Override + public BasicBeanPane appearanceForSubmit() { + return new SmartInsertBatchSubmitPane(); + } + + @Override + public String dataForSubmit() { + return Inter.getLocText("Performance-plugin_submitbatch_name"); + } + + @Override + public String keyForSubmit() { + return "submitbatch"; + } + + @Override + public int currentAPILevel() { + return 1; + } +} diff --git a/designer/src/com/fr/design/write/submit/batch/SmartInsertBatchSubmitPane.java b/designer/src/com/fr/design/write/submit/batch/SmartInsertBatchSubmitPane.java new file mode 100644 index 0000000000..791f801ede --- /dev/null +++ b/designer/src/com/fr/design/write/submit/batch/SmartInsertBatchSubmitPane.java @@ -0,0 +1,591 @@ +package com.fr.design.write.submit.batch; + +import com.fr.cache.list.IntList; +import com.fr.data.ClassSubmitJob; +import com.fr.design.actions.UpdateAction; +import com.fr.design.cell.smartaction.AbstractSmartJTablePaneAction; +import com.fr.design.cell.smartaction.SmartJTablePane; +import com.fr.design.cell.smartaction.SmartJTablePaneAction; +import com.fr.design.dialog.BasicDialog; +import com.fr.design.dialog.BasicPane; +import com.fr.design.dialog.DialogActionAdapter; +import com.fr.design.editor.ValueEditorPaneFactory; +import com.fr.design.gui.controlpane.NameObjectCreator; +import com.fr.design.gui.controlpane.NameableCreator; +import com.fr.design.gui.ilable.UILabel; +import com.fr.design.gui.ispinner.UIBasicSpinner; +import com.fr.design.layout.FRGUIPaneFactory; +import com.fr.design.layout.TableLayout; +import com.fr.design.layout.TableLayoutHelper; +import com.fr.design.mainframe.DesignerContext; +import com.fr.design.mainframe.ElementCasePane; +import com.fr.design.mainframe.JTemplate; +import com.fr.design.mainframe.JWorkBook; +import com.fr.design.selection.SelectionEvent; +import com.fr.design.selection.SelectionListener; +import com.fr.design.write.submit.CustomSubmitJobPane; +import com.fr.design.write.submit.SmartInsertDMLJobPane; +import com.fr.design.write.submit.SubmitJobListPane; +import com.fr.general.Inter; +import com.fr.grid.selection.CellSelection; +import com.fr.grid.selection.FloatSelection; +import com.fr.grid.selection.Selection; +import com.fr.stable.ColumnRow; +import com.fr.stable.ColumnRowGroup; +import com.fr.write.DMLConfigJob; + +import javax.swing.*; +import javax.swing.table.DefaultTableCellRenderer; +import javax.swing.table.TableCellRenderer; +import javax.swing.table.TableColumn; +import java.awt.*; +import java.awt.event.ActionEvent; + +/** + * Created by loy on 16/8/16. + */ +public class SmartInsertBatchSubmitPane extends BatchSubmitPane { + private static final Selection NO_SELECTION = new CellSelection(-1, -1, -1, -1); + private ElementCasePane ePane; + private static int CELL_GROUP_LIMIT = 6; + + public SmartInsertBatchSubmitPane(ElementCasePane ePane) { + super(ValueEditorPaneFactory.extendedCellGroupEditors()); + this.ePane = ePane; + } + + public SmartInsertBatchSubmitPane() { + super(ValueEditorPaneFactory.extendedCellGroupEditors()); + JTemplate jTemplate = DesignerContext.getDesignerFrame().getSelectedJTemplate(); + this.ePane = ((JWorkBook) jTemplate).getEditingElementCasePane(); + } + + @Override + protected SubmitJobListPane createSubmitJobListPane() { + return new SmartInsertSubmitJobListPane(); + } + + class SmartInsertSubmitJobListPane extends SubmitJobListPane { + + public SmartInsertSubmitJobListPane() { + super(ePane); + } + + public void hideParentDialog() { + hideDialog4AddCellAction(); + } + + public void showParentDialog() { + showDialogAfterAddCellAction(); + } + + @Override + public NameableCreator[] createNameableCreators() { + return new NameableCreator[] { + new NameObjectCreator(Inter.getLocText(new String[]{"Submit", "Event"}), + "/com/fr/web/images/reportlet.png", + DMLConfigJob.class, + SmartInsertDMLJobPane.class), + new NameObjectCreator(Inter.getLocText(new String[]{"Custom", "Event"}), + "/com/fr/web/images/reportlet.png", + ClassSubmitJob.class, + CustomSubmitJobPane.class) }; + } + } + + @Override + protected UpdateAction[] getActions() { + return new UpdateAction[] { + new BatchSubmitPane.SmartAddFieldsAction(), + new BatchSubmitPane.AddFieldAction(), + new SmartAddCellAction(), + new SmartAddCellGroupAction(), + new BatchModCellAction(), + new BatchSubmitPane.RemoveFieldAction() + }; + } + + public class BatchModCellAction extends UpdateAction { + public BatchModCellAction() { + this.setName(Inter.getLocText("RWA-Batch_Modify_Cells")); + } + + /** + * 执行事件 + * @param evt 事件对象 + */ + public void actionPerformed(ActionEvent evt) { + BasicPane bPane = new BasicPane() { + @Override + protected String title4PopupWindow() { + return Inter.getLocText("RWA-Batch_Modify_Cells"); + } + }; + bPane.setLayout(FRGUIPaneFactory.createBorderLayout()); + bPane.setBorder(BorderFactory.createEmptyBorder(30, 0, 0, 0)); + final UIBasicSpinner columnSpinner = new UIBasicSpinner(); + final UIBasicSpinner rowSpinner = new UIBasicSpinner(); + Component[][] coms = new Component[][] { { new UILabel(Inter.getLocText("RWA-Row_Offset")), rowSpinner },{ new UILabel(Inter.getLocText("RWA-Column_Offset")), columnSpinner } }; + double p = TableLayout.PREFERRED; + double f = TableLayout.FILL; + bPane.add(TableLayoutHelper.createTableLayoutPane(coms, new double[]{p, p}, new double[]{p, f}), BorderLayout.NORTH); + BasicDialog dlg = bPane.showSmallWindow(SwingUtilities.getWindowAncestor(SmartInsertBatchSubmitPane.this), new DialogActionAdapter() { + public void doOk() { + int row_offset = ((Number)rowSpinner.getValue()).intValue(); + int column_offset = ((Number)columnSpinner.getValue()).intValue(); + BatchSubmitPane.KeyColumnTableModel model = (BatchSubmitPane.KeyColumnTableModel)keyColumnValuesTable.getModel(); + int[] selectedRows = keyColumnValuesTable.getSelectedRows(); + // 如果一行都没选中,取所有的行 + if (selectedRows.length == 0) { + selectedRows = IntList.range(model.getRowCount()); + } + for (int i = 0; i < selectedRows.length; i++) { + int row = selectedRows[i]; + BatchSubmitPane.KeyColumnNameValue kcnv = model.getKeyColumnNameValue(row); + if (kcnv.cv.obj instanceof ColumnRow) { + ColumnRow or = (ColumnRow)kcnv.cv.obj; + int n_column = or.getColumn() + column_offset; + if (n_column < 0) { + n_column = 0; + } + int n_row = or.getRow() + row_offset; + if (n_row < 0) { + n_row = 0; + } + kcnv.cv.obj = ColumnRow.valueOf(n_column, n_row); + } + } + model.fireTableDataChanged(); + keyColumnValuesTable.validate(); + for (int i = 0; i < selectedRows.length; i++) { + keyColumnValuesTable.addRowSelectionInterval(selectedRows[i], selectedRows[i]); + } + } + }); + dlg.setVisible(true); + } + } + + public class SmartAddCellAction extends UpdateAction { + public SmartAddCellAction() { + this.setName(Inter.getLocText("RWA-Smart_Add_Cells")); + } + + /** + * 执行事件 + * @param evt 事件s + */ + public void actionPerformed(ActionEvent evt) { + + // Grid.GridSelectionListener + if (ePane == null) { + return; + } + + /* + * 布局 + */ + BasicPane bPane = new SmartJTablePane4DB(keyColumnValuesTable.getTableModel4SmartAddCell(), ePane); + + // ReportWriteAttrDialog.this.setVisible(false); + hideDialog4AddCellAction(); + /* + * 当前的ReportPane不可编辑,不可切换Sheet,加GridSelectionChangeListener + */ + ePane.setSelection(NO_SELECTION); + ePane.setEditable(false); + ePane.getGrid().setNotShowingTableSelectPane(false); + + BasicDialog dlg = bPane.showWindow(SwingUtilities.getWindowAncestor(SmartInsertBatchSubmitPane.this)); + + dlg.setModal(false); + dlg.setVisible(true); + } + } + + public class SmartAddCellGroupAction extends UpdateAction { + public SmartAddCellGroupAction() { + this.setName(Inter.getLocText("RWA-Smart_Add_Cell_Group")); + } + + /** + * 智能添加单元格组 + * @param e 事件s + */ + public void actionPerformed(ActionEvent e) { + if (ePane == null) { + return; + } + + BasicPane bPane = new SmartJTablePane4DB(keyColumnValuesTable.getTableModel4SmartAddCell(), ePane, true); + + // ReportWriteAttrDialog.this.setVisible(false); + hideDialog4AddCellAction(); + /* + * 当前的ReportPane不可编辑,不可切换Sheet,加GridSelectionChangeListener + */ + ePane.setSelection(NO_SELECTION); + ePane.setEditable(false); + ePane.getGrid().setNotShowingTableSelectPane(false); + + BasicDialog dlg = bPane.showWindow(SwingUtilities.getWindowAncestor(SmartInsertBatchSubmitPane.this)); + + dlg.setModal(false); + dlg.setVisible(true); + } + } + + private void showDialogAfterAddCellAction() { + Container dialog = this; + if (parentPane != null && parentPane.getContentDBManiPane() instanceof SmartInsertBatchSubmitPane && parentPane.getContentDBManiPane() != this) { + ((SmartInsertBatchSubmitPane)parentPane.getContentDBManiPane()).showDialogAfterAddCellAction(); + } + while (dialog.getParent() != null) { + dialog = dialog.getParent(); + if (dialog instanceof SmartInsertSubmitJobListPane) { + ((SmartInsertSubmitJobListPane)dialog).showParentDialog(); + } else if (dialog instanceof Dialog) { + dialog.setVisible(true); + } + } + } + + private void hideDialog4AddCellAction() { + Container dialog = this; + if (parentPane != null && parentPane.getContentDBManiPane() instanceof SmartInsertBatchSubmitPane && parentPane.getContentDBManiPane() != this) { + ((SmartInsertBatchSubmitPane)parentPane.getContentDBManiPane()).hideDialog4AddCellAction(); + } + while (dialog.getParent() != null) { + dialog = dialog.getParent(); + if (dialog instanceof SmartInsertSubmitJobListPane) { + ((SmartInsertSubmitJobListPane)dialog).hideParentDialog(); + } else if (dialog instanceof Dialog) { + // 条件属性中添加的控件的话有两层dialog,需要都隐藏 + dialog.setVisible(false); + } + } + } + + /** + * 检测是否合法 + * @throws Exception + */ + public void checkValid() throws Exception { + BatchSubmitPane.KeyColumnTableModel model = (BatchSubmitPane.KeyColumnTableModel)keyColumnValuesTable.getModel(); + int cnt = model.getRowCount(); + int groupLength = -1; + for (int i=0; i= model.getRowCount()) { + return; + } + BatchSubmitPane.KeyColumnNameValue kcv = model.getKeyColumnNameValue(editingRowIndex); + ElementCasePane currentReportPane = (ElementCasePane)e.getSource(); + Selection selection = currentReportPane.getSelection(); + if (selection == NO_SELECTION || selection instanceof FloatSelection) { + return; + } + CellSelection cellselection = (CellSelection)selection; + kcv.cv.obj = ColumnRow.valueOf(cellselection.getColumn(), cellselection.getRow()); + + if (editingRowIndex >= model.getRowCount() - 1) { + setEditingRowIndex(0); + } else { + setEditingRowIndex(editingRowIndex + 1); + } + + model.fireTableDataChanged(); + + } + + }; + + /** + * 单元格组的点选格子事件 + */ + private SelectionListener groupListener = new SelectionListener() { + @Override + public void selectionChanged(SelectionEvent e) { + BatchSubmitPane.KeyColumnTableModel model = (BatchSubmitPane.KeyColumnTableModel)table.getModel(); + if (editingRowIndex < 0 || editingRowIndex >= model.getRowCount()) { + return; + } + BatchSubmitPane.KeyColumnNameValue kcv = model.getKeyColumnNameValue(editingRowIndex); + ElementCasePane currentReportPane = (ElementCasePane)e.getSource(); + Selection selection = currentReportPane.getSelection(); + if (selection == NO_SELECTION || selection instanceof FloatSelection) { + return; + } + CellSelection cellselection = (CellSelection)selection; + Object oriValue = kcv.cv.obj; + ColumnRowGroup newValue = getColumnRowGroupValue(oriValue); + + // 要考虑多选的情况 要结合之前的看看 可能是增加 也可能需要减少 + ColumnRowGroup add = new ColumnRowGroup(); + int removeCount = 0; + if (oriCellSelection != null && sameStartPoint(cellselection, oriCellSelection)) { + removeCount = dealDragSelection(add, cellselection); + } else if (cellselection.getSelectedType() == CellSelection.CHOOSE_ROW || cellselection.getSelectedType() == CellSelection.CHOOSE_COLUMN) { + dealSelectColRow(add, cellselection); + } else { + add.addColumnRow(ColumnRow.valueOf(cellselection.getColumn(), cellselection.getRow())); + } + + if (add.getSize() > 0) { + newValue.addAll(add); + } else if (removeCount > 0) { + newValue.splice(newValue.getSize()-removeCount, removeCount); + } + + kcv.cv.obj = newValue; + + model.fireTableDataChanged(); + + oriCellSelection = cellselection; + } + + private ColumnRowGroup getColumnRowGroupValue(Object oriValue) { + ColumnRowGroup newValue = new ColumnRowGroup(); + if (oriValue instanceof ColumnRowGroup) { + newValue.addAll((ColumnRowGroup)oriValue); + } else if (oriValue instanceof ColumnRow) { + newValue.addColumnRow((ColumnRow) oriValue); + } + return newValue; + } + + private boolean sameStartPoint(CellSelection cs1, CellSelection cs2) { + return cs1.getColumn() == cs2.getColumn() && cs1.getRow() == cs2.getRow(); + } + + private int dealDragSelection(ColumnRowGroup add, CellSelection cellselection) { + int removeCount = 0; + if (cellselection.getRowSpan() == oriCellSelection.getRowSpan() + 1) { + for (int i=0; i= CELL_GROUP_LIMIT) { + text.setText("[" + Inter.getLocText(new String[]{"Has_Selected", "Classifier-Ge", "Cell"}, + new String[]{((ColumnRowGroup)cv).getSize()+"", ""}) + "]"); + tip = cv.toString() + " " + tip; + } else if (cv != null) { + text.setText(cv.toString()); + } else { + text.setText(""); + } + } + + if (row == SmartJTablePane4DB.this.editingRowIndex) { + pane.setBackground(Color.cyan); + } else { + pane.setBackground(Color.white); + } + + pane.setToolTipText(tip); + pane.add(text); + + return pane; + } + } + + private class ColumnRowGroupCellRenderer2 extends DefaultTableCellRenderer { + @Override + public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { + super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); + + String tip = Inter.getLocText("FR-Designer_Double_Click_Edit_OR_Clear"); + + if (value instanceof BatchSubmitPane.ColumnValue) { + Object cv = ((BatchSubmitPane.ColumnValue) value).obj; + if (cv instanceof ColumnRowGroup && ((ColumnRowGroup)cv).getSize() >= CELL_GROUP_LIMIT) { + this.setText("[" + Inter.getLocText(new String[]{"Has_Selected", "Classifier-Ge", "Cell"}, + new String[]{((ColumnRowGroup)cv).getSize()+"", ""}) + "]"); + tip = cv.toString() + " " + tip; + } else if (cv != null) { + this.setText(cv.toString()); + } else { + this.setText(""); + } + } + + this.setToolTipText(tip); + + if (row == SmartJTablePane4DB.this.editingRowIndex) { + this.setBackground(Color.cyan); + } else { + this.setBackground(Color.white); + } + + return this; + } + } + } +} diff --git a/designer/src/com/fr/design/write/submit/batch/service/BatchDBCommitService.java b/designer/src/com/fr/design/write/submit/batch/service/BatchDBCommitService.java new file mode 100644 index 0000000000..01ac8a155b --- /dev/null +++ b/designer/src/com/fr/design/write/submit/batch/service/BatchDBCommitService.java @@ -0,0 +1,288 @@ +package com.fr.design.write.submit.batch.service; + +import com.fr.base.*; +import com.fr.data.NetworkHelper; +import com.fr.data.core.db.DBUtils; +import com.fr.data.impl.NameDatabaseConnection; +import com.fr.design.write.submit.batch.BatchCommit2DBJavaScript; +import com.fr.form.ui.WebContentUtils; +import com.fr.general.xml.GeneralXMLTools; +import com.fr.js.JavaScript; +import com.fr.json.JSONException; +import com.fr.json.JSONFunction; +import com.fr.json.JSONObject; +import com.fr.script.Calculator; +import com.fr.stable.ColumnRow; +import com.fr.stable.ParameterProvider; +import com.fr.stable.StringUtils; +import com.fr.stable.UtilEvalError; +import com.fr.stable.fun.impl.NoSessionIDOPService; +import com.fr.stable.script.NameSpace; +import com.fr.web.RepositoryDeal; +import com.fr.web.core.ReportSessionIDInfor; +import com.fr.web.core.SessionDealWith; +import com.fr.web.core.SessionIDInfor; +import com.fr.web.utils.WebUtils; +import com.fr.write.batch.SubmitMain; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.sql.Connection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +/** + * Created by loy on 16/8/23. + */ +public class BatchDBCommitService extends NoSessionIDOPService { + private static BatchDBCommitService service = null; + + public static final String ARG_XMLCONF = "xmlconf"; + public static final String ARG_SESSIONID = "sessionID"; + + /** + * 构造 + * + * @return + */ + public static BatchDBCommitService getInstance() { + if (service == null) { + service = new BatchDBCommitService(); + } + + return service; + } + + /** + * OP值 + * + * @return 返回OP值 + */ + public String actionOP() { + return "batchcommit"; + } + + private Map sessionMap = new HashMap(); + + /** + * 提交入库请求处理 + * + * @param req 请求 + * @param res 响应 + * @throws Exception 抛出异常 + */ + public void process(HttpServletRequest req, HttpServletResponse res) throws Exception { + String sessionID = WebUtils.getHTTPRequestParameter(req, ARG_SESSIONID); + CountLock lock = getCountLock(sessionID); + try { + synchronized (lock) { + process0(req, res); + } + } finally { + releaseLock(sessionID, lock); + } + + } + + private void releaseLock(String sessionID, CountLock lock) { + synchronized (sessionMap) { + lock.reduce(); + if (lock.getCount() == 0) { + sessionMap.remove(sessionID); + } + } + } + + private CountLock getCountLock(String sessionID) { + CountLock lock; + synchronized (sessionMap) { + lock = (CountLock) sessionMap.get(sessionID); + if (lock == null) { + lock = new CountLock(); + sessionMap.put(sessionID, lock); + } + lock.increase(); + } + return lock; + } + + private void process0(HttpServletRequest req, HttpServletResponse res) throws Exception { + String dbManiXML = NetworkHelper.getHTTPRequestEncodeParameter(req, ARG_XMLCONF, false); + if (dbManiXML != null) { + BatchCommit2DBJavaScript commit2DBJS = (BatchCommit2DBJavaScript) GeneralXMLTools.readStringAsXMLable(dbManiXML); + Calculator ca = Calculator.createCalculator(); + RepositoryDeal repo = prepareRepository(req, ca); + NameSpace ns = ParameterMapNameSpace.create(WebUtils.parameters4SessionIDInfor(req)); + ca.pushNameSpace(ns); + Map feedbackMap = prepareFeedBackMap(req, ca); + boolean success = true; + JSONObject fr_submitinfo = new JSONObject(); + Map connectionMap = new HashMap(); + try { + String location = WebUtils.getHTTPRequestParameter(req, "location"); + ColumnRow cr = ColumnRow.valueOf(location); + for (int i = 0; i < commit2DBJS.getDBManipulation().size(); i++) { + if (cr != ColumnRow.ERROR) { + ca.setCurrentFromColumnRow(cr);// commit的时候, 也会去set关联格子的ColumnRow, 为防止上一个提交对下一个造成影响, 这边每次重置 + } + SubmitMain dbManipulation = (SubmitMain) commit2DBJS.getDBManipulation().get(i); + if (dbManipulation != null && dbManipulation.getDmlConfig() != null) { + String dbName = dbManipulation.getDBName(ca); + Connection conn = createConnection(dbName, connectionMap); + ca.putConnection(dbName, conn); + dbManipulation.doJob(ca); + connectionMap.put(dbName, conn); + } + } + } catch (Exception e) { + FRContext.getLogger().error(e.getMessage(), e); + Iterator iter = connectionMap.entrySet().iterator(); + while (iter.hasNext()) { + DBUtils.rollback((Connection) ((Map.Entry) iter.next()).getValue()); + } + success = false; + fr_submitinfo.put("failinfo", e.getMessage()); + } finally { + DBUtils.commitConnections(connectionMap); + } + ca.removeNameSpace(ns); + JSONObject jo = new JSONObject(); + createJo(req, jo, feedbackMap, success, fr_submitinfo, repo); + java.io.PrintWriter writer = WebUtils.createPrintWriter(res); + writer.write(jo.toString()); + writer.flush(); + writer.close(); + } + } + + private Connection createConnection(String dbName, Map connectionMap) throws Exception { + Connection conn = null; + if (connectionMap.containsKey(dbName)) { + return connectionMap.get(dbName); + } else { + NameDatabaseConnection db = new NameDatabaseConnection(dbName); + return db.createConnection(); + } + } + + private RepositoryDeal prepareRepository(HttpServletRequest req, Calculator ca) { + String sessionID = WebUtils.getHTTPRequestParameter(req, ARG_SESSIONID); + SessionIDInfor sessionIDInfor = SessionDealWith.getSessionIDInfor(sessionID); + + RepositoryDeal repo = null; + if (sessionIDInfor != null) { + repo = new RepositoryDeal(req, sessionIDInfor); + if (sessionIDInfor instanceof ReportSessionIDInfor) { + dealWithParaForCa(req, ca, (ReportSessionIDInfor) sessionIDInfor); + } + + NameSpace sessionNamespace = sessionIDInfor.asNameSpace(sessionID); + ca.pushNameSpace(sessionNamespace); + } + + return repo; + } + + private Map prepareFeedBackMap(HttpServletRequest req, Calculator ca) { + String feedback = WebUtils.getHTTPRequestParameter(req, "feedbackMap"); + Map feedbackMap = null; + if (StringUtils.isNotEmpty(feedback)) { + try { + feedbackMap = new JSONObject(feedback).toMap(); + } catch (JSONException e) { + FRContext.getLogger().error(e.getMessage()); + } + NameSpace feedbackNS = ParameterMapNameSpace.create(feedbackMap); + ca.pushNameSpace(feedbackNS); + } + + return feedbackMap; + } + + private void dealWithParaForCa(HttpServletRequest req, Calculator ca, ReportSessionIDInfor sessionIDInfor) { + ca.setAttribute(Calculator.SHEET_NUMBER_KEY, WebUtils.getHTTPRequestParameter(req, "sheetNum")); + sessionIDInfor.setUpAttribute4dbCommit(ca); + + // 提交入库可能用当前模板的参数 + NameSpace paras = ParameterMapNameSpace.create(sessionIDInfor.getParameterMap4Execute()); + ca.pushNameSpace(paras); + } + + private void createJo(HttpServletRequest req, JSONObject jo, Map feedbackMap, + boolean success, JSONObject fr_submitinfo, RepositoryDeal repo) throws Exception { + // 生成的时候没有encode的,用没有进行urldecode的方法获取callback语句,防止+和%出错 + String callBackXML = NetworkHelper.getHTTPRequestEncodeParameter(req, "callback", false); + JavaScript callBack = (JavaScript) GeneralXMLTools.readStringAsXMLable(callBackXML); + fr_submitinfo.put("success", success); + + if (callBack != null) { + dealWithSuccessPara(callBack, fr_submitinfo, repo); + if (feedbackMap != null) { + callBack.addParameterMap(feedbackMap); + } + jo.put("callback", new JSONFunction(new String[]{"res"}, callBack.createJS(repo), repo.getDevice())); + } + } + + private void dealWithSuccessPara(JavaScript callBack, JSONObject fr_submitinfo, RepositoryDeal repo) { + Map submitInfo = new HashMap(); + submitInfo.put(WebContentUtils.FR_SUBMITINFO, fr_submitinfo); + if (callBack != null) { + callBack.addParameterMap(submitInfo); + ParameterProvider[] ps = new ParameterProvider[callBack.getParameters().length + 1]; + int len = callBack.getParameters().length; + for (int i = 0; i < len; i++) { + ps[i] = callBack.getParameters()[i]; + } + ps[len] = new Parameter(WebContentUtils.FR_SUBMITINFO, fr_submitinfo); + callBack.setParameters(ps); + } + + // if the parameter contains "fr_submitinfo" then recalculate it + ParameterProvider[] paras; + if (callBack != null) { + paras = callBack.getParameters(); + } else { + paras = new Parameter[0]; + } + Calculator ca = Calculator.createCalculator(); + if (repo != null) { + NameSpace ns = ParameterMapNameSpace.create(repo.getReportParameterMap()); + ca.pushNameSpace(ns); + } + ca.pushNameSpace(ParameterMapNameSpace.create(submitInfo)); + + for (int i = 0; i < paras.length; i++) { + Object obj = paras[i].getValue(); + if (obj instanceof Formula && needToRecalculate(paras[i])) { + try { + ((Formula) obj).setResult(ca.eval((Formula) obj)); + } catch (UtilEvalError utilEvalError) { + + } + } + } + } + + private boolean needToRecalculate(ParameterProvider p) { + return Utils.objectToString(p.getValue()).toLowerCase().indexOf(WebContentUtils.FR_SUBMITINFO) != -1; + } +} + + +class CountLock { + private int count; + + public void increase() { + count++; + } + + public void reduce() { + count--; + } + + public int getCount() { + return count; + } +}