2015年3月30日 星期一

spring aopaspectj

package com.lfo.suntone.aspect;

import java.io.IOException;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import com.lfo.suntone.NotValidException;
import com.lfo.suntone.controller.StockController;
import com.lfo.suntone.dao.DaoUtilsInterface;
import com.lfo.suntone.tool.AspectTool;
@EnableAspectJAutoProxy(proxyTargetClass = true)
@Aspect
@Component

public class ControllerAspect {
    @Autowired
    private DaoUtilsInterface daoUtils;
  
    @Autowired
    private  AspectTool aspectTool;
    @Pointcut("execution(* com.lfo.suntone.controller.StockController.*(..))")
    public void header() {
      
    }
    @Around("header()")
    public Object   header(ProceedingJoinPoint  j) throws NotValidException {
      
      
      
      
         if(aspectTool.isValidUserid(j)){
             try {
          
                 System.out.println("header isValidUserid:");
                 daoUtils.insertHistory();
                Object ss = j.proceed();//不但攔截並且取回結果
                return ss  ;//必須回傳否則無法原方法return 為null

            } catch (Throwable e) {
                e.printStackTrace();
            }
         }else{
             System.out.println("header isnot  ValidUserid");
            
             StockController s=(StockController) j.getTarget();
             try {
                s.getResponse().getOutputStream().print("error userid");
            } catch (IOException e) {
              
                e.printStackTrace();
            }
             return null;
            
            
            
         }
        return null;
    }
}

2015年3月29日 星期日

spring root-context.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"
   
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    " >

<context:component-scan base-package="com.lfo.suntone"/>
   <mvc:annotation-driven>
        <mvc:message-converters>
            <!-- Use the HibernateAware mapper instead of the default -->
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="com.lfo.suntone.controller.HibernateAwareObjectMapper" />
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <context:component-scan base-package="com.lfo.sintone"/>
    <context:annotation-config /> 
    <!-- Root Context: defines shared resources visible to all other web components -->
        <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
    <property name="poolName" value="springHikariCP" />
   
    <property name="connectionTestQuery" value="SELECT 1" />
    <property name="dataSourceClassName"       value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
    <property name="dataSourceProperties">
        <props>
            <prop key="url">jdbc:mysql://localhost:3306/suntone</prop>
            <prop key="user">suntone</prop>
            <prop key="password">suntone</prop>
        </props>
    </property>
</bean>

<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
    <constructor-arg ref="hikariConfig" />
</bean>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
   <property name="configLocation" value="classpath:hibernate.cfg.xml" />
 
</bean>
<tx:annotation-driven   transaction-manager="transactionManager" />
<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:aspectj-autoproxy/>

  </beans>

CloneFactory clone object

protected class CloneFactory{
     protected Stock stock;
     CloneFactory(Stock stock){
         this.stock=stock;
     }
   
 }

2015年3月28日 星期六

How to update a spinner dynamically in Android?

http://stackoverflow.com/questions/3283337/how-to-update-a-spinner-dynamically-in-android
package com.lfo.sumtone.sdk;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Spinner;

import java.util.List;

/**
 * Created by user on 2015/3/25.
 */
public class ListBaseAdapter extends BaseAdapter {

    protected Holder holder;
    protected    Context context;
    protected LayoutInflater inflater;



    public  ListBaseAdapter( Holder holder ){
        inflater = LayoutInflater.from(holder.context);
        this.holder=holder;
        this.context=holder.getContext();

    }
    @Override
    public int getCount() {
        if(holder.getList()==null){
            return 0;
        }
        return holder.getList().size();
    }

    @Override
    public Object getItem(int position) {
        return holder.getList().get(position);
    }

    @Override
    public long getItemId(int position) {
        return holder.getItemId(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(holder.getLayout(), parent, false);

            holder.setConvertView(convertView);
            holder.setValue(position);

            holder.getConvertView().setTag(holder);
            //Utils.log("convertView == null,name:"+category.getProducts().get(position).getName());
        } else {
            holder = (Holder) holder.getConvertView().getTag();
            //Utils.log("convertView not null");
        }
        //holder.setValue(category.getProducts().get(position));

        return holder.getConvertView();
    }

    public Holder getHolder() {
        return holder;
    }

    public void setHolder(Holder holder,Spinner spinner) {
        this.holder = holder;
        spinner.setAdapter(this);
        this.notifyDataSetChanged();
    }
    public <T> void changeHolderList(List<T> list ,Spinner spinner){

        holder.list=list;
        spinner.setAdapter(this);
        this.notifyDataSetChanged();
    }
}

ItemFilter

package suntone.lfo.com.suntonehelper.adapter;

import android.content.Context;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.Filter;

import com.lfo.suntone.db.Stock;

