Tuesday, April 4, 2017

how to use const vars in AngularJS

1. define const with .constant

 angular.module('myapp').constant("CONST", {
     "URL" : "www.google.com",
     "WELCOME_MSG": "Hello world",
     "PI" : 3.1415926
  }); 

2. Reference consts with:
angular.module('myapp').controller('myCtrl', ['$scope', '$rootScope', '$log', 'CONST', function($scope, $rootScope, $log, CONST) {
     $log.info("URL is", CONST.URL);
 }]);

Monday, March 6, 2017

$http to call REST APIs in Angularjs

Examples:
1. GET
            var successCallback = function(response) {
                $scope.kerberos = response.data.Kerberos;
                if ($scope.kerberos == "disabled") {
                    $log.info('kerberos is disabled.');
                } else {
                    $log.info('kerberos is enbaled.');
                }
            };

            var errorCallback = function(response) {
                $scope.error = response.data;
            };

            $http({
                method: 'GET',
                url: '/api/v1.0/configuration/kerberos'
            })
                .then(successCallback, errorCallback);

2. POST
            $scope.addDevice = function($event) {
                if($scope.addDevice.password != $scope.addDevice.confirmPassword) {
                    alert("Passwords Don't Match");
                    return false;
                }

                var successCallback4addDevice = function(response) {
                    $log.info('Device', response.data.id);
                    eventBus.emit('backup:adddevice:off', response);
                };

                var errorCallback4addDevice = function(response) {
                    eventBus.emit('backup:adddevice:off', response);
                };

                var dataobj = {
                        username: $scope.addDevice.user,
                        hostname: $scope.addDevice.server,
                        path: $scope.addDevice.storage
                };
                if ($scope.kerberos == "disabled") {
                    $log.info('create password obj');
                    dataobj.password = $scope.addDevice.password;
                } else {
                    $log.info('create kerberoscc obj');
                    dataobj.kerberoscc = $scope.addDevice.kerberoscc;
                }

                eventBus.emit('backup:adddevice:on');
                $http({
                    method: 'POST',
                    url: '/api/v1.0/configuration/backup-devices',
                    data: dataobj
                })
                    .then(successCallback4addDevice, errorCallback4addDevice);

            };

Friday, February 24, 2017

angularjs - access value of ng-model in a controller

To access the value of ng-model in a controller, you have to have a dot in there.
With a format like object.property you'll be good to go.

In my case "add.device.storage" does not work. And  "addDevice.storage" is good.

In view(.html):

<input type="text" id="input-id-dd-password" ng-model="addDevice.password"
    placeholder="type here">

In controller(.js): access it with $scope.addDevice.password

Thursday, February 16, 2017

java cmd cheatsheet

1. List files in a jar package:
 $ jar tvf my.jar

2. Extract files out of a jar package:
 $ jar xvf my.jar

3. Package current directory with Manifest file:
 $ jar cvfm my.jar META-INF/MANIFEST.MF ./

 4. javap is like nm in *NIX

5. jps like ps in *NIX
    jps -lvm

6. Print flags of a JVM process
    jinfo -flags

7. jconsole, other than jps/jinfo commands you can use jconsole to monitor a JVM process. It provides lots of information you need to tune your process.

8. jvisualvm, similar as jconsole

9. jstack, print process stack information

Wednesday, February 15, 2017

Adding getter/setter for Java class members

"Source" -> "Generate Getters and Setters".

Wednesday, January 25, 2017

boost ptree

This is an awesome post on how to use ptree for json processing. http://zenol.fr/blog/boost-property-tree/en.html

Thursday, January 12, 2017

JSON in Java

Add to your Maven dependency
1
2
3
4
5
<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.json</artifactId>
    <version>1.0.4</version>
</dependency>
Demo:
1
2
3
4
5
6
7
String json = "{\"id\":3}";
JsonReader reader = Json.createReader(new StringReader(json));
JsonObject rootObject = reader.readObject();
reader.close();

int devID = rootObject.getInt("id");
String sDevID = Integer.toString(devID);