/****************************************************************************** * * Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. * All rights reserved. * ******************************************************************************/ package com.fourelementscapital.db.mariadb; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Vector; import org.apache.commons.dbutils.BasicRowProcessor; import com.fourelementscapital.db.FlexiFieldDB; import com.fourelementscapital.db.GeneralUtilDB; import com.fourelementscapital.db.vo.FlexiField; /** * FlexiField MariaDB DAO implementation */ public class FlexiFieldDBMariaDB extends FlexiFieldDB { /** * {@inheritDoc} */ public List getFlexiFields(String tablename) throws Exception { PreparedStatement pst=this.connection().prepareStatement("select * from flexi_field WHERE tablename=? ORDER BY displayorder"); pst.setString(1, tablename); ResultSet rs=pst.executeQuery(); List list= new BasicRowProcessor().toBeanList(rs, FlexiField.class); return list; } /** * {@inheritDoc} */ public void addFlexiField(FlexiField ffield) throws Exception { PreparedStatement pst=this.connection().prepareStatement("insert into flexi_field(fieldlabel,fieldtype,fieldoptions,tablename) values(?,?,?,?)"); pst.setString(1, ffield.getFieldlabel()); pst.setString(2, ffield.getFieldtype()); pst.setString(3, ffield.getFieldoptions()); pst.setString(4, ffield.getTablename()); pst.executeUpdate(); } /** * {@inheritDoc} */ public void updateFlexiField(long id,FlexiField ffield) throws Exception { PreparedStatement pst=this.connection().prepareStatement("UPDATE flexi_field SET fieldlabel=?,fieldoptions=? WHERE id=?"); pst.setString(1, ffield.getFieldlabel()); pst.setString(2, ffield.getFieldoptions()); pst.setLong(3, id); pst.executeUpdate(); } /** * {@inheritDoc} */ public void updateFlexiFieldOrder(long id,int order) throws Exception { PreparedStatement pst=this.connection().prepareStatement("UPDATE flexi_field SET displayorder=? WHERE id=?"); pst.setInt(1, order); pst.setLong(2,id); pst.executeUpdate(); } /** * {@inheritDoc} */ public void deleteFlexiField(long id) throws Exception { PreparedStatement pst=this.connection().prepareStatement("DELETE FROM flexi_field WHERE id=?"); pst.setLong(1, id); pst.executeUpdate(); } /** * {@inheritDoc} */ public Map getFlexiFieldData (String tablename, String idfield, String idvalue) throws Exception { PreparedStatement pst=this.connection().prepareStatement("select * from "+tablename+" WHERE "+idfield+"=?"); pst.setString(1, idvalue); ResultSet rs=pst.executeQuery(); HashMap data=new HashMap(); while(rs.next()){ long flexid= rs.getLong("flexi_field_id"); String val=rs.getString("val"); data.put(flexid, val); } return data; } /** * {@inheritDoc} */ public Vector getFlexiFieldData4LuceneToken (String tablename, String idfield, String idvalue) throws Exception { String query="select a.*,b.fieldlabel from "+tablename+" as a left outer join flexi_field as b on b.id=a.flexi_field_id WHERE a."+idfield+"=?"; PreparedStatement pst=this.connection().prepareStatement(query); //Log.debug("getFlexiFieldData4LuceneToken() query:"+query); pst.setString(1, idvalue); ResultSet rs=pst.executeQuery(); Vector rtn=new Vector(); while(rs.next()){ //Map rdata= new BasicRowProcessor().toMap(rs); Map rdata = GeneralUtilDB.resultsetToMap(rs); rtn.add(rdata); } return rtn; } /** * {@inheritDoc} */ public void saveFlexiFieldData (String tablename, String idfield, String idvalue, Map data) throws Exception { PreparedStatement pst1=this.connection().prepareStatement("DELETE FROM "+tablename+" WHERE "+idfield+"=?" ); pst1.setString(1, idvalue); pst1.executeUpdate(); pst1.close(); PreparedStatement pst=this.connection().prepareStatement("insert into "+tablename+"(flexi_field_id,"+idfield+",val) values (?,?,?)"); for(Iterator i=data.keySet().iterator();i.hasNext();){ Object ky=i.next(); Object val=data.get(ky); Long fid=Long.parseLong(ky+""); pst.setLong(1, fid); pst.setString(2, idvalue); pst.setString(3, val+""); pst.execute(); } pst1.close(); } }