Friday, January 31, 2014

Django Setup for Windows

I know that most Django developers are using Linux and such operating systems, but some of us have no choice but to use windows. And it actually works! Even in production! (although not the ideal choice...)

I've been working with Django for several years now (I used to develop mainly in Java/Spring/Hibernate etc.), and at first I was very skeptic, but now - I'm planning to start my new venture with Django/Python as well.

So, if you have to use a windows environment, here are the first steps to setup you env:

  1. Install Python
    Install python 2.7 (you can also use python 3, but it's different...and I'm not writing about this here) from http://www.python.org/download/
    Setup python 2.7 in your windows environment variables (if you do not know how to set the environment variables view http://www.itechtalk.com/thread3595.html).
  2. Database installation
    Install the database you want (MySQL, PostgreSQL etc. even SQLServer will work, but this is not a natural choice).
  3. Install the Database Driver
    For each database you chose, you should download the relevant driver for python, for example, MySQL driver can be found here - http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz/download, there's a windows installation available in codegood.com (I used this installation for my windows env).
  4. Install Pip
    So what is pip? Pip is the python package index, it will make your life easier! by using a simple command -
    pip install <your_package>
    you'll be able to install almost any python package available.
    To install pip, first download "easy install" from https://pypi.python.org/pypi/setuptools/2.1#windows
    Now, from the command line write
    easy_install pip 
    And you are ready with pip.
    To see the available packages that can be installed with pip see - https://pypi.python.org/pypi
  5. Install VirtualEnv (in a nutshell...)
    I'm not going to exaplin about the virtual anv here, but basically if you want to run several applications with several Django versions etc. You should use the virtual env, which stores in a local folder all the packages relevant to your app.
    To install -
    easy_install virtualenv
    Or
    python pip install virtualenv 
    See http://www.virtualenv.org/en/latest/virtualenv.html

  6. Eclipse setup with PyDev
    It's not mandatory, but it will make your life easier when coding.
    Download eclipse from http://www.eclipse.org/downloads/
    Install PyDev from eclipse "Help" --> "Install New Software" --> In the "work with" section add PyDev - http://pydev.org/updates

  7. Install Django
    From the command line run
    pip install django==1.6.1   
    (1.6.1 is currently the latest official version).
    If you are using a virtual env, make sure you run pip from within the relevant application.
  8. Start the Django Tutorial 
    https://docs.djangoproject.com/en/1.6/
    I recommend to start developing with eclipse.
I'm here if you have any questions,
good luck.




Monday, March 22, 2010

Joomla - Help! How to Add Tabs to JA Purity Template

Adding tabs to JA Purity is very simple if you know where to look.
Adding Tabs to Joomla JA Purity:

  • Login to Joomla administration (your_url/administrator/)
  • Go to Extensions --> Module Manager
  • Click the "Main Menu" module from the list - a "Module" editing page will be opened.
  • In the edit page that was opened, go to the details section at the left side of the page and change the "position" from 'left' (or any other value) to 'hornav'.


  • Save
  • Refresh your website, and you'll see the main menu as tabs instead of a list at the left side of the screen.
Good luck!

Joomla - Help! How to Change Joomla Default Template

I've just installed Joomla after a long long time and I'm trying to work with it again. I thought it would be simple, but it isn't. Here are a bunch of posts that will help anyone who is trying to do the same (I'm writing this so I won't forget...again):

How to Change Joomla Default Template:

  • Login to Joomla administration (your_url/administrator/)
  • Go to Extensions --> Template Manager
  • In the Template Manager screen, make sure that the "site" option is selected (it should be the default)
  • Select the template you want from the list (by default there are 3 options. If you added more templates or removed some you'll have a different list), and click the "Default" icon (on the top right corner with the yellow star icon.)
  • That's it - now you have a different template for your Joomla website.
Good luck!

Saturday, November 21, 2009

From Spring applicationContext.xml to Spring Annotations

Moving from Spring applicationContext.xml or any other Spring xml file to Spring annotations is done in a few steps.

First, make sure your Spring’s jar file is at least version 2.5 or download the Spring jar from SpringSource downloads.

Second, your definitions of dataSource bean, sessionFactory bean etc. are still needed, so you still need to have an XML file, though all the POJO bean definitions are replaced by Spring annotations.

So what are the available annotations?

The annotations that I’m going to use in this article are as follows:

- @Repository - org.springframework.stereotype.Repository
Repository is a class level annotation that is used for DAO objects.

- @Service - org.springframework.stereotype.Service
Service is a class level annotation that is used for Business Logic and general Façade POJOs (spring beans).

- @Autowired - org.springframework.beans.factory.annotation.Autowired
Autowired is a method/field level annotation. It handles the automatic detection of relations between classes. It is defined on a setter or a field and indicates that Spring should ‘inject’/initialize the filed with the relevant spring bean.

- @Transactional – org.springframework.transaction.annotation.Transactional
defines the transactional type of the transactions (for example propagation REQUIRED, NEW etc.)
It can be used at the class or method level.

Other useful Spring annotations:

- @Component - org.springframework.stereotype.Component
Component is a class level annotation. Indicates a Spring managed bean.
Repository and Service are both sub-annotations of Component.

- @Controller - org.springframework.stereotype.Controller
Controller is a class level annotation. It is used for Controller/Web controller POJOs. It is a Component as well.


Here you can view the annotations documentation in Spring 2.5 API.

Example of how to replace the applicationContext.xml with Spring annotations:

Before using the annotations:

I have 2 Spring beans (POJOs), one is a façade (MyFacade) and the other is a DAO (MyDao):


---------------------------------------

package com.myStuff.impl;


import java.io.Serializable;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class MyDaoImpl extends HibernateDaoSupport implements MyDao {

public Serializable createORM(MyOrm orm) {
return getHibernateTemplate().save(orm);
}

}

----------------------------------------

package com.myStuff.impl;

public class MyFacadeImpl implements MyFacade {

private MyDao myDao;

public void setMyDao(MyDao myDao) {
this.myDao = myDao;
}

@Override
public void someMethod() {

...

}

}

----------------------------------------

applicationContext.xml is defined with the following bean properties:

<bean id="MyDao" class="com.myStuff.impl.MyDaoImpl"
parent="daoTmpl" />

<bean id="MyFacade" class="com.myStuff.impl.MyFacadeImpl"/>

<bean id="daoTmpl" abstract="true" lazy-init="default"autowire="default"
dependency-check="default">
<property name="sessionFactory">
<
ref bean="sessionFactory" />
property>
bean>

----------------------------------------

In order to replace the Spring beans with annotations let’s make the following modifications in the applicationContext.xml file - We need to make sure that the “beans” tag in the applicationContext.xml imports the correct xsd files and that we enable the annotations configuration:

----------------------------------------

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

<context:component-scan base-package="com.myStuff" />
<context:annotation-config />

<bean id="MyDao" class="com.myStuff.impl.MyDaoImpl"
parent="daoTmpl" />

<bean id="MyFacade" class="com.myStuff.impl.MyFacadeImpl"/>

...

beans>

----------------------------------------

Once we’ve updated the “beans” definitions and added the <context:component-scan base-package="com.myStuff" /> and <context:annotation-config /> tags to the xml file, we can remove MyDao and MyFacade Spring beans from the applicationContext.xml and add the annotations.

So, what you’ll finally get is:


After we replaced the beans definitions with annotations
:

The POJO classes:

----------------------------------------

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

@Repository(value = "MyDao")
@Transactional
public class MyDaoImpl extends HibernateDaoSupport implements MyDao {

/* I had to add constructor in order to initialize
HibernateDaoSupport’s sessionFacade object.
*/
@Autowired
public MyDaoImpl(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}

@Override
public Serializable createORM(MyOrm orm) {
return getHibernateTemplate().save(orm);
}

}

----------------------------------------

@Service("MyFacade")
@Transactional(propagation = Propagation.REQUIRED)
public class MyFacadeImpl implements {

private MyDao myDao;

@Autowired
public void setMyDao(MyDao myDao) {
this.myDao = myDao;
}

@Override
public void someMethod() {

...

}

}

----------------------------------------

And the applicationContext.xml would be:

----------------------------------------

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

<context:component-scan base-package="com.myStuff" /> <context:annotation-config />

...

beans>

----------------------------------------

And that’s all. No more defining your custom Spring beans in XMLs.

Tuesday, November 17, 2009

HibernateDaoSupport with annotations - Solved!

I've been trying to move from Spring applicationContext.xml to Spring annotations only.
One of the problems I was facing was that when I tried to move my Dao from xml to Spring annotations I got some errors.

My Dao class was defined as follows:

public class MyDaoImpl extends HibernateDaoSupport implements MyDao {
...
}

My xml configuration was -

<bean id="MyDao" class="com.myStuff.dao.impl.MyDaoImpl"
parent="daoTmpl" />

<bean id="daoTmpl" abstract="true" lazy-init="default" autowire="default"
dependency-check="default">
<property name="sessionFactory">
<ref bean="sessionFactory" />
property>
bean>


My first try was to replace the Dao definition with the Spring annotations the following way:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository(value = "MyDao")
@Transactional
public class MyDaoImpl extends HibernateDaoSupport implements MyDao {
...
}

But then I got an error saying that the SessionFactory was not initialized in the dao -

Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required

To solve this problem (i.e. to be able to use Spring annotations only and remove the Dao definition from the xml file) you should @Autowire the session factory by creating a constructor in the Dao class:

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository(value = "MyDao")
@Transactional
public class MyDaoImpl extends HibernateDaoSupport implements MyDao {

@Autowired
public MyDaoImpl (SessionFactory sessionFactory)
super.setSessionFactory(sessionFactory);
}

...

}

and of course, you can and should remove MyDao definition from the xml file (not the daoImpl)

Hope this helps anyone who got this problem as well

- Li

Monday, November 16, 2009

Hibernate Annotations - Composite Key

There are several ways to create an ORM mapped to a composite key using hibernate annotations.
A code sample that shows how to create a composite key with hibernate annotations can be find below:

The Composite Key class:
-------------------------
import javax.persistence.Embeddable;

@Embeddable
public class MyCompositeKey implements Serializable {

private static final long serialVersionUID = 8057719523073696665L;

private String name;
private String value;

public MyCompositeKey() {

}

public MyCompositeKey(String name, String value) {
this.name = name;
this.value = value;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name== null) ? 0 : name.hashCode());
result = prime * result + ((value== null) ? 0 : value.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyCompositeKey other = (MyCompositeKey)obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}

}
-------------------------------------------------
The ORM class with the composite key mappings:
-------------------------------------------------
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "my_table_name")
public class MyClassOrm {

private MyCompositeKey id;
private String data;

public MyClassOrm() {

}

@Id
@AttributeOverrides( {
@AttributeOverride(name = "name", column = @Column(name = "name")),
@AttributeOverride(name = "value", column = @Column(name = "value")) })
public MyCompositeKey getId() {
return id;
}

public void setId(MyCompositeKey id) {
this.id = id;
}

@Column(name = "data")
public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}
}