DHIS2-import database to an existing running instance

To import an existing database from your localhost to your deployment server, follow steps below please:

  1. export your database at localhost
  2. scp database, war file (if any) to your server
  3. in remote server, run following commands:
$ sudo -su postgres psql
# drop database wwf1;
Then create a new database with the same name wwf1
# create database wwf1 owner wwf1;
Then import your db file to the new database created
$ sudo -su postgres psql wwf1 < /root/dbfiles/local_wwf1_20170526.dump
Finally we need to grant all permission to wwf1. 
If we miss this step, we cannot start the instance.

# GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO wwf1;

Next is update owner of hibernate_sequence to wwf1.
To see all sequences owners, run: 
# \d
Update sequence owner with command:
# alter sequence hibernate_sequence owner to wwf1;

Note:

Cursor #: meaning that you are logged in posgres
Curso $: meaning that you are in command shell window, not in Posgres command window


			
By luanvm Posted in DHIS2

Simple commands for working with mongo in EDX

EDX is using mongodb, and mysql. I am using devstack version installed on MacOSX.

I was attempting to connect to mongo db to discover which inside it. I spent a couple days and always got the same issues as below:

vagrant@vagrant:/edx/app/edxapp$ mongo

MongoDB shell version: 2.6.12

connecting to: test

2017-05-23T04:53:57.796+0000 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused

2017-05-23T04:53:57.798+0000 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146

exception: connect failed

I also tried following commands:

$ mongo admin

$ mongo edxapp

$ mongo localhost/edxapp -username edxapp -password edxapp

 

All printed the same error message.

I thought that the mongo db was running because i was doing debug with PyCharm that time. So i was stuck there.

 

Fortunately i found the solution next day when trying to start mongo db first. Then try above commands. and it works fine.

 

So, to start developing EDX, we should check where mongodb is running or not.

 

Check whether mongo is running or not

$ sudo service mongod status

My db is down

● mongod.service - High-performance, schema-free document-oriented database

   Loaded: loaded (/etc/systemd/system/mongod.service; enabled; vendor preset: e

   Active: inactive (dead) since Tue 2017-05-23 04:43:27 UTC; 1min 18s ago

     Docs: https://docs.mongodb.org/manual

  Process: 14428 ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf (co

 Main PID: 14428 (code=exited, status=0/SUCCESS)

May 23 04:31:17 vagrant systemd[1]: Started High-performance, schema-free docume

May 23 04:43:27 vagrant systemd[1]: Stopping High-performance, schema-free docum

May 23 04:43:27 vagrant systemd[1]: Stopped High-performance, schema-free docume

 

Start mongo

$ sudo rm /edx/var/mongo/mongodb/mongod.lock
$ sudo -u mongodb mongod --dbpath /edx/var/mongo/mongodb --repair --repairpath /edx/var/mongo/mongodb
$ sudo systemctl start mongod.service

 

Stop mongo

$ sudo service mongod stop

Update default title in EDX

Are you deploying with the ansible scripts?  If so, add the following to your override vars yaml file and re-run the ansible playbook:
EDXAPP_PLATFORM_NAME: ‘Some other name’
If you’re not using ansible, you should be able to edit your /edx/app/edxapp/lms.env.json and /edx/app/edxapp/cms.env.json and look for the variable: “PLATFORM_NAME”

Django project basics

A simple Django project will have following directory structure:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

These files are:

  • The outer mysite/ root directory is just a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
  • manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py.
  • The inner mysite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. mysite.urls).
  • mysite/__init__.py: An empty file that tells Python that this directory should be considered a Python package. If you’re a Python beginner, read more about packages in the official Python docs.
  • mysite/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
  • mysite/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL dispatcher.
  • mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.

 

Reference URL:

https://docs.djangoproject.com/en/1.11/intro/tutorial01/

 

PHP – get php.ini file in MAC

If you have installed a new PHP version and you are not sure which php.ini is using. You can check with following command:

php --ini

Output in my case:

Configuration File (php.ini) Path: /usr/local/etc/php/7.0

Loaded Configuration File:         /usr/local/etc/php/7.0/php.ini

Scan for additional .ini files in: /usr/local/etc/php/7.0/conf.d

Additional .ini files parsed:      /usr/local/etc/php/7.0/conf.d/ext-opcache.ini
By luanvm Posted in PHP

Write your own java method interception with Proxy

By using java.lang.reflect.Proxy class, you can create your own method interception without depending on any other Frameworks.

  1. CatService:
public interface CatService {
    public void says();
    public void says(String message);
}
  1. Cat
public class Cat implements CatService {
    private String name;

    @Override
    public void says(){
        System.out.println("I am cat");
    }

    @Override
    public void says(String message) {
        System.out.println(message);
    }
}
  1. SampleProxy
package main.java.com.luanvm.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by Admin on 1/18/2017.
 */
public class SampleProxy{
    public class ACLCommonInvocationHandler implements InvocationHandler{
        private Object applicationService; // object need to be wrappered.
        public ACLCommonInvocationHandler(Object applicationService){
            this.applicationService = applicationService;
        }
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            // Do your business here.
            System.out.println("===================== Do you business here");
            boolean isAllowed = true;
            if (isAllowed) {
                Object object = method.invoke(this.applicationService, args);
                System.out.println("=========================== " +  method.getName() + " is complete");
                return null;
            } else {
                return null;
            }
        }
    }


    public Object getWrapperProxy(Object objectService, Class<?> objectInterfaces){
        Object t = Proxy.newProxyInstance(objectService.getClass().getClassLoader()
                                            ,new Class<?>[]{objectInterfaces}
                                            , new ACLCommonInvocationHandler(objectService));
                ; // params: loader, interfaces, InvocationHandler.
        return t;
    }
}
  1. Main