import java.util.ArrayList;
import java.util.List;

import suntone.lfo.com.suntonehelper.tool.Utils;
import suntone.lfo.com.suntonehelper.tool.WarehouseTool;

/**
 * Created by user on 2015/3/28.
 */

//not used
public class ItemFilter extends Filter {
    private  List<Stock> list;
    private BaseAdapter baseAdapter;
   // private  View view;
    public ItemFilter(BaseAdapter baseAdapter, List<Stock> list){
       // this.view=view;
        this.list=list;
        this.baseAdapter=baseAdapter;
    }
    private  FilterResults results ;
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        //List<String> stringlist = WarehouseTool.instance.toNameList(list);
        if(results==null){
             results = new FilterResults();
        }
        if(constraint==null){
            results.values=list;
            results.count=list.size();
            return results;
        }
        if(constraint.length() ==0){
            results.values=list;
            results.count=list.size();
            return results;
        }
        //Utils.instance.log("constraint:"+String.valueOf(constraint));

        List<Stock> returnlist= new  ArrayList<Stock>();

        for(Stock stock:list){
            if(stock.getStocktype().getName().startsWith(String.valueOf(constraint))){
returnlist.add(stock);
            }
        }
        //List<String> returnstringlist = WarehouseTool.instance.toNameList(returnlist);
        results.values=returnlist;
        results.count=returnlist.size();


        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        // Now we have to inform the adapter about the new list filtered
        if(results.count==0){
            baseAdapter.notifyDataSetInvalidated();
        }else{
            baseAdapter.notifyDataSetChanged();
        }


    }

    @Override
    public CharSequence convertResultToString(Object resultValue) {
        if (resultValue==null){
            return "沒有名稱";
        }
        Stock s = (Stock) resultValue;
        return s.getStocktype().getName();
    }
}

2015年3月20日 星期五

post 參數

@RequestMapping("/stock/move")
public Status move(@RequestParam(value="stockId") int stockId, @RequestParam(value="destWarehouseId") int destWarehouseId){
   
    daoUtils.editWarehouseId(stockId, destWarehouseId);
   
    return new Status(true,String.valueOf(stockId));
}

為必要
(value="stockId")

2015年3月16日 星期一

Hibernate Native SQL

http://www.tutorialspoint.com/hibernate/hibernate_native_sql.htm

    @Transactional
    public  List listInForeignKey(Class classes,String fromtable,String foreignkeyname,Long foreignkey){

        Session session =beforeTransaction();
        //List  list = session.createCriteria( classes).createAlias(foreignkeyname, foreignkeyname).add(Restrictions.eq(foreignkeyname+"."+foreignkeyname, foreignkey)).list();
       

        SQLQuery query=whrere(session,classes,fromtable,foreignkeyname,foreignkey);
        List results = query.list();
       
        afterTransaction(session);
        return results;
    }
   
    private SQLQuery whrere(Session session,Class classes,String fromtable,String foreignkeyname,Long foreignkey){
        String sql = "SELECT * FROM ".concat(fromtable).concat(" WHERE ").concat(fromtable).concat(".").concat(foreignkeyname).concat(" =:").concat(foreignkeyname);
        //System.out.println("whrere:"+sql); whrere:SELECT * FROM stock WHERE stock.warehouseId =:warehouseId
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity( classes);
        query.setParameter(foreignkeyname, foreignkey);
        return query;
    }


@RequestMapping("/stock/list/{warehouseId}")
public List<Stock> listwarehouseId(@PathVariable("warehouseId") Long warehouseId){
    //http://localhost:8081/suntoneBackend/stock/list/2
    List<Stock> list = (List<Stock>)daoUtils.listInForeignKey(Stock.class, "stock", "warehouseId", warehouseId);
   
    return list;
}
http://localhost:8081/suntoneBackend/stock/list/2

2015年3月7日 星期六

install tomcat8

http://tecadmin.net/install-tomcat-8-on-centos-rhel-and-ubuntu/

install java

https://www.digitalocean.com/community/tutorials/how-to-install-java-on-centos-and-fedora

cd /opt
sudo wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" \
"http://download.oracle.com/otn-pub/java/jdk/8u25-b17/jdk-8u25-linux-x64.tar.gz"


sudo alternatives --install /usr/bin/java java /opt/jdk1.8.0_25/bin/java 1
sudo alternatives --install /usr/bin/javac javac /opt/jdk1.8.0_25/bin/javac 1
sudo alternatives --install /usr/bin/jar jar /opt/jdk1.8.0_25/bin/jar 1

How to Install VIM in CentOS

http://apetec.com/linux/InstallVIM.htm
yum install vim-X11 vim-common vim-enhanced vim-minimal