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.

SVN integration to Eclipse setup and error – unable to load default svn client

-startup
plugins/org.eclipse.equinox.launcher_1.0.201.R35x_v20090715.jar
–launcher.library
plugins/org.eclipse.equinox.launcher.gtk.linux.x86_1.0.200.v20090520
-showsplash
org.eclipse.platform
–launcher.XXMaxPermSize
256m
-vmargs
-Djava.library.path=/usr/share/java/
-Djava.library.path=/usr/lib/jni/
-Xms128m
-Xmx512m
-Dorg.eclipse.equinox.p2.reconciler.dropins.directory=/usr/share/eclipse/dropins

http://subclipse.tigris.org/update_1.6.x – depends on what eclipse you have – check this for compatibility. how do i know what version i have.

sudo apt-get install libsvn-java

default path /usr/lib/eclipse/eclipse.ini

http://ubuntuforums.org/showthread.php?t=916633

One of prevailing repository tools is SVN. I like and use it. I am also a fun of eclipse. The two would make programming cool.

To install SVN in eclipse

Go to http://subclipse.tigris.org/ and see the download and install link. Check which to install based on the Eclipse version – which is depicted on the page.

Install JavaHL

From your command line in Linux: sudo apt-get install libsvn-java.
This is the major factor for the unable to load default svn client error.

Finally

Update your eclipse.ini by adding this line just next to -vmargs at the end
-Djava.library.path=/usr/share/java/
-Djava.library.path=/usr/lib/jni

The location of eclipse.ini would be in your installation folder. The default location for eclipse which loaded from package would be /usr/lib/eclipse/eclipse.ini

Now, restart your Eclipse and enjoy SVNing.

Adding multiple javascript files to page

I was pulling my hair just for some silly stuff. All I wanted was to put two external javascript file for my floundering page – that was it!! and I do it as

<script language="javascript" src="sourceone" />
<script language="javascript" src="sourcetwo" />

BTW, I am using firefox on ubuntu, then when I try to run the page it would turn white. Astonishingly, the kick for the error was

<script language="javascript" src="sourceone"></script>
<script language="javascript" src="sourcetwo" ></script>

Showing error message on selectOneMenu while selecting default

So, we have a menu, brimmed with our objects from list/database. The intention would be creating a default “Select” kind of the first row and to brag on the user when s/he selects the default.

The trick is simple, create the first list with null object and label it as Select. Lets show it with simple example.
We want to have a list of programming languages on our select menu. And, the first would be a default object.

    //Actual domain class
    class Language{
        private String name;
        private String type;
        private boolan isCompiled;

       getters/setters goes here
   }
   //Bean class
   class someBean{
        private List languages;
        .
        .
        public String getLanguages(){
            //Let languages would be filled by some service or factory
            this.languages = someLanguageFactory.getAllLanguages(); 
           //Assign the first element as null
           this.languages.add(0, null);
       }
   }
   
   //JSF page
   :
   :
   <h:selectOneMenu id="selectLanguage" value="someBean.languages" 
     requred="true" ....

This should do the task, make the list required, and populate the first list null. So, during validation if the selection is null it would be rejected as we have told it to watch an eye on blank fields.