.:: CODE SNIPPET ::.

"Your time is limited, so don't waste it living someone else's life"

Basically usage of properties file in Java project


Properties file is just a file whose extension is properties in a Java project. It simply contains the pair of key-value string separated by a equal character. Properties file is used for obtaining configuration information, interface messages,…for usual. In generally, it is used for some circumstances in which we need to change the information easily. What functions does Java suppose us to do with a properties files? They comprise loading all information stored in properties file and listing them, retrieving any key-value pair, updating the value of a pair. These actions are all via a Properties object. The following snippets depict the functions:
Getting a value from a key:
Read more of this post

Advertisements

How to customize GUI base on user role with spring security


To restrict user from seeing one or more parts on any page you can use a function of spring security that check user login ‘s role. Snippet below is an example of the granted authority checking:

<sec:ifAnyGranted roles="ROLE_ADMIN">
    <li><g:link class="create" action="create">
            <g:message code="default.new.label" args="[entityName]" />
        </g:link></li>
</sec:ifAnyGranted>

The code restrict user from access a button with user role to only ADMIN. If we pre-define the role hierarchy of roles in system, we just only specify one role, otherwise you have to declare all the roles that can see the inside elements.

How to create a tree main menu in grails


Build the GSP page contain the menu with one div tag named mainMenu

<body>
    <div id="spsLogo" role="banner">
        <a href="http://tranhoangminh.vn"><asset:image src="sps_logo.png" alt="Code Snippet" /></a>
    </div>
    <sec:ifLoggedIn>
        <div class="mainMenu">
            <ul>
                <li><a href="${createLink(uri: '/')}" style="text-decoration: none;"> Home</a></li>
                <li><a href="#">Resource </a>
                    <ul>
                        <li><a href="${createLink(action:'index', controller:'customer')}" style="text-decoration: none">Customer</a></li>
                        <li><a href="${createLink(action:'index', controller:'project')}" style="text-decoration: none">Project</a></li>
                        <li><a href="${createLink(action:'index', controller:'document')}" style="text-decoration: none">Document</a></li>
                        <li><a href="${createLink(action:'index', controller:'contact')}" style="text-decoration: none">Contact</a></li>
                    </ul></li>
                <li><a href="${createLink(action:'index', controller:'Search')}">Search</a></li>
                <li><a href="#">Manager</a>
                    <ul>
                        <li><a href="${createLink(action:'viewUser', controller:'User')}" style="text-decoration: none">Add User </a></li>
                        <li><a href="${createLink(action:'viewRole', controller:'User')}" style="text-decoration: none">Manager Role </a></li>
                    </ul></li>
            </ul>
        </div>
    </sec:ifLoggedIn>
    <g:layoutBody />

    <div class="footer" role="contentinfo">
        <small>&copy; Copyright 2014 CRM | Powered by &nbsp;<a>SPS VietNam</a></small>
    </div>
    <div id="spinner" class="spinner" style="display: none;">
        <g:message code="spinner.alt" default="Loading&hellip;" />
    </div>
</body>

Read more of this post

Configure spring security with ldap authentication and local system roles in grails


Add plugins Add plugins of Spring security and ldap in BuildConfig

plugins {
        compile ":spring-security-core:2.0-RC4"
        compile ":spring-security-ldap:2.0-RC2"
    }

Configure spring security
Configure spring security and LDAP authentication in Config
Read more of this post

Using factory pattern base on class name


Factory pattern helps you to centralize your object construction. Hence, you have an easy way to create object while developing. Otherwise, it also create a good infrastructure design for maintenance. I mentioned the simple way to create a factory design pattern before, so this is another way to use factory pattern. It maybe more automatic on creating multiple objects.

The mechanism of this method is the at one url location, the factory will recursively search for all class file with any predefined name at all the class paths of the sub-packages. Then it will create a new instance of each found java class file. So, prerequisites for this factory way is the way you name your classes which are required to be constructed.
Read more of this post

Get all sub-packages from a Class


A small code snippet to dictate how to get out all the sub package from a java class.

public static void getSubPackages(
        final List<String> lstSubPackages,
        final Class<?> anyClass) throws NullPointerException
    {
        URL resource = anyClass.getResource("");
        if (resource != null)
        {
            File processDir = new File(resource.getPath());
            File[] lstFiles = processDir.listFiles();
            if (lstFiles != null)
            {
                for (File file : lstFiles)
                {
                    if (file.isDirectory())
                    {
                        lstSubPackages.add(file.getName());
                    }
                }
            }
        }
    }

SWT and background processing thread


Assume that we need to create a simple log-in form in which a user can enter his username and password. Then, our application will use that username and password to process a background process ( e.g: connect to a remote database server ). Now we use SWT widgets for creating a simple user interface comprises the text boxes for input username and password, progress bar displays the percentage of background processing, and button to start processing.
Read more of this post

%d bloggers like this: