Archive

Archive for the ‘Programming’ Category

magento upgrade

August 31st, 2011 No comments

rm -rf /var/cache session
./mage mage-setup .
./mage config-set preferred_state stable
./mage list-installed
./mage list-upgrades
./mage install http://connect20.magentocommerce.com/community Mage_All_Latest –force
./shell php indexer.php reindexall

 

app/etc/config.xml

<initStatements>SET NAMES utf8; SET FOREIGN_KEY_CHECKS=0; SET UNIQUE_CHECKS=0;</initStatements>

Categories: Programming, System, Web Tags:

Magento payment do not continue

October 16th, 2010 No comments

app/design/frontend/yourtheme/youtemplate/layout/page.xml

<action method=”addJs”><script>lib/ccard.js</script></action>

<block type=”page/html_head” name=”head” as=”head”>
<action method=”addJs”><script>prototype/prototype.js</script></action>
<action method=”addJs” ifconfig=”dev/js/deprecation”><script>prototype/deprecation.js</script></action>
<action method=”addJs”><script>lib/ccard.js</script></action>
<action method=”addJs”><script>prototype/validation.js</script></action>

…..

Categories: Programming Tags: , ,

Magento display “Only X left Threshold”

September 14th, 2010 1 comment

in layout/catalog.xml

<PRODUCT_TYPE_simple translate=”label” module=”catalog”>
<label>Catalog Product View (Simple)</label>
<reference name=”product.info”>
<block type=”catalog/product_view_type_simple” name=”product.info.simple” as=”product_type_data” template=”catalog/product/view/type/simple.phtml”>
<block type=”core/text_list” name=”product.info.simple.extra” as=”product_type_data_extra”/>
</block>
</reference>
</PRODUCT_TYPE_simple>

<PRODUCT_TYPE_simple translate=”label” module=”catalog”>        <label>Catalog Product View (Simple)</label>        <reference name=”product.info”>            <block type=”catalog/product_view_type_simple” name=”product.info.simple” as=”product_type_data” template=”catalog/product/view/type/simple.phtml”>                <block type=”core/text_list” name=”product.info.simple.extra” as=”product_type_data_extra”/>            </block>        </reference>    </PRODUCT_TYPE_simple>

Categories: Programming, Software Tags: ,

Replace blank space in file name

July 8th, 2010 No comments

for f in *; do mv “$f” `echo $f | tr ‘ ‘ ‘_’`; done

Categories: Programming, System Tags: ,

Spring MVC + Tiles

December 13th, 2009 No comments

1. create web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

2. create action-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="message" />
</bean>

<bean id="testController" class="com.bluewiseinc.erp.common.web.TestController">
<property name="methodNameResolver" ref="methodNameResolver" />
</bean>

<bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName">
<value>med</value>
</property>
<property name="defaultMethodName">
<value>userMain</value>
</property>
</bean>

<!--
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="cache" value="false" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</bean>

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<value>/WEB-INF/UrlMap.properties</value>
</property>
</bean>
</property>
</bean>

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles-def.xml</value>
</list>
</property>
</bean>

<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</bean>
</beans>

3. create TestController
package com.bluewiseinc.erp.common.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class TestController extends MultiActionController {
public ModelAndView testMain(HttpServletRequest request, HttpServletResponse response) throws Exception {
return new ModelAndView("test/test_main", "testMain", null);
}
}

4. UrlMap.properties
## test module mapping
/test/index.do=testController

5. tiles-def.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>
<definition name="defaultTemplate" template="/WEB-INF/view/tiles/default.jsp">
<put-attribute name="header" value="/WEB-INF/view/tiles/def_header.jsp" />
<put-attribute name="menu" value="/WEB-INF/view/tiles/def_menu.jsp" />
<put-attribute name="footer" value="/WEB-INF/view/tiles/def_footer.jsp" />
</definition>
<definition name="test_main" extends="defaultTemplate">
<put-attribute name="content" value="/WEB-INF/view/test/test_main.jsp"/>
</definition>
</tiles-definitions>

6. message.properties
# Page titles
index.title=Test Spring MVC + Tiles

7. default.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><fmt:message key="index.title"/></title>
<link rel=stylesheet href="${pageContext.request.contextPath}/css/global.css" type="text/css">
<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/global.js"></script>
</head>
<body>
<div id="header">
<div id="headerTitle"><tiles:insertAttribute name="header" /></div>
</div>
<div id="menu">
<tiles:insertAttribute name="menu" />
</div>
<div id="content">
<tiles:insertAttribute name="content" />
</div>
<div id="footer">
<tiles:insertAttribute name="footer" />
</div>
</body>
</html>

PHP Pear Image_Graph

September 1st, 2009 No comments

#apt-get install php-pear

#pear install Numbers_Words-0.16.1
#pear install Numbers_Roman-1.0.2
#pear install Image_Canvas-0.3.1
#pear install Image_Graph-0.7.2

