Wednesday, November 25, 2015

Android Shared Preferences.

Shared Preferences are used in Android to store data. These data can be used in appropriate places in the application from reading them.
The shared preferences allows us to manage a session. Session is useful when need to store user data globally throughout the application. This can be done storing the data in shared preferences.
Shared Preferences is an API which includes in Android SDK to store and retrieve application preferences. Shared Preferences are simply sets of data in key value pair that stored persistently. Furthermore if the application stop or turn off the device will not clear the data stored in  shared preferences. Shared Preferences available at the Activity level or shared across all Activity in application package.
Shared Preferences are used in android to store some data persistently so when you need to store few amount of data then you can use Shared Preferences instead of SQLite.
But shared preference is not a solution to keep complex relational data.

The following basic information will be  needed when you use the Shared Preferences in your application.

Store Data in Shared Preferences.
All the primitive data types like boolean, floats, int, longs, and strings can store in Shared Preferences using following methods.
editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string
editor.putInt("key_name", "int value"); // Storing integer
editor.putFloat("key_name", "float value"); // Storing float
editor.putLong("key_name", "long value"); // Storing long


Retrieve data from Shared Preferences.
All the primitive data types like boolean, floats, int, longs, and strings can retrieve from Shared Preferences using following methods.
// returns stored preference value
// If value is not present return second param value - In this case null
pref.getString("key_name", null); // getting String
pref.getInt("key_name", null); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean

Delete data from Shared Preferences
If you want to delete from shared preferences you can call remove(“key_name”) to delete that particular value. If you want to delete all the data, call clear().
Eg :- editor.remove(“email”) – this will remove the key email from your Shared Preference.
Editor.clear() – this will clear all the data from shared preferences.

 Save Changes in Shared Preferences
You can save all the changes in Shared Preferences using commit().

The following example method implemented to show how to store data in the Shared Preferences.
public static void saveGlobalStringPreferences(final Activity activity,
                                    final String key, final String value) {
                        SharedPreferences sp = PreferenceManager
                                                .getDefaultSharedPreferences(activity.getApplicationContext());
                        Editor editor = sp.edit();
                        editor.putString(key, value);
                        editor.commit();
}
As you can see above, after storing a particular data in shared preferences need to commit it to save the changes successfully.

The following example method implemented to show how to retrieve data from the Shared Preferences.
public static String readGlobalStringPreferences(final Activity activity,
                                    final String key, final String defaultValue) {
                        SharedPreferences sp = PreferenceManager
                                    .getDefaultSharedPreferences(activity.getApplicationContext());
                        return sp.getString(key, defaultValue);
}

The following example method implemented to show how to delete data from the Shared Preferences.

public static void removeGlobalPreferences(final Activity activity,
                                    final String key) {
                        SharedPreferences sp = PreferenceManager
                                    .getDefaultSharedPreferences(activity.getApplicationContext());
                        Editor editor = sp.edit();
                        editor.remove(key).commit();
}
As you can see above, after removing a particular data from shared preferences need to commit it for successfully deletion.
For more details Click Here 

Sunday, November 22, 2015

GIT

 

What is Git ?
Git is a version control system.
Version control system is a system which allows developers to work together. In a version control system the developers can do the implementations simultaneously without overwriting each other’s  changes.
There are two types of version control  systems called Centralized Version Control System and Distributed Version Control System.
In Centralized Version Control System uses a central server. This server enables to store all files and all the developers work with this central server. But the worst case  is the single point of failure.
In distributed version control system’s this worst case scenario does not exists because if any failure is occurred in the server then the repository from any client can be copied back to the server to resolve the failure.
Git belongs to Distributed Version Control System type.

Install Git in your machine [1].

Terminology in Git

  •  Local repository – This is your private working place which is in your machine.
  • Working directory -  This is used to do the all modification and then you can commit them in to your local repository .
  • Commit – Commit holds the current status of the repository
  • Branch – When we need to add a new feature to the project then we create a new branch and do that modification within that branch. After the all modifications we can merge the branch to the master branch and delete the  temporary created branch. Where master branch means the default   branch.
  • Clone – Create a local repository from the git repository
  • Pull – Copy changes from remote repository to local repository.
  • Push -  This is used to store all the changes permanently in git repository.


How to  Make a pull request?

Fork the repository from parent repository to your Git repository.
To do this go to the parent repository and click on the ‘’Fork’’ button as shown below.

Clone the project from your Git repository to your local repository using following command.

 > git clone <HTTP clone URL>

 The following figure shows the HTTP clone URL.




















Go inside the cloned directory from command prompt and enter the following command.


>git remote - v.

The above command shows the origin.

Create an upstream.

>git remote add upstream <HTTP clone URL of parent repository>

Default you have a master branch. But if we do the modifications we need to create a new branch to do the modifications.
Create a new branch using the following git command

> git checkout -b <name for the branch>

Now manually do your changes in to your local repository.
To view the all modified files/directories use the following git command.

>git status

 Now you can see the modified files in red color in command prompt as shown in below figure.

















The modifications that you made can view  using the following git command.


>git diff


You can view all the modification as shown below.
































Add those modification to the relevant files using the following git command.


>git add <Path of the modification file/directory>

Then you can view the successfully added modifications in green color as shown below.







Commit the changes in to local repository.


>git commit

