Saturday, April 19, 2014

java.lang.IllegalStateException: ArrayAdapter requires the resources ID to be a TextView

Recently i was creating ListActivity for my Android application. After doing the necessary work, when i try to run the application, i was spatted with the error java.lang.IllegalStateException: ArrayAdapter requires the resources ID to be a TextView. What causes this issue?.

When you are passing an layout to the constructor like this new ArrayAdapter(this, R.layout.list_employee, this.employeeList). You need to make sure that your layout file list_employee wasn't wrapped by another layout. Unfortunately, my layout was wrapped with the RelativeLayout , which results in the above exception.

Below is the layout of my activity, which results in the exception

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:paddingBottom="@dimen/activity_vertical_margin"  
   android:paddingLeft="@dimen/activity_horizontal_margin"  
   android:paddingRight="@dimen/activity_horizontal_margin"  
   android:paddingTop="@dimen/activity_vertical_margin"  
  >  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="@string/employee_list_heading" />  
 </RelativeLayout>  

To Resolve the issue, i have just removed the layout element which just wrapped my TextView, and modified my XML as below

 <?xml version="1.0" encoding="utf-8"?>  
 <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="wrap_content"   
   // other attributes of the TextView  
 />  

Happy Programming...!!!

Sunday, January 26, 2014

Hibernate mapping exception: Could not determine type for columns: [org.hibernate.mapping.Column]

Recently when i was implementing Many to Many with Join columns with Additional column, i was getting the following error when i was building my application. Below is the exception from my application stack.

Invocation of init method failed; nested exception is org.hibernate.MappingException: 
Could not determine type for: com.expense.domain.Medicine, at table: Medicine_Shop, for columns: [org.hibernate.mapping.Column(medicine)]

This is because my application doesn't find all of your annotations, because I was annotating both fields and methods. You have to use only one strategy.
Either annotate in fields else methods, here in my case, i was annotating at field level in all classes, except in one class, where i have annotated at the method level (getter/setter) because of that annotation doesn't have any effect. Below is the sample code, which triggers the above exception, where you can see in the MedicineShop class, i have Annotated @EmbeddedId at the method level instead of the field level, Once i move the annotation to the field level, the issue is resolved.

@Entity
@Table(name="Medicine")
public class Medicine{
 
 @Id
 @GeneratedValue
 private int id;
 @Column(name="Name")
 private String name;
 @Column(name="CompanyName")
 private String companyName;
 @OneToMany(fetch=FetchType.LAZY, mappedBy="pk.medicine", cascade=CascadeType.ALL)
 private Set medicineShop= new HashSet();
}

@Entity
@Table(name="Medicine_Shop")
@AssociationOverrides({
 @AssociationOverride(name="pk.medicine", 
   joinColumns= @JoinColumn(name = "medicine_id")),
 @AssociationOverride(name="pk.shop", 
   joinColumns= @JoinColumn(name = "shop_id"))
})
public class MedicineShop implements Serializable 
{
 
 private MedicineShopId pk = new MedicineShopId ();
 @Column(name="capacity", length=10, nullable=false)
 private int capacity;
 
 @EmbeddedId
 public MedicineShopId getPk() {
  return pk;
 }
 public void setPk(MedicineShopId pk) {
  this.pk = pk;
 }
}


Happy Programming...!!!

Saturday, October 19, 2013

Angular JS - Basic Introduction and Example

AngularJS is a javascript framework supported by Google that embraces extending HTML into a more expressive and readable format. It decreases emphasis on directly handling DOM manipulation from the application logic, allowing for easier testing. It employs efficient two-way data binding and sensible MVC implementation, reducing the server load of applications, a new wave of Single Page Applications (SPA). Your application is defined with modules that can depend from one to the others. It also encapsulates the behavior of your application in controllers which are instanciated thanks to dependency injection. Lets start with the well know example in the computure world, Hello World !!!, why can't we change a bit lets say Hello Universe.