(Download TrueType core fonts from http://corefonts.sourceforge.net/)
cp *.ttf /usr/share/php/Image/Canvas/Fonts

Image_Graph

<?php
require_once '../../includes/include.php';
require_once 'Image/Graph.php';
require_once 'Image/Canvas.php';
date_default_timezone_set('America/New_York');

$customer_id = $_REQUEST[customer_id];
if (!$customer_id) exit();
$customer_data = get_order_customer_report_xy($customer_id);
if (!$customer_data) exit();

$Canvas =& Image_Canvas::factory('png', array('width' => 575, 'height' => 280));

// create the graph
$Graph =& Image_Graph::factory('graph', $Canvas);
// add a TrueType font
$Font =& $Graph->addNew('font', 'DejaVuSans');
$Font->setSize(8);

$Graph->setFont($Font);

// create the plotarea layout
$Graph->add(
Image_Graph::vertical(
Image_Graph::factory('title', array('', 11)),
Image_Graph::vertical(
$Plotarea = Image_Graph::factory('plotarea'),
$Legend = Image_Graph::factory('legend'),
90
),
5
)
);

$Legend->setPlotarea($Plotarea);

// create a grid and assign it to the secondary Y axis
$GridY2 =& $Plotarea->addNew('bar_grid', IMAGE_GRAPH_AXIS_Y_SECONDARY);
$GridY2->setFillStyle(
Image_Graph::factory(
'gradient',
array(IMAGE_GRAPH_GRAD_VERTICAL, 'white', 'lightgrey')
)
);

list($year, $month) = split('[-.-]', $customer_data[0][1]);
$start=mktime(0,0,0,$month,1,$year);
$today=mktime(0,0,0,date('m'),1,date('Y'));
$interv=12-$month+(12*(date('Y')-$year-1))+date('m')+1;

// create a line plot
$Dataset1 =& Image_Graph::factory('dataset');
$Dataset2 =& Image_Graph::factory('dataset');

$max = 0;
for ($i=0; $i<count($customer_data); $i++) {
if ($max<$customer_data[$i][0]) $max=$customer_data[$i][0];
}
for ($i=0; $i<$interv; $i++) {
$Dataset2->addPoint(date('m/y', mktime(0,0,0,$month,1,$year)), 0);
$month++;
}
for ($i=0; $i<count($customer_data); $i++) {
list($year, $month) = split('[-.-]', $customer_data[$i][1]);
$Dataset1->addPoint(date('m/y', mktime(0,0,0,$month,1,$year)), $customer_data[$i][0]);
}

$Plot1 =& $Plotarea->addNew('bar', array(&$Dataset1));
$Plot1->setLineColor('red');

$Plot2 =& $Plotarea->addNew(
'Image_Graph_Plot_Area',
$Dataset2,
IMAGE_GRAPH_AXIS_Y_SECONDARY
);

$Plot2->setLineColor('gray');
$Plot2->setFillColor('white@0.2');

$AxisX =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X);
$labInterv = floor($interv / 13);
$AxisX->setLabelInterval($labInterv);
$AxisY =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y);
$AxisY->setTitle('Orders', 'vertical');
$AxisYsecondary =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y_SECONDARY);

// output the Graph
$Graph->done();
db_close()
?>

Categories: Programming Tags: , , ,

SwingWorker not on Java 6.0

April 24th, 2008 4 comments

SwingWorker is useful when a time-consuming task has to be performed following a user-interaction event (for example, parsing a huge XML File, on pressing a Button). The most straightforward way to do it is :

[sourcecode language='java']
private Document doc;

JButton button = new JButton(“Open XML”);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doc = loadXML();
}
});
[/sourcecode]

This will work, but unfortunately, the loadXML() method will be called in the same thread as the main Swing thread, so if the method needs time to perform, the GUI will freeze during this time.

[sourcecode language='java']
package com.wsjoung.demo;
import javax.swing.SwingUtilities;

public abstract class SwingWorker {
// see getValue(), setValue()
private Object value;
private Thread thread;

private static class ThreadVar {
private Thread thread;
ThreadVar(Thread t) { thread = t; }
synchronized Thread get() { return thread; }
synchronized void clear() { thread = null; }
}

private ThreadVar threadVar;

protected synchronized Object getValue() {
return value;
}

private synchronized void setValue(Object x) {
value = x;
}

public abstract Object construct();

public void finished() {
}

public void interrupt() {
Thread t = threadVar.get();
if (t != null) {
t.interrupt();
}
threadVar.clear();
}

public Object get() {
while (true) {
Thread t = threadVar.get();
if (t == null) {
return getValue();
}
try {
t.join();
}
catch (InterruptedException e) {
// propagate
Thread.currentThread().interrupt();
return null;
}
}
}

public SwingWorker() {
final Runnable doFinished = new Runnable() {
public void run() { finished(); }
};

Runnable doConstruct = new Runnable() {
public void run() {
try {
setValue(construct());
}
finally {
threadVar.clear();
}

SwingUtilities.invokeLater(doFinished);
}
};

Thread t = new Thread(doConstruct);
threadVar = new ThreadVar(t);
t.start();
}
}
[/sourcecode]

