T
turumbay
вроде работающий агент:
<div class="sp-wrap"><div class="sp-head-wrap"><div class="sp-head folded clickable">"Код"</div></div><div class="sp-body"><div class="sp-content">
в яве не силен - за качество кода сильно не пинайте...
<div class="sp-wrap"><div class="sp-head-wrap"><div class="sp-head folded clickable">"Код"</div></div><div class="sp-body"><div class="sp-content">
Код:
import lotus.domino.*;
import java.util.Vector;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
// (Your code goes here)
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
System.out.println("SQLException: " + ex.getMessage());
}
try {
conn = DriverManager.getConnection("jdbc:mysql://server/dbname?user=USER&password=PASSWORD");
} catch (SQLException ex) {
System.out.println("On connect SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
System.out.println("Connection Created");
try {
stmt = conn.createStatement();
// Создаем временные таблицы
// 1. Наличие товара на складах по датам
String cutOffDate = "2006-10-16"; // дата отсечения
String tmpTableQuery = "CREATE TEMPORARY TABLE T1(guid integer, storeuid integer, days integer) SELECT guid, storeuid, count(distinct(storedate)) AS days FROM stores WHERE storedate>'"+cutOffDate+"' GROUP BY guid, storeuid ";
try {
stmt.executeUpdate(tmpTableQuery);
} catch(SQLException ex) {
System.out.println("Creating t1... SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
// 2. Текущее состояние складов
tmpTableQuery = "CREATE TEMPORARY TABLE T2(guid integer, storeuid integer, amount integer) SELECT guid,storeuid, sum(amount) AS amount FROM stores WHERE storedate = (SELECT MAX(storedate) FROM stores) GROUP BY guid, storeuid";
try {
stmt.executeUpdate(tmpTableQuery);
} catch(SQLException ex) {
System.out.println("Creating t2... SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
// 3. Продажи по датам
tmpTableQuery = "CREATE TEMPORARY TABLE T3(guid integer, storeuid integer, amount integer) SELECT guid, storeuid, sum(amount) AS amount FROM buystore WHERE indoccreatedate>'"+cutOffDate+"' GROUP BY guid, storeuid";
try {
stmt.executeUpdate(tmpTableQuery);
} catch(SQLException ex) {
System.out.println("Creating t3... SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
// ацкий JOIN
String joinQuery = "SELECT t1.*,goodslist.goodname, IFNULL(t2.amount,0) AS onstore, IFNULL(t3.amount,0) AS sold, ";
joinQuery = joinQuery + "IFNULL(t3.amount,0)/t1.days AS avrg, IFNULL(t3.amount,0)/t1.days-IFNULL(t2.amount,0) AS ness FROM t1 ";
joinQuery = joinQuery + "LEFT JOIN t2 ON t1.guid=t2.guid AND t1.storeuid=t2.storeuid ";
joinQuery = joinQuery + "LEFT JOIN t3 ON t1.guid=t3.guid AND t1.storeuid=t3.storeuid ";
joinQuery = joinQuery + "LEFT JOIN goodslist ON t1.guid=goodslist.guid ";
joinQuery = joinQuery + "WHERE IFNULL(t3.amount,0)/t1.days-IFNULL(t2.amount,0) >0 ";
joinQuery = joinQuery + "ORDER BY IFNULL(t3.amount,0)/t1.days-IFNULL(t2.amount,0)";
try {
rs = stmt.executeQuery(joinQuery);
} catch(SQLException ex) {
System.out.println("on JOIN SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
Database db = agentContext.getCurrentDatabase();
Document doc = db.createDocument();
doc.appendItemValue("Form", "ProductReport");
Vector vguid = new Vector();
Vector vgoodname = new Vector();
Vector vstoreuid = new Vector();
Vector vonstore = new Vector();
Vector vtobuy = new Vector();
while (rs.next()) {
// retrieve and print the values for the current row
Integer guid = new Integer(rs.getInt("guid"));
Integer storeuid = new Integer(rs.getInt("storeuid"));
Integer onstore = new Integer(rs.getInt("onstore"));
Float tobuy = new Float(rs.getFloat("ness"));
vguid.addElement(guid);
vgoodname.addElement(rs.getString("goodname"));
vstoreuid.addElement(storeuid);
vonstore.addElement(onstore);
vtobuy.addElement(tobuy);
}
doc.appendItemValue("guid",vguid);
doc.appendItemValue("goodname",vgoodname);
doc.appendItemValue("storeuid", vstoreuid);
doc.appendItemValue("onstore", vonstore);
doc.appendItemValue("tobuy", vtobuy);
doc.save();
} finally {
if (rs != null) {
try {
rs.close();
} catch(SQLException ex) {}
rs = null;
} //if
if (stmt != null) {
try {
stmt.close();
} catch(SQLException ex) {}
stmt = null;
} //if
} //finally
} catch(Exception e) {
e.printStackTrace();
}
}
}