Before we can do anything we need to create a simple HTML page in that we can include AngularJS. Create a file called index.html and use the following code: which is just a normal html code, which includes Angular JS Javascript, regular label, textbox and two new items.

 <!DOCTYPE html>  
 <html ng-app>  
 <head>  
   <title>Learning AngularJS - Hello Universe</title>  
   <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>  
 </head>  
 <body>     
   Write Your Name in Textbox:  
   <input type="text" ng-model="yourname" />  
   <h1>Hello Universe by : {{ yourname }}</h1>  
 </body>  
 </html>  

First item which strikes our eyes is ng-app, which tells the active portion of he page. The directive ng-app will cause Angular to auto initialize your application.
Nextone is the attribute ng-model in textbox as ng-model="yourname". Whenever Angular sees this directive ng-model it automatically sets up two-way data binding.
How this works? When this page is loaded, Angualar bounds the state of text with model, thus when the user changes the value, model yourname will get's
automatically changed.
And finally, the mysterious expression {{ yourname }}: Which tells Angular to bind the value of model yourname in place of {{ yourname }}.

That's it we have just learned how to say Hello Universe through Angular

Happy Programming ...!!!

Monday, September 23, 2013

Not showing SVN Logs on Zend Studio 10

When i recently moved to Zend Studio 10 from 8, i see wired issue on displaying the SVN Logs. Whenever i commit my changes, Zend Studio 8, used to disply the logs on the SVN Console, but this was not happening in Zend Studio 10. How to resolve that?

You have to enable Console Output manually.

In the menu bar select Window->Preference.

In the Preference selecte Team -> SVN -> Console.

This trigger the Console Popup screen, as shown below. In that you will see options under Show console automatically, you have to select the option On Ouput which will show the SVN logs on the console.



Happy Programming...!!!

Saturday, September 14, 2013

Get Innerhtml by Parsing html DOM with DOMDocument

The domdocument class of PHP is a very handy one which can be used for a number of tasks like parsing xml, html and creating xml. In this tutorial we are going to see how to use this class to parse html content, and get a Innerhtml of a specific div element.

Below is the sample html code, from which we are going to get the innerhtml of the div tag with id=test2.
 <html>  
 <title>Test Content</title>  
 <body>  
 <div id="test1" class="container1">  
   Test content inside div id test1  
 </div>  
 <div id="test2" class="container2">  
   <p>Test content inside div id test2</p>  
 </div>  
 </body>  
 </html>  
Lets construct DOMDocument object and load the html content into it.
 $dom = new DOMDocument('1.0');;   
 $siteUrl = "http://*******";  
 //Fetch the filec content from the url  
 $file = file_get_contents($siteUrl);  
 // load the html into the object  
 $dom->loadHTML($file);   
 // discard white space  
 $dom->preserveWhiteSpace = false;  
Now the $dom object has loaded with the html content, which and can be used to extract contents from the whole html structure. DOMDocument has enough functions to make our work easier to parse and extract the content as needed, some of the common function which will be used frequently are getElementsByTagName and getElementById.

Lets how we can extract the div based on the id. To fetch the div based on id, we need to use the getElementById.

//get element by id
$test1div = $dom->getElementById('test1');

This will return the DOMNode, through which can fetch the text content my accessing $test1div->textcontent; that's simple. But if you want to fetch the innerhtml, you need to iterate your node, and add up all the child elements and display it. Below we have created a new method, which accepts the DOMNode
and iterates through all the childNodes within that, accumulates the data, and saves it as XML string and returns it.

 function getInnerHtml( $node )   
 {  
   $innerHTML= '';  
   $children = $node->childNodes;     
   foreach ($children as $child)  
   {  
     $innerHTML .= $child->ownerDocument->saveXML( $child );  
   }     
   return $innerHTML;  
 }  

That's it, you will get the Innerhtml of the DIV, with the all the html tags inside the div as it is.

Happy Programming...!!!

Sunday, July 28, 2013

How to integrate Spring Security to an existing web application?

Let see how we can integrate a Spring Security to an existing web application. Download the Spring Security from here, from the download add the following jar spring-security-core-3.2.0.M2.jar and spring-security-config-3.2.0.M2.jar from \spring-security-3.2.0.M2\dist, to your project lib folder.