package main.java.com.luanvm.proxy;

/**
 * Created by Admin on 1/19/2017.
 */
public class MainTest {

    public static void main(String[] args){

        Cat cat = new Cat();

        SampleProxy sampleProxy = new SampleProxy();
        Object t= sampleProxy.getWrapperProxy(cat,cat.getClass().getInterfaces()[0]);
        CatService catService = (CatService) t;
        catService.says();
        catService.says("Test AOP with proxy");
        //wrapperCat.says();

    }
}


Output:

===================== Do you business here
I am cat
=========================== says is complete
===================== Do you business here
Test AOP with proxy
=========================== says is complete

By luanvm Posted in Java

run Laravel5 project on local apache2 on Mac

I am new to Laravel. It tooks me few hours to setup environment to run a Laravel5 project.

There are some ways but i just know 2 of them until now ^_^

  1. Using MAMP
  2. Using pre-installed apache2 that come along with the MAC OSX.

Both of them need some configuration to configure virtualhost, document directory, directory permission…

Below are my steps to setup apache2:

Enable PHP5

Firstly, we need to enable php5 by uncommenting 2 following lines in /etc/apache2/httpd.conf:

LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule php5_module libexec/apache2/libphp5.so

 

Install virtualHost.

This will help to create a separate config file for each project.

Open Terminal window, and execute the following command:

$ brew install virtualhost.sh

After virtualHost installed successfully, run the following command to create your new project configuration file. For example synmatic.dev in my case:

$ virtualhost.sh synmatic.dev

Thi will create a new file at

/etc/apache2/virtualhosts/synmatic.dev

And it also updates the /etc/apache2/httpd.conf file to append following at the bottom of file:

Include /private/etc/apache2/virtualhosts

Update your virtualHost

Update to point document directory to your project directory.

Example for my case:

<VirtualHost *:80>
 DocumentRoot "/Users/luan/Sites/synmatic.dev"
 ServerName synmatic.dev
 #ServerAlias your.alias.here

ScriptAlias /cgi-bin "/Users/luan/Sites/synmatic.dev/cgi-bin"

<Directory "/Users/luan/Sites/synmatic.dev">
 Options All
 AllowOverride All
 <IfModule mod_authz_core.c>
 Require all granted
 </IfModule>
 <IfModule !mod_authz_core.c>
 Order allow,deny
 Allow from all
 </IfModule>
 </Directory>

CustomLog "/Users/luan/Sites/synmatic.dev/logs/access_log" combined
 ErrorLog "/Users/luan/Sites/synmatic.dev/logs/error_log"

</VirtualHost>

Finally, we need to grant write permission to following directories inside your Laravel project:

  • your_project/public/storage/cache
  • your_project/public/bootstrap

It is done by following commands in Terminal, change directory to your Laravel project, then:

$ sudo chmod -R 777 public/storage/cache

$sudo chmod -R 777 public/boostrap

 

Regarding MAMP, it should be similar. Actually, it is more easier because it does not require to update httpd.conf file to enable php5.

That’s all.

Unix helpful commands

  1. Download file, directory from remote server to local
scp -r user@server:/server_directory local_directory

-r means recursive. It is applied to directory

For file downloading:

scp  user@server:/server_directory/file_name local_directory
By luanvm Posted in Unix

Java Collection: Best way to remove element?

We should use Iterator to remove element for both List and Set interfaces.

List interface

Assume that you have a list of strings as below [Java, Android, Web, Spring, AngularJS, CSS]

The requirement is to remove all elements that contain ‘a’ character.

Iterator is the best one

LinkedList<String> list = new LinkedList<String>();
list.add("Java");
list.add("Android");
list.add("Web");
list.add("Spring");
list.add("AngularJS");
list.add("CSS");
String luan = list.getLast();
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()){
    String data = iterator.next();
    if(data.toUpperCase().contains("A"))
        iterator.remove();
}

System.out.println(list);

Output: [Web, Spring, CSS]

We may think following ways but please note that they both are not correct

Issue 1

public static void method2_not_correct(){
    LinkedList<String> list = new LinkedList<String>();
    list.add("Java");
    list.add("Android");
    list.add("Web");
    list.add("Spring");
    list.add("AngularJS");
    list.add("CSS");
    for(int i=0; i< list.size(); i++){
        String data = list.get(i);
        if(data.toUpperCase().contains("A"))
            list.remove(data);
    }

    System.out.println(list);
}

