Monday, July 24, 2017

MySQL BIT field values are not being displayed properly when using SELECT command


Suppose if we have created a mysql table with bit type, while using SELECT sql command, the results returned will not be readable and visible.

Therefore by using the cast function, we can easily fix this

SELECT id, CAST(enabled AS UNSIGNED) AS enabled FROM my_table

Sunday, July 23, 2017

How to add local dependencies Jar on maven





${project.basedir} points to the POM location.

custom jar file is placed on following location.

/libs/com.wso2.enterprise/enterprise-1.0.0.jar

This location can be defined under dependency path location.


<dependency>
    <groupId>com.wso2.enterprise</groupId>
    <artifactId>enterprise</artifactId>
    <version>1.0.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/libs/com.wso2.enterprise/enterprise-1.0.0.jar</systemPath>
</dependency>

Monday, July 17, 2017

Java Regular Expression

Regex defines the the search pattern of strings in Java.

Regex consists of set of symbols. Following are the common matching symbols used for regex purpose.



Symbol
Description
.
Any character
^      $
Regex matches beginning of the line and end of the line
[abc][pq]
Matches a/b/c followed by p/q
[^abc]
Negates the matching pattern
[p-z4-9]
Matches characters between p and z or 4 to 9.
p|q
Equivalent to [pq]
pq
q immediately follows p


These matching symbols are further defined by quantifiers which will be defining number of occurrences of regex pattern.


Regex
Description
{p,q}
Occurs between p to q times
{p}
Occurs p times
*
{0,}
+
{1,}
?
{0,1}


There are certain meta characters that can be used to simplify defining symbols.


Symbol
Description
\s
Equivalent of  [ \t\n\x0b\r\f]
\S
[ ^\t\n\x0b\r\f]
\w
[A-Za-Z0-9_]
\W
[^\w]
\d
[0-9]
\D
[^0-9]
\b
\w

Note: Grouping of regex elements can be done using ( ) operators.
          p(?!q) denotes regex matches iff p is not followed by q.

Eventually we can consider following example, where I wanted to identify following string

{
               "@nil":"true"
            }


and replace this string with empty("") String.

Input String;

{
   "Opportunities":{
      "Opportunity":[
         {
            "Name":"Firzhan",
            "Age":"30 years",
            "EPF_Start_Date__c":{
               "@nil":"true"
            },
            "EPF-Id":"006E000000",
            "EPF_End_Date__c":{
               "@nil":"true"
            },
            "EPF_Closed_Date__c":{
               "@nil":"true"
            },
             
"CreatedDate":"2016-12-12+05:30",
            "Location":"Dehiwala"
         },
         {
            "Name":"Firzhan2",
            "Age":"31 years",
             "Puppies":{
               "@nil":"true"
            },

            "EPF-Id":"006E000000",
        
            "EPF_Start_Date__c":"2017-03-25+05:30",
            "EPF_End_Date__c":"2017-03-02T00:00:00.000+05:30",
            "EPF_Closed_Date__c":"2017-07-17T12:26:46.000+05:30",
            "CreatedDate":"2013-12-12+05:30",
             "Location":"Dehiwala"
         }
      ]
   }
}


Pattern pattern = Pattern.compile(Utils.JSON_REGEX_CONSTANT);
//Matcher matcher = pattern.matcher(opportunityJson.replaceAll("^\\s|\n\\s|\\s$", "''"));Matcher matcher = pattern.matcher(jsonResponse);
jsonResponse = matcher.replaceAll("\"\"");

Here JSON_REGEX_CONSTANT is equal to 

([\{]{1}[\s|\n]*(\"@nil\"){1}(\s*|\n*[:]+\s*|\n*)(\"true\"){1}[\s|\n]*[\}]{1})

Posting JSON payload via CURL

curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:8080/StockQuote -k -v