Since this is the existing project, i assume the all the url pattern will be routed through Spring Dispatcher Servlet. Now let's modify our web.xml to add the filter, since spring security uses filter approach, this provides a hook into the Spring Security web infrastructure.


   org.springframework.web.context.ContextLoaderListener               


   contextConfigLocationWEB-INF\applicationContext-security.xml    

   springSecurityFilterChain
   org.springframework.web.filter.DelegatingFilterProxy


   springSecurityFilterChain
   /*


Next we need to create applicationContext-security.xml under WEB-INF

           


    

   

 
  
   
   
  
 
 
   

The element http in the applicationContext-security.xml, says that we want all URLs within our application to be secured, requiring the role ROLE_USER to access them. Thus we are forcing to authenticate ourselves when visiting our pages in the site.

Now to add some test users for a rapid development, we have configured two users here ramanujam and user1, with their password and roles using the authentication-provider and user-service. That's all you have integrated a simple security to your web application. Now if you access any of your web pages, you will redirecting to a default spring login pages.

Happy Programming...!!!

Saturday, July 13, 2013

Remot Sort : How to override SortParameters in Ext-Js Grid Panel?

When you sort using the gird, typically clicking on the column header, the dataIndex of the column will be passed as the sort properties, but not always the server will understand the grid name. for example, lets take below example.

Below is the Snap shot of the Activity Model
Ext.define('Activity', {
    extend: 'Ext.data.Model',
    uses: [ActivityType, ActivityStatus]
    fields: [
        {name: 'activityTypeId', type: 'int'},
        {name: 'activityStatusTypeId', type: 'int'},
        {name: 'accountId', type: 'int'},
        {name: 'dueDate', type: 'date', dateFormat: 'm/d/Y'},
    ],
});


The Relevant Java Model for the Activity
public class Activity {
  @ManyToOne(Fetch = FetchType.EAGAR)
  @JoinColumn(name = "activity_type_id")
  private ActivityType activityType;

  @NotNull
  @ManyToOne(Fetch = FetchType.EAGAR)
  @JoinColumn(name = "activity_status_type_id")
  private ActivityStatusType activityStatusType;

  @ManyToOne(Fetch = FetchType.EAGAR)
  @JoinColumn(name = "account_id")
  private Account account;

  @DateTimeFormat(pattern = "MM/dd/yyyy")
  private Date dueDate;
 
}

If you look at the variable names in Java Bean and Ext Model, you can see the difference. So when you are passing the sort parameters to your service call through, it needs to know the exact column name for example, Lets take activityStatusTypeId. If you click on the Column for Sort based on the activityStatsTypeId, the grid will pass on the dataIndex name as the property to the server. If you look at the FireBug console for the parameters, you see like this.

sort [{"property":"activityStatusTypeId", "direction":"DESC"}]

When you pass activityStatusTypeId to the service, really my server doesn't understand what activityStatusTypeId to the activity table. So we need to pass the extact column name as the sort property to make my service understand. To override the default parameters, we need to override getSortParam() in the colum config.

Ext.define('ActivitiesGridPanel', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.activitiesGridPanel,
    initComponent: function() {
       var me = this;
       Ext.applyIf(me, {
         columns:[
           { xtype: 'gridcolum',
             dataIndex: 'activityStatusTypeId',
             text: 'Status',
             getSortParam: function() {
                 return 'activity_status_type_id';
             }
           },
           { xtype: 'datecolumn',
             dataIndex: 'activityStatusTypeId',
             text: 'Due Date'
           },
           { xtype: 'gridcolum',
             dataIndex: 'activityTypeId',
             text: 'Type'
           },
         ]
       }    
    }    
});

After overriding the sortParameter for the column using getSortParam() function, if you click on the column in the gird, instead of passing the dataIndex name, it will sent out the overridden string as the sort parameter. You see the parameter in FirBug

sort [{"property":"activity_status_type_id", "direction":"DESC"}]

Happy Programming ...!!!