Output: [Android, Web, Spring, CSS]

We can see: ‘Android’ is still there instead of being removed from the list.

Why?

We can do a debug to see  the reason. At the first run, ‘Java’ is removed, then the linked list is updated the index: Each element will be left-shifted 1 index.  ‘Android’ is moved to at index 0 then.

In the next iteration, i =1, so it skip Android element. That is the issue.

 

Issue2

We may think about way:

public static void method3_not_correct(){
    LinkedList<String> list = new LinkedList<String>();
    list.add("Java");
    list.add("Android");
    list.add("Web");
    list.add("Spring");
    list.add("AngularJS");
    list.add("CSS");
    for(String data : list){
        if(data.toUpperCase().contains("A"))
            list.remove(data);
    }

    System.out.println(list);
}

Output: Exception in thread “main” java.util.ConcurrentModificationException

We cannot read and remove element  (data object in the example) at the same time.

 

Set interface

Do the same with Iterator object.

ArrayList vs LinkedList

I usually use ArrayList when implement a List interface. Actually, we should use LinkedList in some cases to leverage benefit of each implementation.

So, what are differences between them?

LinkedList and ArrayList are two different implementations of the List interface. LinkedListimplements it with a doubly-linked list. ArrayList implements it with a dynamically re-sizing array.

As with standard linked list and array operations, the various methods will have different algorithmic runtimes.

For LinkedList<E>

  • get(int index) is O(n/4) average
  • add(E element) is O(1)
  • add(int index, E element) is O(n/4) average
    but O(1) when index = 0 <— main benefit of LinkedList<E>
  • remove(int index) is O(n/4) average
  • Iterator.remove() is O(1) <— main benefit of LinkedList<E>
  • ListIterator.add(E element) is O(1) <— main benefit of LinkedList<E>

Note: O(n/4) is average, O(1) best case (e.g. index = 0), O(n/2) worst case (middle of list)

For ArrayList<E>

  • get(int index) is O(1) <— main benefit of ArrayList<E>
  • add(E element) is O(1) amortized, but O(n) worst-case since the array must be resized and copied
  • add(int index, E element) is O(n/2) average
  • remove(int index) is O(n/2) average
  • Iterator.remove() is O(n/2) average
  • ListIterator.add(E element) is O(n/2) average

Note: O(n/2) is average, O(1) best case (end of list), O(n) worst case (start of list)

LinkedList<E> allows for constant-time insertions or removals using iterators, but only sequential access of elements. In other words, you can walk the list forwards or backwards, but finding a position in the list takes time proportional to the size of the list. Javadoc says “operations that index into the list will traverse the list from the beginning or the end, whichever is closer”, so those methods are O(n/4) on average, though O(1) for index = 0.

ArrayList<E>, on the other hand, allow fast random read access, so you can grab any element in constant time. But adding or removing from anywhere but the end requires shifting all the latter elements over, either to make an opening or fill the gap. Also, if you add more elements than the capacity of the underlying array, a new array (1.5 times the size) is allocated, and the old array is copied to the new one, so adding to an ArrayList is O(n) in the worst case but constant on average.

So depending on the operations you intend to do, you should choose the implementations accordingly. Iterating over either kind of List is practically equally cheap. (Iterating over an ArrayList is technically faster, but unless you’re doing something really performance-sensitive, you shouldn’t worry about this — they’re both constants.)

The main benefits of using a LinkedList arise when you re-use existing iterators to insert and remove elements. These operations can then be done in O(1) by changing the list locally only. In an array list, the remainder of the array needs to be moved (i.e. copied). On the other side, seeking in a LinkedList means following the links in O(n), whereas in an ArrayList the desired position can be computed mathematically and accessed in O(1).

Another benefit of using a LinkedList arise when you add or remove from the head of the list, since those operations are O(1), while they are O(n) for ArrayList. Note that ArrayDeque may be a good alternative to LinkedList for adding and removing from the head, but it is not a List.

Also, if you have large lists, keep in mind that memory usage is also different. Each element of a LinkedList has more overhead since pointers to the next and previous elements are also stored. ArrayLists don’t have this overhead. However, ArrayLists take up as much memory as is allocated for the capacity, regardless of whether elements have actually been added.

The default initial capacity of an ArrayList is pretty small (10 from Java 1.4 – 1.8). But since the underlying implementation is an array, the array must be resized if you add a lot of elements. To avoid the high cost of resizing when you know you’re going to add a lot of elements, construct the ArrayList with a higher initial capacity.

It’s worth noting that Vector also implements the List interface and is almost identical toArrayList. The difference is that Vector is synchronized, so it is thread-safe. Because of this, it is also slightly slower than ArrayList. So as far as I understand, most Java programmers avoid Vector in favor of ArrayList since they will probably synchronize explicitly anyway if they care about that.

 

Source: http://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist