Fixed issue 18530. Changed reference of the product image. Added modulescript to convert images.
authorAntonio Moreno <antonio.moreno@openbravo.com>
Mon, 06 Feb 2012 09:58:07 +0100
changeset 15358 ce1acb5ef9dd
parent 15357 d26e6860e147
child 15359 d2f0ce67fd86
Fixed issue 18530. Changed reference of the product image. Added modulescript to convert images.
src-db/database/sourcedata/AD_COLUMN.xml
src-db/database/sourcedata/AD_FIELD.xml
src-util/modulescript/build/classes/org/openbravo/modulescript/ConvertImages.class
src-util/modulescript/src/org/openbravo/modulescript/ConvertImages.java
--- a/src-db/database/sourcedata/AD_COLUMN.xml	Mon Feb 06 10:02:12 2012 +0100
+++ b/src-db/database/sourcedata/AD_COLUMN.xml	Mon Feb 06 09:58:07 2012 +0100
@@ -130149,8 +130149,8 @@
 <!--800508-->  <HELP><![CDATA[Defines a system image]]></HELP>
 <!--800508-->  <COLUMNNAME><![CDATA[AD_Image_ID]]></COLUMNNAME>
 <!--800508-->  <AD_TABLE_ID><![CDATA[208]]></AD_TABLE_ID>
-<!--800508-->  <AD_REFERENCE_ID><![CDATA[32]]></AD_REFERENCE_ID>
-<!--800508-->  <FIELDLENGTH><![CDATA[12]]></FIELDLENGTH>
+<!--800508-->  <AD_REFERENCE_ID><![CDATA[4AA6C3BE9D3B4D84A3B80489505A23E5]]></AD_REFERENCE_ID>
+<!--800508-->  <FIELDLENGTH><![CDATA[32]]></FIELDLENGTH>
 <!--800508-->  <ISKEY><![CDATA[N]]></ISKEY>
 <!--800508-->  <ISPARENT><![CDATA[N]]></ISPARENT>
 <!--800508-->  <ISMANDATORY><![CDATA[N]]></ISMANDATORY>
--- a/src-db/database/sourcedata/AD_FIELD.xml	Mon Feb 06 10:02:12 2012 +0100
+++ b/src-db/database/sourcedata/AD_FIELD.xml	Mon Feb 06 09:58:07 2012 +0100
@@ -101157,7 +101157,7 @@
 <!--800724-->  <AD_TAB_ID><![CDATA[180]]></AD_TAB_ID>
 <!--800724-->  <AD_COLUMN_ID><![CDATA[800508]]></AD_COLUMN_ID>
 <!--800724-->  <ISDISPLAYED><![CDATA[Y]]></ISDISPLAYED>
-<!--800724-->  <DISPLAYLENGTH><![CDATA[100]]></DISPLAYLENGTH>
+<!--800724-->  <DISPLAYLENGTH><![CDATA[20]]></DISPLAYLENGTH>
 <!--800724-->  <ISREADONLY><![CDATA[N]]></ISREADONLY>
 <!--800724-->  <SEQNO><![CDATA[390]]></SEQNO>
 <!--800724-->  <ISSAMELINE><![CDATA[N]]></ISSAMELINE>
Binary file src-util/modulescript/build/classes/org/openbravo/modulescript/ConvertImages.class has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-util/modulescript/src/org/openbravo/modulescript/ConvertImages.java	Mon Feb 06 09:58:07 2012 +0100
@@ -0,0 +1,96 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License.
+ * The Original Code is Openbravo ERP.
+ * The Initial Developer of the Original Code is Openbravo SLU
+ * All portions are Copyright (C) 2012 Openbravo SLU
+ * All Rights Reserved.
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+package org.openbravo.modulescript;
+
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import javax.imageio.ImageIO;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import net.sourceforge.jnlp.util.PropertiesFile;
+
+import org.openbravo.modulescript.ModuleScript;
+import org.openbravo.database.ConnectionProvider;
+import java.io.FileInputStream;
+import java.util.Properties;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+
+import javax.imageio.ImageIO;
+
+public class ConvertImages extends ModuleScript {
+
+  @Override
+  public void execute() {
+    try {  
+      String catalinabase=System.getenv("CATALINA_BASE");
+      if(catalinabase==null || catalinabase.equals("")){
+        return;
+      }
+      Properties properties = new Properties();
+      properties.load(new FileInputStream(getPropertiesFile()));
+      File imagesFolder=new File(catalinabase, "/webapps/"+properties.getProperty("context.name")+"/web/images/");
+      if(!imagesFolder.exists() || !imagesFolder.isDirectory()){
+        return;
+      }
+
+      ConnectionProvider cp = getConnectionProvider();
+      String sql="SELECT i.imageurl, i.ad_image_id from ad_image i, m_product p where ";
+      sql+=" i.ad_image_id=p.ad_image_id and binarydata is null order by i.ad_image_id ";
+      ResultSet rs=cp.getPreparedStatement(sql).executeQuery();
+      while(rs.next()){
+        String imageurl=rs.getString(1);
+        String imageid=rs.getString(2);
+        File imageFile=new File(imagesFolder, imageurl);
+        if(imageFile.exists()){
+          FileInputStream is = new FileInputStream(imageFile);
+          long length = imageFile.length();
+          byte[] bytes = new byte[(int)length];
+
+          int offset = 0;
+          int numRead = 0;
+          while (offset < bytes.length
+                 && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
+              offset += numRead;
+          }
+          is.close();
+          ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
+          BufferedImage rImage = ImageIO.read(bis);
+          String qupdate="UPDATE ad_image set binarydata=?, width=?, height=? where ad_image_id=?";
+          PreparedStatement ps=cp.getPreparedStatement(qupdate);
+          ps.setObject(1, bytes);
+          ps.setLong(2, rImage.getWidth());
+          ps.setLong(3, rImage.getHeight());
+          ps.setString(4, imageid);
+          ps.executeUpdate();
+          cp.releasePreparedStatement(ps);
+        }
+      }
+    } catch (Exception e) {
+      return;
+      //This modulescript can fail in different ways.
+      //However, the build should continue without problems
+    }
+    return;
+  }
+}