May 24, 2012

Symfony - Disable sf_web_debug in your web page

In our development environment, there some way we need to programmaticall disable the Web Debug. In some instance like we are retrieving text data on out controller.
  public function executeGetText(sfWebRequest $request)
  {
     sfConfig::set('sf_web_debug', false); // Here to disable web debug only and hase html response
     // Some code here.. Good luck =)
  }
You can also do this one, this will return you the plain text.
  public function executeGetText(sfWebRequest $request)
  {
     $this->getResponse()->setHttpHeader('Content-type', 'text/plain', true);
     // Some code here.. Good luck =)
  }

May 14, 2012

Struts - Calculating on struts tags

We need to initilaize variable.
<s:set var="total" value="%{0.00}"/>
Calculation
<s:set var="total" value="%{amount + #attr.total}" />

May 11, 2012

Swapping Algorithm without extra variable.

One way to this is by using the Exclusive OR (XOR)

Sample Code

$x = $x ^ $y;
$y = $y ^ $x;
$x = $x ^ $y;

Explanation

Lets try to use X=5 and Y=15; Now let convert the value of X and Y to each Binary data. Here's is, X=0101 and Y=1111

  • Lets see first statement.
    $x = $x ^ $y; Binary: 1010 ^ 1111 ------ 0101 ( X now has value of 10)
  • Second Statement
  • Last statement

Paging Algorithm

Paging Algorithm
int topage = maxRecords * currentPage;
int offset = topage - (maxRecords);

May 9, 2012

Struts - Formating Text, Date or Number


Formating Pattern

format.time = {0,time}
format.number = {0,number,#0.0##}
format.percent = {0,number,##0.00'%'}
format.money = {0,number,\u00A4##0.00}

Formatting a number

<s:property value="getText('{0,number,#,##0.00}',{product.price})" />

May 8, 2012

JAVA - Reading file relatively

This will all getting the base path of you application to know how/where to read the file

Eclipse

Java Application


* File function
File fXmlFile = new File("test.xml");
The will look on the PROJECT_DIR path.
/YOUR/PROJECT_DIR/test.xml
* FileInputStream
FileInputStream is = new FileInputStream("test.cfg.xml");

The will look on the ECLIPSE path.
/YOUR/ECLIPSE_DIR/test.cfg.xml

*
Loader context
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
classLoader.getResource(".").getPath()
This will return: PROJECT_DIRt/build/classes/

Web Application(Running in Tomcat)


*
Loader context
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
classLoader.getResource(".").getPath()
This will return the path TOMCAT_DIR/lib/

* File function
File fXmlFile = new File("test.xml");
The will look on the ECLIPSE_DIR path.
/YOUR/ECLIPSE_DIR/test.xml

Deployed


* Java Application

* Web Application(Running in Tomcat)
* Loader context
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
classLoader.getResource(".").getPath()
This will return the path TOMCAT_DIR/lib/

* File function
File fXmlFile = new File("test.xml");
The will look on the
TOMCAT_DIR path.
/TOMCAT_DIR/test.xml

May 2, 2012

Java: org.hibernate.ObjectNotFoundException: No row with the given identifier exists:

None-annotated

<many-to-one name="employee" class="Employee"
column="CITY_ID" not-null="false" lazy="false" not-found="ignore">
</many-to-one>

Annotated

@NotFound(action=NotFoundAction.IGNORE)
@OneToOne
@JoinColumn(name="CITY_ID")
private City city;
// your getter and setter here