IT Certificaions

Google Cloud Platform

Tuesday, November 3, 2015

MS SQL Server : How to remove duplicate text in a csv String in table

Here is the function which will return string after removing duplicate text from a csv cell value.

create function dbo.RemoveDuplicateTextInCSVString(@SOURCE_STR varchar(50))
returns varchar(50)
as
begin  
  declare @TEMP_STR varchar(50)
  declare @WORD_STR varchar(50)

  set @TEMP_STR = ','

  while len(@SOURCE_STR) > 0
begin
print '--------------------'
set @WORD_STR = left(@SOURCE_STR, charindex(',', @SOURCE_STR+',')-1)+','
print '@WORD_STR :'+ @WORD_STR
if charindex(','+@WORD_STR, @TEMP_STR) = 0
begin
set @TEMP_STR = @TEMP_STR + @WORD_STR
print '@TEMP_STR : '+@TEMP_STR
end
else
print 'found duplicate, remove this..'

set @SOURCE_STR = stuff(@SOURCE_STR, 1, charindex(',', @SOURCE_STR+','), '')
print '@SOURCE_STR '+@SOURCE_STR
  end
  return @SOURCE_STR
end

Thursday, October 29, 2015

Collection holds value as a refernce in java

Value as a Reference example : Here we are fecthing a list from a collection and updating this which leads to automatic update of Map because numList is directly referring to  testmap.
-----------------------------------


public class ValueAsReference{

enum EvenOdd{
EVEN,ODD
}
public static void main(String ... arg){

Map<String,List<String>> testmap=new HashMap<String, List<String>>();
for(int i=0;i<20;i++){
String key="";
if(i%2 ==0){
key=""+EvenOdd.EVEN;

}else{
key=""+EvenOdd.ODD;
}
List<String> numList=testmap.get(key);

if(numList==null){
numList=new ArrayList<String>();
numList.add("i="+i);
testmap.put(key, numList);
}else{
numList.add("i="+i);
}
}
for(String key : testmap.keySet()){
System.out.println("key : "+key);
System.out.println(testmap.get(key));
}

}
}
--------------------------------------
Output :
key : EVEN
[i=0, i=2, i=4, i=6, i=8, i=10, i=12, i=14, i=16, i=18]
key : ODD
[i=1, i=3, i=5, i=7, i=9, i=11, i=13, i=15, i=17, i=19]


Monday, October 19, 2015

Google App Engine deleted default GCS bucket. Is there a way to restore it?

No, there is no way to restore a deleted bucket or object inside a bucket. DELETE operation comes under strong global consistency. Keep in mind that you can use Google Cloud Storage only after enabling billing for your project. It's not free.

You can go through Cloud storage documentation 

Bucket will be deleted permanently. You can see google documentation. Although a bucket will be deleted only if it is empty, means you have to delete all objects first and then you can delete an empty bucket. Choose bucket carefully as these are unique globally and can not be nested like folders or directories.

Friday, August 28, 2015

Set your application context path in Tomcat 7

Set Context path in Tomcat 7 or more using catalina.properties file,

Step 1)
Add following lines to catalina.properties file under <tomcat-install dir>/conf directory,

#myApp war file version
myApp .war=myAppLive -1.0.0.1.war
myApp .path=/myAppLive

#application deployment location
appBase=webapps

Step 2)
Now update context path in <Host> node in server.xml file in same location,
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="false">
...
....  
 <Context path="${myApp .path}" docBase="${catalina.base}/${appBase}/${myApp .war}"/>
...
....

</Host>

Custom directive with dynamic attribute using angular JS

Custom directive with dynamic attribute using angular JS

Javascript:

var myApp=angular.module('myApp', []);
myApp.controller('myCtrl', ['$scope', function($scope) { 

  $scope.data= [
               { name: 'A', size: '12' },
               { name: 'C', size: '11' },
               { name: 'B', size: '10' }
              ];

}])
.directive('myOrderList', function(){ 


 function link(scope, element, attrs) {
       var myData=scope.data;    
       console.log(attrs);
   
       var ulElem=element.html('<ol>');
       for(var i=0;i<scope.data.length;i++){              
          ulElem.append('<li>'+scope.data[i][attrs.value]+'</li>');
        }    
   
  }

  return {
    link: link
  };

});

HTML CODE:
<div ng-app="myApp">


<div ng-controller="myCtrl">

 <div my-order-list value="size"></div>
 <div my-order-list value="name"></div>

