Tuesday, October 26, 2010

Get map for latitude & longitude using Google Maps API

Google provides a FREE Java Script map API for longitude & latitude. Simple demonstration is given below. Position marker, mouse wheel zooming, Map view/Satelite view/Hybrid view controls are enabled in the example.

NOTE :- Map searching is commented.

<html>
<head>
<script type="text/javascript"
src="http://www.google.com/jsapi?key=ABCDEFG"></script>
<script type="text/javascript">
google.load("maps", "2");
// google.load("search", "1");
// Call this function when the page has been loaded

function initialize() {
var map = new google.maps.Map2(document.getElementById("map"));
map.setCenter(new google.maps.LatLng(7, 80), 13);
map.enableRotation();
map.addControl(new GOverviewMapControl());
map.enableDoubleClickZoom();
map.enableScrollWheelZoom();
map.addControl(new GMapTypeControl());

map.addControl(new GSmallMapControl());

var marker = new GMarker(new GLatLng(7, 80));
map.addOverlay(marker);
map.setMapType(G_SATELLITE_MAP);

// var searchControl = new google.search.SearchControl();
// searchControl.addSearcher(new google.search.WebSearch());
// searchControl.addSearcher(new google.search.NewsSearch());
// searchControl.draw(document.getElementById("searchcontrol"));
}

google.setOnLoadCallback(initialize);
</script>
</head>
<body>
<div id="map" style="width: 500px; height: 500px"></div>
<!-- <div id="searchcontrol"></div> -->
</body>
</html>

Reference :- http://code.google.com/apis/ajax/documentation/ 

Tuesday, October 12, 2010

Shell Script to load data from a TableA to TableB in mysql

Suppose you have to select custom fields in TableA and insert those fields into TableB in mysql, then the following shell script command would be appropriate.

mysql -u user -p -D database -e 'insert into TableB select id, addr from TableA;'

Insert above code in a file and save it as ".sh" file extension. Then run it as "./Test.sh".

NOTE :- Test.sh must be stored in the server that mysql db server is running.

Thursday, October 7, 2010

Remove non empty directory in linux

Use the below command to remove non empty directory in Linux without prompting for verification "yes" or "no".

rm -rf directory

This will remove all the contents in the directory without asking further verifications for files deletion.

Monday, August 30, 2010

File download using Struts

In some developments, you have to prompt "OPEN/SAVE" dialog for file viewing.
Here is a sample code for file download with the support of the "DownloadAction".

import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;


public class ExampleByteArrayDownload extends DownloadAction {

    protected StreamInfo getStreamInfo(ActionMapping mapping, 
                                       ActionForm form,
                                       HttpServletRequest request, 
                                       HttpServletResponse response)
            throws Exception {
   
        String fileName = httpServletRequest.getParameter("fileName");

        String fileExtension =
            fileName.substring(fileName.lastIndexOf("."), fileName.length());

        httpServletResponse.setHeader("Content-Disposition",
                                      "attachment;filename=\"" + fileName +
                                      "\"");
        
        // Download a "pdf" file
        String contentType = "application/pdf";
        byte[] myPdfBytes  = null;              // Get the bytes from somewhere

        return new ByteArrayStreamInfo(contentType, myPdfBytes);
        
    }

    protected class ByteArrayStreamInfo implements StreamInfo {
        
        protected String contentType;
        protected byte[] bytes;
        
        public ByteArrayStreamInfo(String contentType, byte[] bytes) {
            this.contentType = contentType;
            this.bytes = bytes;
        }
        
        public String getContentType() {
            return contentType;
        }

        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(bytes);
        }
    }
}

Reference
http://wiki.apache.org/struts/StrutsFileDownload

Wednesday, July 28, 2010

Replace String pattern in "Vi"

1) Open the TEXT file using "vi" editor in linux.
2) Press ESC
3) Following command will replace "tab" character by "|".

    :%s/    /|/g

4) Save & quit file using following command.

    :wq!

Monday, July 19, 2010

Java property file usage

Use the below code to read the java property file and retrieve the property value.
 
      Properties properties = new Properties();
        FileInputStream in;
        try {
            in = new FileInputStream("app.properties");
            properties.load(in);
            in.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return properties.getProperty("appcode");

Sample "app.properties" file is shown below.

appcode=50
email=admin@yahoo.com

Tuesday, July 6, 2010

From date < To date validate using js

Use the below js code to validate date "From date < To date"

         var frmDate = document.getElementById("frmDate").value;
         var toDate = document.getElementById("toDate").value;

         if (frmDate < toDate) {
               alert("Invalid Date Range!\nStart Date cannot be after End Date!")
               return false;
           }