azizkhani.net

I know that I know nothing

prototype

clock August 31, 2012 14:14 by author Administrator

 

A picture is worth a thousand words.

A prototype is worth a thousand lines of code.


Prototypes Are For Two-Way Communication

The key element, and the reason for creating prototypes, is to get two-way communication. A prototype is not just a status update about your design.

A prototype is the start of a conversation.

Prototyping is not only useful for design conversations, it is critical to understanding your requirements. Yes, a prototype will you get feedback about the design-execution of your proposed solution. More importantly, a prototype can help you make sure you’re solving the right market problems.

 



Top 10 Eclipse Shortcuts

clock August 24, 2012 23:04 by author Administrator

 

Here are some of my most used keyboard shortcuts for the Eclipse IDE.

  • Code Assist (CTRL + Space)

  • Quick Fix (CTRL + 1)

  • Refactoring (ALT + SHIFT + T)

  • Source (ALT + SHIFT + S)

  • Surround With (ALT + SHIFT + Z)

  • Delete Rows (CTRL + D)

  • Call Hierarchy (CTRL + ALT + H)

  • Quick Type Hierarchy (CTRL + T)

  • Quick Outline (CTRL + O)

  • Show All Shortcuts (CTRL + SHIFT + L)

 



setResultTransformer in hibernate

clock August 24, 2012 22:34 by author Administrator

یکی از مواردی که حتما در کار کردن با hibernate براتون پیش اومده اینه که یکquery بنویسیدکه نتیجه اون چیزی به غیر از یکی از

domain model شما باشه برای مثال استفاده از دستورات count,sum,و  در این حالت باید یک مدل جدید برای نتایج ایجاد کنید برای

مثال اگر شما بخواهید این نتایج را برگدونید باید روش زیر را انجام بدید

 
Iterator results = sess.createQuery(
        "select cat.color, min(cat.birthdate), count(cat) from Cat cat " +
        "group by cat.color")
        .list()
        .iterator();

while ( results.hasNext() ) {
    Object[] row = (Object[]) results.next();
    Color type = (Color) row[0];
    Date oldest = (Date) row[1];
    Integer count = (Integer) row[2];
    .....
}

 ولی این راه حل مشکلی که داره اینه که اولا وابسته به index نتایج هستید و کلا کار قشنگ و درستی نیست ولی حالا کد زیر را نگاه کنید

 