</div>

</div>

Maven build is failing to include struts-config.xml or web.xml file to target../../WEB-INF directory

 Maven build is failing to include struts-config.xml or web.xml file to target../../WEB-INF directory due to ‘WebContent ‘ as maven needs ‘ webapps ‘

Add this plugin to pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<webResources>
<resource>
<directory>${basedir}/WebContent</directory>
</resource>
</webResources>
<warSourceDirectory>WebContent</warSourceDirectory>
<warSourceExcludes>WebContent/WEB-INF/lib/*.jar</warSourceExcludes>
<archiveClasses>false</archiveClasses>
</configuration>
</plugin>

Dynamic response by java and handle using ajax jquery for all content types

Dynamic response by java and handle using ajax jquery for all content types

Jquery ajax call
             var url="trackDynamicResponse.do";
              $.ajax({                           
                       url: url,
                       type: 'GET',
                       data :{
                             responseType : _responseType
                              
                       },
                       success: function(response, status, xhr){
                            
                               var ct = xhr.getResponseHeader("content-type") || "";
                               if (ct.indexOf('html') > -1) {
                                 console.log("html ...");                              
                               }
                               if (ct.indexOf('json') > -1) {
                                  console.log("json ...");                                 
                               }
                               if (ct.indexOf('xml') > -1) {
                                  console.log("xml ...");                               
                                  }
                               console.log(response);
                              $('#responseDiv').html("response :: "+response.toString());
                       },
              error :function( xhr, status, errorThrown ){                            
                alert(" An error occured: " + errorThrown + ", Status : " + status);           
                             console.log(errorThrown);
                             /* if(errorThrown.indexOf("XML")>-1){
                                    $('#responseDiv').html(errorThrown);
                             } */
              }
         });

---------------------------------------------------------------------------
Servlet :

  public String trackDynamicResponse() throws IOException{
 
    String responseType=request.getParameter("responseType");
   
    
    if(responseType !=null && responseType.equalsIgnoreCase("JSON")){
       
    JSONObject json      = new JSONObject();
    JSONArray  addresses = new JSONArray();
    JSONObject address;
   
      int count = 15;
  for (int i=0 ; i<count ; i++)
  {
      address = new JSONObject();
      address.put("CustomerName"     , "Decepticons" + i);
      address.put("AccountId"        , "1999" + i);
      address.put("SiteId"           , "1888" + i);
      address.put("Number"            , "7" + i);
      address.put("Building"          , "StarScream Skyscraper" + i);
      address.put("Street"            , "Devestator Avenue" + i);
      address.put("City"              , "Megatron City" + i);
      address.put("ZipCode"          , "ZZ00 XX1" + i);
      address.put("Country"           , "CyberTron" + i);
      addresses.add(address);
  }
  json.put("Addresses", addresses);
   
   
    response.setContentType("application/json");
    response.getWriter().write(json.toString());
   
    }else if(responseType !=null && responseType.equalsIgnoreCase("XML")){
    response.setContentType("application/xml");
    String xml = "";
             xml = xml + "<response>";
             xml = xml + " This is XML Response";
             xml = xml + "</response>";
    response.getWriter().write(xml);
    }else if(responseType !=null && responseType.equalsIgnoreCase("HTML")){
   
    String html = "<html><body>";
    html = html + "This is <b>HTML</b> Response";          
    html = html + "</response>";
             
    response.setContentType("text/html");
    response.getWriter().write(html);
    }else{
    response.getWriter().write("default text Response type");
    }
   
    return null;

}


Open popup window with post request data in Javascript

 Open popup window with post request data using javascript:
                              
  function openWindowWithPostRequest() {
           var winName='MyWindow';
           var winURL='search.action';
           var windowoption='resizable=yes,height=600,width=800,location=0,menubar=0,scrollbars=1';
           var params = { 'param1' : '1','param2' :'2'};           
           var form = document.createElement("form");
           form.setAttribute("method", "post");
           form.setAttribute("action", winURL);
           form.setAttribute("target",winName); 
           for (var i in params) {
                 if (params.hasOwnProperty(i)) {
                     var input = document.createElement('input');
                     input.type = 'hidden';
                     input.name = i;
                     input.value = params[i];
                     form.appendChild(input);
                 }
           }             
           document.body.appendChild(form);                                   
           window.open('', winName,windowoption);
           form.target = winName;
           form.submit();                  
           document.body.removeChild(form);          
   }