SwingWorker.java

[sourcecode language='java']
public class Task{
public void go() {
current = 0;
final SwingWorker worker = new SwingWorker() {
public Object construct() {
return new ActualTask();
}
};
}

class ActualTask {
ActualTask() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
}
}
}
[/sourcecode]

Task.java

Categories: Java, Programming Tags: ,

what's static?

April 14th, 2008 1 comment

First of all, the meaning of static in java is embedded or belong to a specific class not to its instance, and therefore there is no such static class.

  • There is no such static class
  • Embedded or belong to a specific class
  • Global variable
  • No need to load and create its instance

Most of methods and variables in Math class are static, because those are not need to be instanced in most of cases. we just use return value of PI or max(int a, int b).

class Bicycle {

private static int numberOfBicycles = 0;
private int id;

public Bicycle() {
id = ++numberOfBicycles;
}

public static int getNumberOfBicycles() {
return numberOfBicycles;
}
public int getID() {
return id;
}

public static void main(String[] args) {
Bicycle b1 = new Bicycle();
Bicycle b2 = new Bicycle();

System.out.println(“static result : “+getNumberOfBicycles());
System.out.println(“static result : “+numberOfBicycles);
System.out.println(“instance result : “+b1.getID());
System.out.println(“instance result : “+b2.getID());
}
}

Another characteristic of static is that even if we create instance of a class, jvm does not create instance variables for static resources. in above source code, there is no difference calling b1.getNumberOfBicycles(), b2.getNumberOfBicycles() and just getNumberOfBicycles() or directly numberOfBicycles
that mean we should call static variable with its class name because it’s belong to it. just like Bicycle.numberOfBicycles or in this example, getNumberOfBicycles() because we have a getter.
otherwise get confused.

Categories: Programming Tags: , ,

javamail smtp with Gmail

August 3rd, 2007 Comments off

package com.mycompany.emailsender;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.security.Security;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
* Servlet implementation class for Servlet: GmailSender
*
*/
public class GmailSender extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public GmailSender() {
super();
}

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
final String SMTP_HOST_NAME = “smtp.gmail.com”;
final int SMTP_HOST_PORT = 465;
final String SMTP_AUTH_USER = “XXXXXX@gmail.com”;
final String SMTP_AUTH_PWD = “XXXXXX”;
final String SSL_FACTORY = “javax.net.ssl.SSLSocketFactory”;

String emailFromAddress = null;
String[] sendTo = null;
String subject = null;
String content = null;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

String address = request.getParameter(“address”);
sendTo = address.split(“;”);

emailFromAddress = request.getParameter(“sender”);
this.subject = request.getParameter(“title”);
this.content = request.getParameter(“content”);

try {
for (int i=0; i<sendTo.length; i++) {
sendSSLMessage(sendTo[i]);
}
System.out.println(“Sucessfully Sent mail to All Users”);

} catch (Exception e) {System.out.println(e);}
}

public void sendSSLMessage(String dest) throws MessagingException {

Properties props = new Properties();

props.put(“mail.transport.protocol”, “smtps”);
props.put(“mail.smtps.host”, SMTP_HOST_NAME);
props.put(“mail.smtps.auth”, “true”);
props.put(“mail.smtps.quitwait”, “false”);

Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(false);
Transport transport = mailSession.getTransport();

MimeMessage message = new MimeMessage(mailSession);
message.setSubject(subject);
InternetAddress addressFrom = new InternetAddress(emailFromAddress);
message.setFrom(addressFrom);
message.setContent(content, “text/html”);

message.addRecipient(Message.RecipientType.TO,new InternetAddress(dest));

transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);

transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}

reference: http://www.rgagnon.com/javadetails/java-0083.html

Signed java applet

May 15th, 2007 No comments

Applet Security Basics

Below are the basic facts regarding applet security and Java Plug-in. More detail can be found in the next chapter, How RSA Signed Applet Verification Works in Java Plug-in.

  • All unsigned applets are run under the standard applet security model.
  • If usePolicy IS NOT DEFINED in the java.policy file, then a signed applet has the AllPermission permission if:
    Java Plug-in can verify the signers, and the user, when prompted, agrees to granting the AllPermission permission.
  • If usePolicy IS DEFINED, then a signed applet has only the permissions defined in java.policy and no prompting occurs.

Moreover, note that Java Plug-in now handles certificate management; i.e., the certificate verification task is no longer passed off to the browser.

keytool -genkey -keyalg rsa -alias MyCert
keytool -certreq -alias MyCert
keytool -import -alias MyCert -file VSSStanleyNew.cer

or

keytool -selfcert

and

jarsigner AppletName.jar MyCert
jarsigner -verify -verbose -certs AppletName.jar

Categories: Programming Tags: , , ,