Query query=sess.createQuery("select cat.color, min(cat.birthdate), count(cat) from Cat cat
group by cat.color").setResultTransformer(Transformers.aliasToBean(YourObject.class));
List<YourObject> lst=query.list();
 


Hibernate NamingStrategy

clock August 24, 2012 22:02 by author Administrator

حتما تا حالا در مورد این قضیه فکر کردید که در هایبرنت کاش میشد یه کاری کنیم که نام گذاریهای تولید شده برای

table,constraint,..

به صورت اتوماتیک و طبق استاندارد مشخصی که خودمون تعریف میکنیم باشه برای مثال اول نام جدول

tbl

باشه برای این کار قابلیتی وجود داره که میتون این کار رو انجام بده

Hibernate NamingStrategy

کد زیر یک نمونه برای اینکار است که با توجه به محدودیتهای پایگاه داده اراکل(تعداد کاراکتر ۳۰ )برای نام گذاری است پیاده شده

package org.common.utility;

import org.hibernate.cfg.DefaultComponentSafeNamingStrategy;

import java.util.StringTokenizer;

public class OracleNamingStrategy extends DefaultComponentSafeNamingStrategy {
    private static final long serialVersionUID = -3020615242092992933L;

    protected static String addUnderscores(String name) {
        if (name == null)
            return null;
        StringBuffer buf = new StringBuffer(name.replace('.', '_'));
        for (int i = 1; i < buf.length() - 1; i++) {
            if ((isLowerToUpper(buf, i)) || (isMultipleUpperToLower(buf, i))

            ) {
                buf.insert(i++, '_');
            }
        }
        return buf.toString().toLowerCase();
    }

    private static boolean isMultipleUpperToLower(StringBuffer buf, int i) {
        return i > 1 && Character.isUpperCase(buf.charAt(i - 1))
                && Character.isUpperCase(buf.charAt(i - 2))
                && Character.isLowerCase(buf.charAt(i));
    }

    private static boolean isLowerToUpper(StringBuffer buf, int i) {
        return Character.isLowerCase(buf.charAt(i - 1))
                && Character.isUpperCase(buf.charAt(i));
    }

    @Override
    public String collectionTableName(String ownerEntity,
            String ownerEntityTable, String associatedEntity,
            String associatedEntityTable, String propertyName) {
        return abbreviateName(super.collectionTableName(
                addUnderscores(ownerEntity), addUnderscores(ownerEntityTable),
                addUnderscores(associatedEntity),
                addUnderscores(associatedEntityTable),
                addUnderscores(propertyName)));
    }

    @Override
    public String foreignKeyColumnName(String propertyName,
            String propertyEntityName, String propertyTableName,
            String referencedColumnName) {
        return abbreviateName(super.foreignKeyColumnName(
                addUnderscores(propertyName),
                addUnderscores(propertyEntityName),
                addUnderscores(propertyTableName),
                addUnderscores(referencedColumnName)));
    }

    @Override
    public String logicalCollectionColumnName(String columnName,
            String propertyName, String referencedColumn) {
        return abbreviateName(super.logicalCollectionColumnName(
                addUnderscores(columnName), addUnderscores(propertyName),
                addUnderscores(referencedColumn)));
    }

    @Override
    public String logicalCollectionTableName(String tableName,
            String ownerEntityTable, String associatedEntityTable,
            String propertyName) {
        return abbreviateName(super.logicalCollectionTableName(
                addUnderscores(tableName), addUnderscores(ownerEntityTable),
                addUnderscores(associatedEntityTable),
                addUnderscores(propertyName)));
    }

    @Override
    public String logicalColumnName(String columnName, String propertyName) {
        return abbreviateName(super.logicalColumnName(
                addUnderscores(columnName), addUnderscores(propertyName)));
    }

    @Override
    public String propertyToColumnName(String propertyName) {
        return abbreviateName(super
                .propertyToColumnName(addUnderscores(propertyName)));
    }

    private static final int MAX_LENGTH = 30;

    public static String abbreviateName(String someName) {
        if (someName.length() <= MAX_LENGTH)
            return someName;

        String[] tokens = splitName(someName);
        shortenName(someName, tokens);

        return assembleResults(tokens);
    }

    private static String[] splitName(String someName) {
        StringTokenizer toki = new StringTokenizer(someName, "_");
        String[] tokens = new String[toki.countTokens()];
        int i = 0;
        while (toki.hasMoreTokens()) {
            tokens[i] = toki.nextToken();
            i++;
        }
        return tokens;
    }

    private static void shortenName(String someName, String[] tokens) {
        int currentLength = someName.length();
        while (currentLength > MAX_LENGTH) {
            int tokenIndex = getIndexOfLongest(tokens);
            String oldToken = tokens[tokenIndex];
            tokens[tokenIndex] = abbreviate(oldToken);
            currentLength -= oldToken.length() - tokens[tokenIndex].length();
        }
    }

    private static String assembleResults(String[] tokens) {
        StringBuilder result = new StringBuilder(tokens[0]);
        for (int j = 1; j < tokens.length; j++) {
            result.append("_").append(tokens[j]);
        }
        return result.toString();
    }

    private static String abbreviate(String token) {
        final String VOWELS = "AEIOUaeiou";
        boolean vowelFound = false;
        for (int i = token.length() - 1; i >= 0; i--) {
            if (!vowelFound)
                vowelFound = VOWELS.contains(String.valueOf(token.charAt(i)));
            else if (!VOWELS.contains(String.valueOf(token.charAt(i))))
                return token.substring(0, i + 1);
        }
        return "";
    }

    private static int getIndexOfLongest(String[] tokens) {
        int maxLength = 0;
        int index = -1;
        for (int i = 0; i < tokens.length; i++) {
            String string = tokens[i];
            if (maxLength < string.length()) {
                maxLength = string.length();
                index = i;
            }
        }
        return index;
    }

    @Override
    public String classToTableName(String aClassName) {

        return abbreviateName(super
                .classToTableName(addUnderscores(aClassName)));
    }
}

حالا باید این را به تنظیمات هایبرنت اضافه کنیم

<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mappingLocations">
			<list>
				<value>/WEB-INF/classes/hibernate/**/*.hbm.xml</value>
			</list>
		</property>
		<property name="namingStrategy" ref="namingStrategy" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.connection.driver_class"> ${hibernate.connection.driver_class}</prop>
				<prop key="hibernate.connection.url"> ${hibernate.connection.url}</prop>
				<prop key="hibernate.connection.username"> ${hibernate.connection.username}</prop>
				<prop key="hibernate.connection.password"> ${hibernate.connection.password}</prop>
				<prop key="hibernate.dialect"> ${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql"> ${hibernate.show_sql}</prop>
				<prop key="hibernate.hbm2ddl.auto"> ${hibernate.hbm2ddl_auto}</prop>
			</props>
		</property>
	</bean>
	<bean id="namingStrategy" class="org.common.utility.OracleNamingSthrategy"/>


java.util package

clock August 20, 2012 15:55 by author Administrator



java.io package

clock August 20, 2012 15:54 by author Administrator



Java Language Classes

clock August 20, 2012 15:51 by author Administrator



how get ip in mac

clock August 20, 2012 15:44 by author Administrator

ipconfig in windows

ifconfig in mac



بدست آوردن ای پی در جاوا

clock August 20, 2012 15:36 by author Administrator

برای بدست آوردن آی پی باید تکه کد زیر را بنویسیم

import java.net.InetAddress; 
import
java.net.UnknownHostException;  
public
class app{  
public
static void main(String[] args){  

InetAddress ip;

try {  

ip = InetAddress.getLocalHost();

System.out.println("Current IP address : " + ip.getHostAddress());
  
}

catch
(UnknownHostException e) {  

e.printStackTrace();  

}  
}  
}




Output

 

Current IP address : 127.0.0.1

 



image

clock August 20, 2012 12:39 by author Administrator



About the author

 Welcome to this web site . This page has two purposes: Sharing information about my professional life such as articles, presentations, etc.
This website is also a place where I would like to share content I enjoy with the rest of the world. Feel free to take a look around, read my blog


Java,J2EE,Spring Framework,JQuery,

Hibernate,NoSql,Cloud,SOA,Rest WebService and Web Stack tech...

RecentPosts

Month List

Sign In