Monday, October 9, 2017

Upload excel file(template) using angular JS Ajax call and Read its contents in Java controller

In your HTML file create a file input and button

<input name="fileupload" type="file" file-model="fileupload">
<button ng-click="uploadFile()" class="btn btn-primary">Upload File</button>

In your script file controller function:

myApp.directive('fileModel', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            var model = $parse(attrs.fileModel);
            var modelSetter = model.assign;
           
            element.bind('change', function(){
                scope.$apply(function(){
                    modelSetter(scope, element[0].files[0]);
                });
            });
        }
    };
}]);


myApp.service('fileUpload', ['$http', function ($http) {
    this.uploadFileToUrl = function(file, uploadUrl){
        var fd = new FormData();
        fd.append('file', file);
        return $http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        })
    }
}]);

myApp.controller('fileController',function($scope,$http,fileUpload){
$scope.uploadFile = function(){
            var file = $scope.fileupload;
         
            var uploadUrl = "../uploadFile";
            fileUpload.uploadFileToUrl(file, uploadUrl)
            .then(function(response) {
                console.log(response) ;
                $scope.fileData= response.data;
                })
           
         };
)};
        
 IN java controller

@RequestMapping(value="uploadFile", method = RequestMethod.POST)
    public @ResponseBody String uploadFile(@RequestParam(value="file", required=true) MultipartFile file,HttpServletRequest request) throws IOException, ParseException {
       
        String fileName=file.getOriginalFilename();
        final ServletContext servletContext = request.getSession().getServletContext();
        final File tempDirectory = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
        final String saveDirectory = tempDirectory.getAbsolutePath();
              
        int i = 1;
        // Creates a workbook object from the uploaded excelfile
        XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
        // Creates a worksheet object representing the first sheet
        XSSFSheet worksheet = workbook.getSheetAt(0);
        // Reads the data in excel file until last row is encountered
        while (i <= worksheet.getLastRowNum()) {
            // Creates an object for the UserInfo Model
           
            // Creates an object representing a single row in excel
            XSSFRow row = worksheet.getRow(i++);
            System.out.println("File: " + row.getCell(7).getDateCellValue() + row.getCell(8).getDateCellValue()
              + " ----!");
                       
           
           
       
                String dateStringInUse = row.getCell(7).getDateCellValue().toString();
               
                SimpleDateFormat parseFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
                Date dateInUse = parseFormat.parse(dateStringInUse);

                SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
                String useDate = format.format(dateInUse);
 //assign value to a list model
              list.add(Model); 
          }

        String jsonStr=null;
        Gson gson = new Gson();
        jsonStr = gson.toJson(list);
        System.out.println(jsonStr);
        return jsonStr;
       
    }





Monday, August 28, 2017

Display Line number in Text Editor - Eclispe

By default, Eclipse's editor will not display line numbers, it's no good for debugging. Here's a tip to show you how to turn on the “display line numbers” feature in Eclipse. In Eclipse IDE, select “Windows” > “Preference” > “General” > “Editors” > “Text Editors” , check on the “Show line numbers

Wednesday, August 2, 2017

Encoding and decoding in Javascript using base64


var testString = "My testing Application";

// Encode the String
    var encodedString = btoa(testString);
    console.log(encodedString);


// Decode the String
    var decodedString = atob(encodedString);
    console.log(decodedString);

Friday, July 28, 2017

Currency Filter in Angular JS

Add this directive in your JS

myApp.filter('myCurrency', ['$filter', function($filter) {
        return function(input) {
            input = parseFloat(input);
            input = input.toFixed(input % 1 === 0 ? 0 : 2);
            var value = 0;
            console.log(input);
            if(isNaN(input)){
                value=0;
            }
            else
            {
                value = input.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
            }
            return value;
        };
    }]);

In the html page you can refer as {{list.amount | myCurrency}}

Saturday, June 24, 2017

How to choose Browser outside Eclipse



You'll have to do it with different browsers anyway if you want to test the app on multiple browsers. 

You can also choose which browser is used by going to Window - Preferences - General - Web Browser.

Friday, May 13, 2016

Checkbox checked in comma separated list

var empRole= $.map($(':checkbox[name=roleId\\[\\]]:checked'), function(n, i){
      return n.value;
}).join(',');

Saturday, May 4, 2013

Multiple Left joins example


SELECT *,t2.col_name, t3.col_name
FROM table1 t1
LEFT JOIN
table2 t2
ON t1.col_name=t2.col_name
LEFT JOIN table3 t3
ON t1.col_name=t3.col_name