Adding Path to Environment in Ubuntu

After adding some software or programing language or whatever to ubuntu, mostly using the non-synaptic method, our command line would crave for the new terms ;).
Here is how to add the new path to the environment
Lets use how to add the tomcat app server after we install it using the tar file.

1. Open the terminal : Application->Accessories->Terminal
2. use your favorite text editor to open /etc/environment in my case I would use view as vi /etc/environment
3. add the path on new line as CATALINA_HOME=”path/to/catalina”
4. save and exit!!
In most cases, the current shell would not see the effect and you may want to open another tab for that or you can use the export command

if we are appending to the existing path variable, we can use the export command as
export PATH = ${PATH}:/path/to/new/location – here the ${PATH} would maintain the existing path information and the colon would be the separator for the paths

Server sent unexpected return value (405 Method Not Allowed) in response to MKCOL request error in svn

The error pops up when trying to commit to the repository.

This SVN error happens when trying to add the directory/file to the repository while the directory is already existing.

It can happen when one of the coworker creates a directory and commits and the other one tries the same task [svn add directory/file] without updating from trunk first.

Solution

A couple of options are there for this.

1. Update to the repository and deal with the conflicts if there are any.
2. if the created directory is assumed to be redundant, delete the directory and recommit it.

Also: Checkout how to identify changes in SVN

Could not open Hibernate Session for transaction

This problem happens when the connection to the database is disconnected or was not created at all by the application, but pool assumes connection is established.
Remedy for this could be checking credential to the database [username, password, host, port] and also checking the connection object before proceeding to database tasks like reading or updating.

Getting list of sub and super class member variables

On my project, I need to collect the member variables of the classes that I am working on.
But, since, the classes have common member variables like date_created and date_modified and others, I have Abstract class for those member variables and their setters and getters
I have been using get_class_vars for such cases, but I want to call this from the parent and be able to access the member variables at run time. which was a problem, it would collect only the parent member variables:

        $classProperties = array();
        $classProperties['vars'] = get_class_vars(get_class($this));

But this hasn’t work.
Then I used the reflection method:

        $child_reflection = new ReflectionClass($this);
        $child_properties = $child_reflection->getdefaultProperties();
        $parent_reflection = new ReflectionClass(__CLASS__);
        $parent_properties = $parent_reflection->getdefaultProperties();
        $class_properties['vars'] = array_merge($child_properties
, $parent_properties);

This would give all properties for the class. In this case it should be up to the developer to limit the access and manipulate the use of the properties not to loose the semantics of having them 🙂

d i s   i z – IT!

Adding Date to the template in netbeans

Updating netbeans template with date

It is common for the source code file to contain the name of the author, copyright notices, the purpose of the script and the date as well.

Can you find the longest palindrome from given sequence of characters?

IDEs would come with template of the source file to auto populate that for you. Netbeans is also doing that.

Netbeans also provides default template placeholders for author and other artifacts.

Importing packages to eclipse from maven – See how easy it is

All the others seems easy to proceed, but if you want to add date [@since] to your source file in netbeans here is how to do it.

Here is how you can add automatic date to the template.

1. Go to tools->templates
2. Select file that you are working on
3. add @since ${date} to the template where you would want to show date

 

Step by step tutorial for nodejs, angularjs and mongodb

Passing all jars to classpath from command line when compiling java

Number formatting in PHP

OK while coding numbers are almost always there. Bit it stat, some calculation, rate finding … a lot is on numbers.
Saying so, we might want to beautify our numbers on displaying or storing or making them an input for another usage or we just want it.
PHP has the function number_format() for this usage: this accepts up to 4 parameters where we can supply either one, two or four of them.

lets have this number 456234.3451678 as a result of some calculation.
$number = 456234.3451678;
Taking the integral part:
we can do this in different methods
1. casting it to integer:

(int)$number;

2. using built-in function:

number_format($number); // output -> 456,234

3. using regex:

 
        $pattern="/.d*/";
        $number = preg_replace($pattern, '', $number);

After this operation, we might need to cast it back to the number for type safety cases.
Also we can use str_replace

Limiting number of decimals after decimal point
This also can be achieved in different ways:
1. Using function:

number_format($number, 2); //takes the two digits next to decimal point

2. Using regex:

preg_match('/(d+(.?d{0,2})?)/', $number, $matches); // here matches[0] would contain the required value

SimpleXMLElement not working with some XML

What other object would make life easier in regard to XML manipulation in PHP other than SimplXMLElement.
I was using this object in deciphering the SOAP response, I could have used other tools for SOAP actually, and found out the object is not happy when it guests xmlns.
All I have to do was to replace the xmlns whole part and SimpleXMLElement was happy 🙂

Unknown modifier ” error preg_match

Running this error while adopting preg_mathc in php? Just check if the delimiters are not used in your string – mostly this would fix the problem.

$example = "<root><tag1>content1</tag1></root>";
$pattern = "/<tag1>.+</tag1>/";
preg_match($pattern, $example, $matches) ; 

If you run the above snippet, you would run the error as the delimiter ‘/’ is the part of the string – so changing the delimiter, say, to ‘?’ would solve the problem. like

 $pattern = "?<tag1>.+</tag1>?

Hope it would help.

Changing default Apache webroot in Ubuntu

By default the webroot for apache/php is in /var/www

To change this, say, to /home/user/workspace/public_html

1 Go to file: /etc/apache2/sites-available/default
2. Change the /var/www to /home/user/workspace/public_html – as simple as this!!

OR,

If you are changing root just to abscond some difficulty from your IDE or just for temporary purpose, you can use the symlik: just open your terminal and –
cd /home/user/workspace/public_html
ln -s /var/www/project project

hibernate automatically saves/edits objects that are updated

Kind of mysterious – all you want to do would be to update the some component on your java logic and it is automatically saved in the database.
Here is the typical scenario, if you are updating collection of objects and and updating one object would also update others based on some logic – say reordering of numbers, the session would all be saved without saving all the collections – yup I got this exact situation and the reason was

After the update, I have another ajax call to the backend where I would get the list of objects. And the method which collect the objects was annotated as Transactional. In this case, all the dirty objects in the session would be saved though what I wanted was to read only.

So, I changed the annotation to Transactional(readOnly=true) – that took care of it.