fd1341bd1195faae2545cb1ff19dffea689e608c.svn-base 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. package com.persistence.service.logic;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.io.Reader;
  8. import java.io.StringReader;
  9. import java.io.StringWriter;
  10. import java.io.Writer;
  11. import java.sql.Blob;
  12. import java.sql.Clob;
  13. import java.sql.Connection;
  14. import java.sql.PreparedStatement;
  15. import java.sql.ResultSet;
  16. import java.sql.SQLException;
  17. import java.sql.Statement;
  18. import java.sql.Timestamp;
  19. import java.util.ArrayList;
  20. import java.util.Calendar;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import org.apache.commons.logging.Log;
  24. import org.apache.commons.logging.LogFactory;
  25. import com.persistence.DbConnection;
  26. import com.persistence.service.ResultSet.TypeUtil;
  27. import com.persistence.service.assitant.DataObject;
  28. import com.persistence.service.assitant.Field;
  29. import com.persistence.service.exception.PersistenceException;
  30. import com.persistence.service.exception.TypeUnsupportedException;
  31. import com.sysmodel.datamodel.xmlmodel.able.MdpDataSource;
  32. /**
  33. * sysmodel com.sysmodel.persistence create time 2006-5-20
  34. *
  35. * @author nbs
  36. */
  37. public class DaMengLogicHelper extends LogicHelper{
  38. private Log log = LogFactory.getLog(DaMengLogicHelper.class);
  39. public Connection getConnection(MdpDataSource datasource) {
  40. Connection connection = super.getConnection(datasource);
  41. if (connection == null) {
  42. try {
  43. DbConnection dbConnection = new DbConnection();
  44. connection = dbConnection.getConnection(datasource);
  45. } catch (ClassNotFoundException e) {
  46. log.error(e);
  47. } catch (SQLException e) {
  48. log.error(e);
  49. }
  50. }
  51. return connection;
  52. }
  53. public String getDataBaseTime(Connection con) throws PersistenceException {
  54. String time = "";
  55. String sql = "select sysdate from dual";
  56. try {
  57. PreparedStatement ps = con.prepareStatement(sql);
  58. ResultSet rs = ps.executeQuery();
  59. if (rs.next()) {
  60. Timestamp timesamp = rs.getTimestamp(1);
  61. if (timesamp != null) {
  62. Calendar calendar = Calendar.getInstance();
  63. calendar.setTimeInMillis(timesamp.getTime());
  64. java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(
  65. "yyyy-MM-dd HH:mm:ss");
  66. time = df.format(calendar.getTime());
  67. }
  68. }
  69. rs.close();
  70. ps.close();
  71. } catch (Exception e) {
  72. throw new PersistenceException(e.getMessage());
  73. }
  74. return time;
  75. }
  76. public String insertChildData(DataObject object, Connection con, String parentid)
  77. throws PersistenceException {
  78. try {
  79. // 判断逻辑主键
  80. if (isExist(con, object.getTablename(), getLogicPrimaryKeys(object.getAttributes()))) {
  81. throw new PersistenceException("logic primary key repeat");
  82. }
  83. // 存储非lob数据
  84. List<Field> reLobFields = new ArrayList<Field>();
  85. List<Field> reOtherFields = new ArrayList<Field>();
  86. String sql = "insert into " + object.getTablename()
  87. + generalChildInsert(object.getAttributes(), reLobFields, reOtherFields);
  88. log.info(sql);
  89. PreparedStatement ps = con.prepareStatement(sql);
  90. Iterator<Field> it = reOtherFields.iterator();
  91. ps.setString(1, object.getObjectID());
  92. ps.setString(2, parentid);
  93. int i = 3;
  94. while (it.hasNext()) {
  95. Field field = it.next();
  96. TypeUtil.getDataType(field.getDataType()).set(ps, field.getFieldvalue(), i);
  97. i++;
  98. }
  99. int count = ps.executeUpdate();
  100. ps.close();
  101. if (count <= 0)
  102. throw new PersistenceException("PreparedStatement execute failture");
  103. // 存储lob数据
  104. if (reLobFields.size() > 0) {
  105. insertLobField(object.getTablename(), object.getObjectID(), reLobFields, con);
  106. }
  107. } catch (TypeUnsupportedException e) {
  108. throw new PersistenceException(e.getMessage());
  109. } catch (SQLException e) {
  110. throw new PersistenceException(e.getMessage());
  111. }
  112. return object.getObjectID();
  113. }
  114. public String insertData(DataObject object, Connection con) throws PersistenceException {
  115. try {
  116. // 判断逻辑主键
  117. if (isExist(con, object.getTablename(), getLogicPrimaryKeys(object.getAttributes()))) {
  118. throw new PersistenceException("logic primary key repeat");
  119. }
  120. // 存储非lob数据
  121. List<Field> reLobFields = new ArrayList<Field>();
  122. List<Field> reOtherFields = new ArrayList<Field>();
  123. String sql = "insert into " + object.getTablename()
  124. + generalInsert(object.getAttributes(), reLobFields, reOtherFields);
  125. PreparedStatement ps = con.prepareStatement(sql);
  126. Iterator<Field> it = reOtherFields.iterator();
  127. ps.setString(1, object.getObjectID());
  128. int i = 2;
  129. while (it.hasNext()) {
  130. Field field = it.next();
  131. System.out.println("fieldname="+field.getFieldname() +" field.getFieldvalue()="+field.getFieldvalue() + " i="+i);
  132. TypeUtil.getDataType(field.getDataType()).set(ps, field.getFieldvalue(), i);
  133. i++;
  134. }
  135. int count = ps.executeUpdate();
  136. ps.close();
  137. if (count <= 0)
  138. throw new PersistenceException("PreparedStatement execute failture");
  139. // 存储lob数据
  140. if (reLobFields.size() > 0) {
  141. insertLobField(object.getTablename(), object.getObjectID(), reLobFields, con);
  142. }
  143. // 存储子表
  144. if (object.getChildobject().size() != 0) {
  145. List<DataObject> child = object.getChildobject();
  146. for (int j = 0; j < child.size(); j++) {
  147. DataObject tempChild = child.get(j);
  148. insertChildData(tempChild, con, object.getObjectID());
  149. }
  150. }
  151. } catch (TypeUnsupportedException e) {
  152. throw new PersistenceException(e.getMessage());
  153. } catch (SQLException e) {
  154. throw new PersistenceException(e.getMessage());
  155. }
  156. return object.getObjectID();
  157. }
  158. public ArrayList<DataObject> searchAllData(DataObject object, Connection con,
  159. List<Field> condition) throws PersistenceException {
  160. ArrayList<DataObject> al = new ArrayList<DataObject>();
  161. StringBuffer sb = new StringBuffer();
  162. String sql = "select " + generalListAndOrderBy(object.getAttributes(), sb)
  163. + ",fd_objectid from " + object.getTablename() + generalWhere(condition, sb);
  164. String order = sb.toString();
  165. if (!order.equals(""))
  166. sql = sql + " order by " + sb.deleteCharAt(sb.length() - 1).toString();
  167. try {
  168. PreparedStatement ps = con.prepareStatement(sql);
  169. for (int i = 0; i < condition.size(); i++) {
  170. Field field = condition.get(i);
  171. TypeUtil.getDataType(field.getDataType()).set(ps, field.getFieldvalue(), i + 1);
  172. }
  173. ResultSet rs = ps.executeQuery();
  174. List<Field> alField = object.getAttributes();
  175. while (rs.next()) {
  176. DataObject temobject = new DataObject();
  177. temobject.setClassid(object.getClassid());
  178. temobject.setTablename(object.getTablename());
  179. temobject.setObjectID(rs.getString("fd_objectid"));
  180. for (int i = 0; i < alField.size(); i++) {
  181. Field field = alField.get(i);
  182. Field tempField = new Field(field.getFieldname(), null);
  183. tempField.setDataType(field.getDataType());
  184. if (field.getDataType().equals(TypeUtil.BLOB)) {
  185. if (rs.getBlob(field.getFieldname()) != null) {
  186. tempField
  187. .setFieldvalue(this.readBolb(rs.getBlob(field.getFieldname())));
  188. }
  189. } else if (field.getDataType().equals(TypeUtil.CLOB)) {
  190. if (rs.getClob(field.getFieldname()) != null) {
  191. tempField.setFieldvalue(readColb(rs.getClob(field.getFieldname())));
  192. }
  193. } else {
  194. tempField.setFieldvalue(TypeUtil.getDataType(field.getDataType()).get(rs,
  195. field.getFieldname()));
  196. }
  197. temobject.addAttribute(tempField);
  198. }
  199. al.add(temobject);
  200. }
  201. } catch (SQLException e) {
  202. throw new PersistenceException(e.getMessage());
  203. } catch (TypeUnsupportedException e) {
  204. throw new PersistenceException(e.getMessage());
  205. }
  206. return al;
  207. }
  208. @Override
  209. public ArrayList<DataObject> searchAllData(DataObject object, Connection con,
  210. String SQLcondition) throws PersistenceException {
  211. ArrayList<DataObject> al = new ArrayList<DataObject>();
  212. StringBuffer sb = new StringBuffer();
  213. String sql = "select " + generalListAndOrderBy(object.getAttributes(), sb)
  214. + ",fd_objectid from " + object.getTablename();
  215. if (SQLcondition.length() > 0)
  216. sql += " " + SQLcondition;
  217. log.info(sql);
  218. try {
  219. PreparedStatement ps = con.prepareStatement(sql);
  220. ResultSet rs = ps.executeQuery();
  221. List<Field> alField = object.getAttributes();
  222. while (rs.next()) {
  223. DataObject temobject = new DataObject();
  224. temobject.setClassid(object.getClassid());
  225. temobject.setTablename(object.getTablename());
  226. temobject.setObjectID(rs.getString("fd_objectid"));
  227. for (int i = 0; i < alField.size(); i++) {
  228. Field field = alField.get(i);
  229. Field tempField = new Field(field.getFieldname(), null);
  230. tempField.setDataType(field.getDataType());
  231. if (field.getDataType().equals(TypeUtil.BLOB)) {
  232. tempField.setFieldvalue(this.readBolb(rs.getBlob(field.getFieldname())));
  233. } else if (field.getDataType().equals(TypeUtil.CLOB)) {
  234. tempField.setFieldvalue(readColb(rs.getClob(field.getFieldname())));
  235. } else {
  236. tempField.setFieldvalue(TypeUtil.getDataType(field.getDataType()).get(rs,
  237. field.getFieldname()));
  238. }
  239. temobject.addAttribute(tempField);
  240. }
  241. al.add(temobject);
  242. }
  243. } catch (SQLException e) {
  244. e.printStackTrace();
  245. // throw new PersistenceException(e.getMessage());
  246. } catch (TypeUnsupportedException e) {
  247. throw new PersistenceException(e.getMessage());
  248. }
  249. return al;
  250. }
  251. public ArrayList<DataObject> searchChildData(DataObject object, Connection con, String parentid)
  252. throws PersistenceException {
  253. ArrayList<DataObject> al = new ArrayList<DataObject>();
  254. StringBuffer sb = new StringBuffer();
  255. String sql = "select " + generalListAndOrderBy(object.getAttributes(), sb)
  256. + ",fd_objectid from " + object.getTablename() + " where fd_parentid='" + parentid
  257. + "'";
  258. String order = sb.toString();
  259. if (!order.equals(""))
  260. sql = sql + " order by " + sb.deleteCharAt(sb.length() - 1).toString();
  261. try {
  262. PreparedStatement ps = con.prepareStatement(sql);
  263. ResultSet rs = ps.executeQuery();
  264. List<Field> alField = object.getAttributes();
  265. while (rs.next()) {
  266. DataObject temobject = new DataObject();
  267. temobject.setClassid(object.getClassid());
  268. temobject.setTablename(object.getTablename());
  269. temobject.setObjectID(rs.getString("fd_objectid"));
  270. for (int i = 0; i < alField.size(); i++) {
  271. Field field = alField.get(i);
  272. Field tempField = new Field(field.getFieldname(), null);
  273. tempField.setDataType(field.getDataType());
  274. if (field.getDataType().equals(TypeUtil.BLOB)) {
  275. tempField.setFieldvalue(this.readBolb(rs.getBlob(field.getFieldname())));
  276. } else if (field.getDataType().equals(TypeUtil.CLOB)) {
  277. tempField.setFieldvalue(readColb(rs.getClob(field.getFieldname())));
  278. } else {
  279. tempField.setFieldvalue(TypeUtil.getDataType(field.getDataType()).get(rs,
  280. field.getFieldname()));
  281. }
  282. temobject.addAttribute(tempField);
  283. }
  284. al.add(temobject);
  285. }
  286. } catch (SQLException e) {
  287. throw new PersistenceException(e.getMessage());
  288. } catch (TypeUnsupportedException e) {
  289. throw new PersistenceException(e.getMessage());
  290. }
  291. return al;
  292. }
  293. public DataObject searchData(DataObject object, Connection con) throws PersistenceException {
  294. String sql = "select " + generalList(object.getAttributes()) + " from "
  295. + object.getTablename() + " where " + object.getObjectIDName() + " =?";
  296. log.info(sql);
  297. try {
  298. PreparedStatement ps = con.prepareStatement(sql);
  299. ps.setString(1,object.getObjectID());
  300. ResultSet rs = ps.executeQuery();
  301. if (rs.next()) {
  302. List<Field> alField = object.getAttributes();
  303. for (int i = 0; i < alField.size(); i++) {
  304. Field field = alField.get(i);
  305. if (field.getDataType().equals(TypeUtil.BLOB)) {
  306. field.setFieldvalue(this.readBolb(rs.getBlob(field.getFieldname())));
  307. } else if (field.getDataType().equals(TypeUtil.CLOB)) {
  308. field.setFieldvalue(readColb(rs.getClob(field.getFieldname())));
  309. } else {
  310. field.setFieldvalue(TypeUtil.getDataType(field.getDataType()).get(rs,
  311. field.getFieldname()));
  312. }
  313. }
  314. }
  315. rs.close();
  316. ps.close();
  317. } catch (SQLException e) {
  318. throw new PersistenceException(e.getMessage());
  319. } catch (TypeUnsupportedException e) {
  320. throw new PersistenceException(e.getMessage());
  321. }
  322. return object;
  323. }
  324. public boolean updateData(DataObject object, Connection con) throws PersistenceException {
  325. if (!updateOneData(object, con))
  326. return false;
  327. // 王志军-2010-10-18 修改,更新子表问题
  328. // 更新子表
  329. // if(object.getChildobject().size()!=0){
  330. // List<DataObject> child = object.getChildobject();
  331. // for (int j = 0; j < child.size(); j++) {
  332. // DataObject tempChild = child.get(j);
  333. // log.info(tempChild.getObjectID()) ;
  334. // if(!updateOneData(tempChild,con)) return false;
  335. // }
  336. // }
  337. return true;
  338. }
  339. public boolean updateOneData(DataObject object, Connection con) throws PersistenceException {
  340. int count = -1;
  341. try {
  342. // 判断逻辑主键
  343. if (isExist(con, object.getTablename(), getLogicPrimaryKeys(object.getAttributes()),
  344. object.getObjectID())) {
  345. throw new PersistenceException("logic primary key repeat");
  346. }
  347. // 存储非lob数据
  348. List<Field> reLobFields = new ArrayList<Field>();
  349. List<Field> reOtherFields = new ArrayList<Field>();
  350. String sql = "update " + object.getTablename()
  351. + generalUpdate(object.getAttributes(), reLobFields, reOtherFields)
  352. + " where fd_objectid='" + object.getObjectID() + "'";
  353. PreparedStatement ps = con.prepareStatement(sql);
  354. Iterator<Field> it = reOtherFields.iterator();
  355. int i = 1;
  356. while (it.hasNext()) {
  357. Field field = it.next();
  358. System.out.println("field.getDataType()="+field.getDataType()+" field.getFieldvalue()="+field.getFieldvalue()+" i="+i);
  359. TypeUtil.getDataType(field.getDataType()).set(ps, field.getFieldvalue(), i);
  360. i++;
  361. }
  362. count = ps.executeUpdate();
  363. ps.close();
  364. if (count == 1) {
  365. // 存储lob数据
  366. if (reLobFields.size() > 0) {
  367. updateLobField(object.getTablename(), object.getObjectID(), reLobFields, con);
  368. }
  369. }
  370. } catch (TypeUnsupportedException e) {
  371. throw new PersistenceException(e.getMessage());
  372. } catch (SQLException e) {
  373. throw new PersistenceException(e.getMessage());
  374. }
  375. return count <= 0 ? false : true;
  376. }
  377. public void readBlobToOutputStream(DataObject object, Connection con, OutputStream os)
  378. throws PersistenceException {
  379. String sql = "select " + generalList(object.getAttributes()) + " from "
  380. + object.getTablename() + " where fd_objectid='" + object.getObjectID() + "'";
  381. try {
  382. PreparedStatement ps = con.prepareStatement(sql);
  383. ResultSet rs = ps.executeQuery();
  384. if (rs.next()) {
  385. readBlob(rs.getBlob(1), os);
  386. }
  387. rs.close();
  388. ps.close();
  389. } catch (SQLException e) {
  390. throw new PersistenceException(e.getMessage());
  391. }
  392. }
  393. public void readClobToWriter(DataObject object, Connection con, Writer writer)
  394. throws PersistenceException {
  395. String sql = "select " + generalList(object.getAttributes()) + " from "
  396. + object.getTablename() + " where fd_objectid='" + object.getObjectID() + "'";
  397. try {
  398. PreparedStatement ps = con.prepareStatement(sql);
  399. ResultSet rs = ps.executeQuery();
  400. if (rs.next()) {
  401. readClob(rs.getClob(1), writer);
  402. }
  403. rs.close();
  404. ps.close();
  405. } catch (SQLException e) {
  406. throw new PersistenceException(e.getMessage());
  407. }
  408. }
  409. public void storeBlob(DataObject object, Connection con, InputStream is)
  410. throws PersistenceException {
  411. String clearLob = "update " + object.getTablename()
  412. + generalClearList(object.getAttributes()) + " where fd_objectid ="
  413. + object.getObjectID();
  414. String sql = "select " + generalList(object.getAttributes()) + " from "
  415. + object.getTablename() + " where fd_objectid =" + object.getObjectID()
  416. + " for update";
  417. try {
  418. Statement stmt = con.createStatement();
  419. if (stmt.executeUpdate(clearLob) != 1)
  420. throw new SQLException("clear Lob failture");
  421. ResultSet lobDetails = stmt.executeQuery(sql);
  422. if (lobDetails.next()) {
  423. writeBlob(lobDetails.getBlob(1), is);
  424. }
  425. lobDetails.close();
  426. stmt.close();
  427. } catch (SQLException e) {
  428. log.error("updateLobField error ", e);
  429. throw new PersistenceException(e.getMessage());
  430. }
  431. }
  432. public void storeClob(DataObject object, Connection con, Reader reader)
  433. throws PersistenceException {
  434. String clearLob = "update " + object.getTablename()
  435. + generalClearList(object.getAttributes()) + " where fd_objectid ="
  436. + object.getObjectID();
  437. String sql = "select " + generalList(object.getAttributes()) + " from "
  438. + object.getTablename() + " where fd_objectid =" + object.getObjectID()
  439. + " for update";
  440. try {
  441. Statement stmt = con.createStatement();
  442. if (stmt.executeUpdate(clearLob) != 1)
  443. throw new SQLException("clear Lob failture");
  444. ResultSet lobDetails = stmt.executeQuery(sql);
  445. if (lobDetails.next()) {
  446. writeClob(lobDetails.getClob(1), reader);
  447. }
  448. lobDetails.close();
  449. stmt.close();
  450. } catch (SQLException e) {
  451. log.error("updateLobField error ", e);
  452. throw new PersistenceException(e.getMessage());
  453. }
  454. }
  455. private String generalInsert(List<Field> allFields, List<Field> reLobFields,
  456. List<Field> reOtherFields) {
  457. StringBuffer sblist = new StringBuffer(" (fd_objectid");
  458. StringBuffer sbvalue = new StringBuffer("values(?");
  459. Iterator<Field> it = allFields.iterator();
  460. while (it.hasNext()) {
  461. Field field = it.next();
  462. if (field.getDataType().equals(TypeUtil.BLOB)) {
  463. sbvalue.append(",EMPTY_BLOB()");
  464. reLobFields.add(field);
  465. } else if (field.getDataType().equals(TypeUtil.CLOB)) {
  466. sbvalue.append(",EMPTY_CLOB()");
  467. reLobFields.add(field);
  468. } else {
  469. sbvalue.append(",?");
  470. reOtherFields.add(field);
  471. }
  472. sblist.append(",");
  473. sblist.append(field.getFieldname());
  474. }
  475. sblist.append(") ");
  476. sbvalue.append(")");
  477. return sblist.toString() + sbvalue.toString();
  478. }
  479. /**
  480. * @param attributes
  481. * @param reLobFields
  482. * @param reOtherFields
  483. * @return insert child sql
  484. */
  485. private String generalChildInsert(ArrayList<Field> attributes, List<Field> reLobFields,
  486. List<Field> reOtherFields) {
  487. StringBuffer sblist = new StringBuffer(" (fd_objectid,fd_parentid");
  488. StringBuffer sbvalue = new StringBuffer("values(?,?");
  489. Iterator<Field> it = attributes.iterator();
  490. while (it.hasNext()) {
  491. Field field = it.next();
  492. if (field.getDataType().equals(TypeUtil.BLOB)) {
  493. sbvalue.append(",EMPTY_BLOB()");
  494. reLobFields.add(field);
  495. } else if (field.getDataType().equals(TypeUtil.CLOB)) {
  496. sbvalue.append(",EMPTY_CLOB()");
  497. reLobFields.add(field);
  498. } else {
  499. sbvalue.append(",?");
  500. reOtherFields.add(field);
  501. }
  502. sblist.append(",");
  503. sblist.append(field.getFieldname());
  504. }
  505. sblist.append(") ");
  506. sbvalue.append(")");
  507. return sblist.toString() + sbvalue.toString();
  508. }
  509. /**
  510. * @param attributes
  511. * @param reLobFields
  512. * @param reOtherFields
  513. * @return update sql
  514. */
  515. private String generalUpdate(ArrayList<Field> attributes, List<Field> reLobFields,
  516. List<Field> reOtherFields) {
  517. StringBuffer sb = new StringBuffer(" set ");
  518. Iterator<Field> it = attributes.iterator();
  519. while (it.hasNext()) {
  520. Field field = it.next();
  521. if (field.getDataType().equals(TypeUtil.BLOB)) {
  522. reLobFields.add(field);
  523. } else if (field.getDataType().equals(TypeUtil.CLOB)) {
  524. reLobFields.add(field);
  525. } else {
  526. sb.append(field.getFieldname());
  527. sb.append("= ? ,");
  528. reOtherFields.add(field);
  529. }
  530. }
  531. return sb.deleteCharAt(sb.length() - 1).toString();
  532. }
  533. /**
  534. * @param tablename
  535. * @param objectID
  536. * @param reLobFields
  537. * @param con
  538. */
  539. private void insertLobField(String tablename, String objectID, List<Field> reLobFields,
  540. Connection con) throws SQLException {
  541. String sql = "select " + generalList(reLobFields) + " from " + tablename
  542. + " where fd_objectid =" + objectID + " for update";
  543. try {
  544. Statement stmt = con.createStatement();
  545. ResultSet lobDetails = stmt.executeQuery(sql);
  546. if (lobDetails.next()) {
  547. for (int i = 0; i < reLobFields.size(); i++) {
  548. Field field = reLobFields.get(i);
  549. if (field.getDataType().equals(TypeUtil.BLOB)) {
  550. if (field.getFieldvalue() != null) {
  551. byte[] content = (byte[]) field.getFieldvalue();
  552. if (content.length > 0) {
  553. InputStream is = new ByteArrayInputStream(content);
  554. writeBlob(lobDetails.getBlob(field.getFieldname()), is);
  555. is.close();
  556. }
  557. }
  558. } else {
  559. if (field.getFieldvalue() != null) {
  560. String content = (String) field.getFieldvalue();
  561. if (!content.equals("")) {
  562. Reader reader = new StringReader(content);
  563. writeClob(lobDetails.getClob(field.getFieldname()), reader);
  564. reader.close();
  565. }
  566. }
  567. }
  568. }
  569. }
  570. lobDetails.close();
  571. stmt.close();
  572. } catch (SQLException e) {
  573. log.error("updateLobField error ", e);
  574. throw e;
  575. } catch (IOException e) {
  576. log.error("close error ", e);
  577. throw new SQLException(e.getMessage());
  578. }
  579. }
  580. /**
  581. * @param tablename
  582. * @param objectID
  583. * @param reLobFields
  584. * @param con
  585. */
  586. private void updateLobField(String tablename, String objectID, List<Field> reLobFields,
  587. Connection con) throws SQLException {
  588. String clearLob = "update " + tablename + generalClearList(reLobFields)
  589. + " where fd_objectid =" + objectID;
  590. String sql = "select " + generalList(reLobFields) + " from " + tablename
  591. + " where fd_objectid =" + objectID + " for update";
  592. try {
  593. Statement stmt = con.createStatement();
  594. if (stmt.executeUpdate(clearLob) != 1)
  595. throw new SQLException("clear Lob failture");
  596. ResultSet lobDetails = stmt.executeQuery(sql);
  597. if (lobDetails.next()) {
  598. for (int i = 0; i < reLobFields.size(); i++) {
  599. Field field = reLobFields.get(i);
  600. if (field.getDataType().equals(TypeUtil.BLOB)) {
  601. if (field.getFieldvalue() != null) {
  602. byte[] content = (byte[]) field.getFieldvalue();
  603. if (content.length > 0) {
  604. InputStream is = new ByteArrayInputStream(content);
  605. writeBlob(lobDetails.getBlob(field.getFieldname()), is);
  606. is.close();
  607. }
  608. }
  609. } else {
  610. if (field.getFieldvalue() != null) {
  611. String content = (String) field.getFieldvalue();
  612. if (!content.equals("")) {
  613. Reader reader = new StringReader(content);
  614. writeClob(lobDetails.getClob(field.getFieldname()), reader);
  615. reader.close();
  616. }
  617. }
  618. }
  619. }
  620. }
  621. lobDetails.close();
  622. stmt.close();
  623. } catch (SQLException e) {
  624. log.error("updateLobField error ", e);
  625. throw e;
  626. } catch (IOException e) {
  627. log.error("close error ", e);
  628. throw new SQLException(e.getMessage());
  629. }
  630. }
  631. /**
  632. * @param reLobFields
  633. * @return
  634. */
  635. private String generalClearList(List<Field> reLobFields) {
  636. StringBuffer sblist = new StringBuffer(" set ");
  637. for (int i = 0; i < reLobFields.size(); i++) {
  638. Field field = reLobFields.get(i);
  639. if (i == 0)
  640. sblist.append(field.getFieldname());
  641. else
  642. sblist.append("," + field.getFieldname());
  643. if (field.getDataType().equals(TypeUtil.BLOB)) {
  644. sblist.append(" = EMPTY_BLOB()");
  645. } else if (field.getDataType().equals(TypeUtil.CLOB)) {
  646. sblist.append(" = EMPTY_CLOB()");
  647. }
  648. }
  649. return sblist.toString();
  650. }
  651. /**
  652. * @param reLobFields
  653. * @return
  654. */
  655. private String generalList(List<Field> reFields) {
  656. StringBuffer sblist = new StringBuffer();
  657. for (int i = 0; i < reFields.size(); i++) {
  658. Field field = reFields.get(i);
  659. if (i == 0)
  660. sblist.append(field.getFieldname());
  661. else
  662. sblist.append("," + field.getFieldname());
  663. }
  664. return sblist.toString();
  665. }
  666. private String generalListAndOrderBy(List<Field> reFields, StringBuffer sb) {
  667. StringBuffer sblist = new StringBuffer();
  668. for (int i = 0; i < reFields.size(); i++) {
  669. Field field = reFields.get(i);
  670. if (i == 0)
  671. sblist.append(field.getFieldname());
  672. else
  673. sblist.append("," + field.getFieldname());
  674. if (field.getOrderType() == Field.ASCEND)
  675. sb.append(field.getFieldname() + " asc,");
  676. else if (field.getOrderType() == Field.DESCEND)
  677. sb.append(field.getFieldname() + " desc,");
  678. }
  679. return sblist.toString();
  680. }
  681. /**
  682. * @param condition
  683. * @param sb
  684. * @return sql where
  685. */
  686. private String generalWhere(List<Field> condition, StringBuffer sb) {
  687. if (condition.size() == 0)
  688. return "";
  689. StringBuffer sbWhere = new StringBuffer(" where ");
  690. Iterator<Field> it = condition.iterator();
  691. boolean flag = true;
  692. while (it.hasNext()) {
  693. Field field = it.next();
  694. if (flag) {
  695. sbWhere.append(field.getFieldname());
  696. sbWhere.append(" = ? ");
  697. flag = false;
  698. } else {
  699. sbWhere.append(" and ");
  700. sbWhere.append(field.getFieldname());
  701. sbWhere.append(" = ?");
  702. }
  703. if (field.getOrderType() == Field.ASCEND)
  704. sb.append(field.getFieldname() + " asc,");
  705. else if (field.getOrderType() == Field.DESCEND)
  706. sb.append(field.getFieldname() + " desc,");
  707. }
  708. return sbWhere.toString();
  709. }
  710. @SuppressWarnings("deprecation")
  711. private void writeBlob(Blob blob, InputStream is) throws SQLException {
  712. // try {
  713. // OutputStream blobOutputStream = ((oracle.sql.BLOB) blob).getBinaryOutputStream();
  714. // byte[] buffer = new byte[1024];
  715. // int nread = 0; // Number of bytes read
  716. // while ((nread = is.read(buffer)) != -1)
  717. // blobOutputStream.write(buffer, 0, nread);
  718. // blobOutputStream.flush();
  719. // blobOutputStream.close();
  720. // } catch (SQLException e) {
  721. // log.error("get OutputStream error", e);
  722. // throw e;
  723. // } catch (IOException e) {
  724. // log.error("read InputStream error", e);
  725. // throw new SQLException(e.getMessage());
  726. // }
  727. }
  728. /**
  729. * @param clob
  730. * @param reader
  731. */
  732. @SuppressWarnings("deprecation")
  733. private void writeClob(Clob clob, Reader reader) throws SQLException {
  734. // try {
  735. // Writer clobWriter = ((oracle.sql.CLOB) clob).getCharacterOutputStream();
  736. // char[] cbuffer = new char[1024];
  737. // int nread = 0; // Number of bytes read
  738. // while ((nread = reader.read(cbuffer)) != -1)
  739. // clobWriter.write(cbuffer, 0, nread);
  740. // clobWriter.flush();
  741. // clobWriter.close();
  742. // } catch (SQLException e) {
  743. // log.error("get CharacterOutputStream error", e);
  744. // throw e;
  745. // } catch (IOException e) {
  746. // log.error("read Reader error", e);
  747. // throw new SQLException(e.getMessage());
  748. // }
  749. }
  750. private byte[] readBolb(Blob blob) throws SQLException {
  751. byte[] reContent = null;
  752. try {
  753. ByteArrayOutputStream os = new ByteArrayOutputStream();
  754. readBlob(blob, os);
  755. reContent = os.toByteArray();
  756. os.close();
  757. } catch (IOException e) {
  758. log.error("close error", e);
  759. throw new SQLException(e.getMessage());
  760. }
  761. return reContent;
  762. }
  763. private void readBlob(Blob blob, OutputStream os) throws SQLException {
  764. try {
  765. InputStream blobinputStream = blob.getBinaryStream();
  766. byte[] buffer = new byte[1024];
  767. int nread = 0; // Number of bytes read
  768. while ((nread = blobinputStream.read(buffer)) != -1)
  769. os.write(buffer, 0, nread);
  770. os.flush();
  771. blobinputStream.close();
  772. } catch (SQLException e) {
  773. log.error("get getBinaryStream error", e);
  774. throw e;
  775. } catch (IOException e) {
  776. log.error("write error", e);
  777. throw new SQLException(e.getMessage());
  778. }
  779. }
  780. private String readColb(Clob clob) throws SQLException {
  781. String reContent = null;
  782. try {
  783. StringWriter writer = new StringWriter();
  784. readClob(clob, writer);
  785. reContent = writer.toString();
  786. writer.close();
  787. } catch (IOException e) {
  788. log.error("close error", e);
  789. throw new SQLException(e.getMessage());
  790. }
  791. return reContent;
  792. }
  793. private void readClob(Clob clob, Writer writer) throws SQLException {
  794. try {
  795. Reader clobReader = clob.getCharacterStream();
  796. char[] cbuffer = new char[1024];
  797. int nread = 0; // Number of bytes read
  798. while ((nread = clobReader.read(cbuffer)) != -1)
  799. writer.write(cbuffer, 0, nread);
  800. writer.flush();
  801. clobReader.close();
  802. } catch (SQLException e) {
  803. log.error("get getBinaryStream error", e);
  804. throw e;
  805. } catch (IOException e) {
  806. log.error("write error", e);
  807. throw new SQLException(e.getMessage());
  808. }
  809. }
  810. @Override
  811. public ArrayList<DataObject> searchAllData(DataObject object, Connection con,
  812. String SQLcondition, String dataBase, String firstPageNum, String lastPageNum)
  813. throws PersistenceException {
  814. // TODO Auto-generated method stub
  815. return null;
  816. }
  817. }