Push the all modifications to git repository using following git command. This will add the all the changes to your git repository.

>git push origin <branch name you are in>

Then go to your git repository and then you can see a button called “Create pull request” .
Go down and then you can see the modified files. Again check whether the modifications are correct or not.
Click on “Create Pull Request”.

[1].https://git-scm.com/book/en/v2/Getting-Started-Installing-Git







Tuesday, November 17, 2015

Publish Data From WSO2 ESB to WSO2 DAS.

Prerequisites

  • WSO2 ESB (Enterprise Service Bus - 4.9.0).
  • WSO2 DAS (Data Analytic Server - 3.0.0) .
The purpose of writing this blog post is to demonstrate how to publish a data stream from WSO2 ESB to WSO2 DAS and persist the data in a MySQL database.
  
First you need login to the WSO2 ESB management console and add an event sink to the ESB. To do that select configure tag and click on Event Sinks. Now you can see the event Sinks Configuration console. Click on Add Event Sink then you will receive a console to enter details about the event sink as shown in the figure 2.

Figure 1


Enter a proper name to the event sink. Then enter the user name and the password.
Receiver URL : tcp:// <your IP address > <thrift port of DAS>
Authenticator URL : ssl:// <your  IP address><authentication port of DAS>

By default the thrift port and the authentication port will be configured as 7611 and 7711 respectively. However, since DAS server was started with an offset, those ports will be opened on 7613 and 7713 from the DAS server.





Create a custom Proxy service and edit the proxy service as shown in figure 3.

How to create custom proxy service to access the Redmine REST API through Redmine Connector ?? Click Here

                                          Figure 3


Edit the already created proxy service in design view add a Publish Event mediator to the proxy service as shown in figure 5 .
Click on design view of the proxy service.

Figure 4



Figure 5



Enter the Stream definition to the publish event mediator as shown in figure 6.
Stream Name :  Name of the data stream.
Stream Version : Version of the data stream.
Event Sink : Select a proper event sink from the drop down list.

Add the correlated attributes of the data stream. Here I added two attributes called id and name. For each attribute you have to select the attribute value from the drop down list.
If you define an attribute for a static value you need to select the value option and if you define an attribute for a dynamic value you need to select expression option from the drop down list.
For the Value/Expression field you need to give the Xpath if the attribute is dynamic other wise you need to add a static value if the attribute is static. Then Select the type of the attribute.
Now save the all settings of the proxy service and go to the proxy service list. Then click on Try this service as shown in the figure 7.


Figure 6


Figure 7

  
Now login to the DAS and go to the main tag and select streams . Now you should be able to see the data stream which was published from the ESB in the stream list. Now you need  to persist this data stream . To do that click on edit . Click on  Next[Persist Event] button at the bottom of the Edit Event Stream Console  as shown in the figure 9.

Figure 8
                                                                         


Figure 9 


Then put a tick to the Persist Event Stream option and to the Persist Attribute option as highlighted in figure 10 and then click on Save Event Stream .

                                                               Figure 10                                                                     
Now you need to add an event receiver to listen  this data stream. To add an event receiver select Main tag and click on Receivers then click on Add Event Receiver. Then you have to enter the details of the event receiver as shown in figure 12.



Figure 11

  Enter a proper name for the event receiver .Select wso2event form the drop down list for the input event adapter type. Select the data stream name form the drop down list for the event stream . Select wso2event as the message format. Then click on Add Event receiver.



Figure 12
Now you are done with persisting the data stream definition and adding the listener to  the data stream. Again you need to run the ESB proxy service to publish the data.
Now select the Main tag and click on Data Explorer. Then select a table from the drop down list and   click on Search. Then you should be able to view the data published from the ESB as shown in figure 14.

Figure 13

                                                          

Figure 14


How Store the published data in a MySQL database?


 Create a MySQL database.




Create a table in the database.




Download MySQL JDBC connector .jar  and put it in <wso2 DAS Server>/repository/components/lib .
Create a data source in DAS. Select configure tag and click on Datasources and then click on Add Datasource as shown in figure 15.


Figure 15
   

Figure 16
                                

·         Datasource type : RDBMS
·         Name : Proper Name
·         Driver : com.mysql.Driver
·         URL : jdbc:mysql://localhost:3306/<database name>
·         User Name : User Name of the MySQL
·         Password : password of MySQL



Then click on Test Connection and then you will receive a success message as shown in figure 17.

Figure 17

  Now select Main tag and click on Scripts . Then you will be able to see the Available Analytics Scripts console as shown in figure 18. Click on Add New Analytics Script to add a new script.

Figure 18

Now you will receive the Add new Analytics Script  console . Enter a proper name for the Script name. Enter the Spark SQL Queries in the text editor .
Here is an example format for the Spark SQL query.

create temporary table xxx using CarbonAnalytics options (tableName "<data stream name>", schema  "attribute data type");
select * from xxx limit 20;
create temporary table yyy using CarbonJDBC options (datasource "<data source name>", tableName "<table name >");
insert into table yyy select * from xxx       

Figure 19
Then click on Execute Script.  Then you can see the data table under the Execution Results as shown in figure 20 .

Figure 20

 Now go to command prompt and enter the following SQL query.
Select * from newtable;
Then you should be able to see the data which were published from ESB stored in the MySQL table as shown in figure 21.










                   Figure 21