domingo, 10 de novembro de 2013

Apache POI - Excel API

1. Introduction


This post only gather references information about Apache POI - Excel API


2. References




quarta-feira, 4 de setembro de 2013

SQLServer Tip's - Procedure, rename table, xml, dynamic SQL


1. Introduction

I'm just gathering references to well done post and documentation about SQLServer.

2. References

2.1. Stored Procedure - with parameters

IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'sp_edi_transf_dummy')
DROP PROCEDURE sp_edi_transf_dummy 
GO

CREATE PROCEDURE sp_edi_transf_dummy
  @pIdLoteEdi int 
AS 
    SET NOCOUNT ON;
    INSERT INTO coc_param_proc_lote_edi ( id_lote_edi, param, id )
    VALUES (@pIdLoteEdi, 'proc_transf_dummy', @pIdLoteEdi);
GO


2.2. Rename Table Column


sp_RENAME 'my_table_name.old_column_name', 'new_column_name' , 'COLUMN'




2.3. Oracle equivalents in SQLServer



2.4. String, Date and Time conversion in SQLServer


2.5. String and Int conversion in SQLServer



2.6. Dynamic SQL




2.7. XML








Java Tip's - JOptionPane, JDBC call to SqlServer Stored Procedure

1. Introduction

I'm just gathering references to well done post and documentation about Java.

2. References

2.1. JOptionPane - Java MessageBox

2.2. JDBC call to Sqlserver Stored Procedure with parameters



    2.3. Reading and Writting Excel files using apache POI



      2.4. JDBC Sqlserver Getting value of last AUTOINC sequence column inserted

        pstmt = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
        int insertCount = pstmt.executeUpdate(); // execute insert statement
        System.out.println("Insert Count:" + insertCount);
        ResultSet resultSet = pstmt.getGeneratedKeys();
        if (resultSet != null && resultSet.next()) {
        logId = resultSet.getLong(1);
        }




        quinta-feira, 1 de agosto de 2013

        Database to Grails Reverse Engineering

        1. Introduction

        This tool DatabaseToGrailsReverseEngineer  (databasetograilsreverseengineer.jar)  was designed to automatizate Grails Class building activity. DatabaseToGrailsReverseEngineer connect to target database ( Oracle, SQL Server, MySQL* and DB2 400* ) using JDBC driver, reads table specifications and generates Grails Class File .groovy.

        * Under development

        2. Basic explanation

        2.1. What I would like when I did this

        • Suppose you would like to build Grails web CRUD applications based on a database table
        • So you think, everyting is defined and declared on database, columns, data-types, nullable coluns, primary key, alternate keys,etc
        • It would be fine if you could extract and build Grails Class Definition, based on database
        • It would be better if  you could ignore any table prefix
        • It would be better if you could automaticaly convert to Grails naming convension, ie "ClassName", "attributeName", etc
        • It would be better if you could automaticaly converte toString() method, always the table has a primary key defined as integer and there is another alternate key on table.

        2.2. What do I need to run DatabaseToGrailsReverseEngineer ?

        • databasetograilsreverseengineer.jar
        • JSAP-2.0a.jar
        • JDBC driver to Oracle, SQL Server, MySQL

        2.3. What are command line parameters?

        C:\> java -cp .;databasetograilsreverseengineer.jar;JSAP-2.0a.jar  br.com.databasetograilsreverseengineer.ClassBuilder -h

        DatabaseToGrailsReverseEngineer.ClassBuilder v1.0.00 allows you to build a Grails Class from a Database Table.
          [-h|--help]
                Print help message

          -d <db-type>
                JDBC database type ['orcl': Oracle, 'sqlserver': Microsoft SQLServer,
                'mysql': MySql, 'db2_400' : DB2/400].

          [-j <jdbc-driver>]
                JDBC driver class name. Defaults:
                - for Oracle   : "oracle.jdbc.OracleDriver'
                - for MySQ     : "com.mysql.jdbc.Driver"
                - for SQLServer: "com.microsoft.sqlserver.jdbc.SQLServerDriver
                - for DB2/400  : "com.ibm.as400.access.AS400JDBCDriver"

          -u <url>
                JDBC url for database connection. Examples:
                - for Oracle   : "jdbc:oracle:thin:127.0.0.1:1521:orcldb '
                - for MySQL    : "jdbc:mysql://127.0.0.1/mydb"
                - for SQLServer: "jdbc:sqlserver://127.0.0.1:1433;databaseName=sqldb;
                - for DB2/400  : ""

          -l <login>
                Database user to connect.

          -x <password>
                Database user password.

          -o <table-owner>
                Table Owner of database Schema.

          -t <table-name>
                Table Name.

          [-i <table-name-prefix-to-ignore>]
                Table Name prefix to igonre. Example: "-t MYOWNER.TAB_CUSTOMER_PERSON -i
                MY_OWNER.TAB_"  generates a class "CustomerPerson"

          [-f <file-name>]
                Force full path and file-name specification. Default is
                <ClassName>.groovy.

          [-p <grails-package-name>]
                Grails Package Name specification. Example:
                br.com.databasetograilsreverseengineer

          [-v <verbose>]
                Verbose level. [0:no-verbose, 1: low, 2:medium; 3:high]. (default: 0)

        Usage: DatabaseToGrailsReverseEngineer.ClassBuilder [-h|--help] -d <db-type> [-j
         <jdbc-driver>] -u <url> -l <login> -x <password> -o <table-owner> -t <table-nam
        e> [-i <table-name-prefix-to-ignore>] [-f <file-name>] [-p <grails-package-name>
        ] [-v <verbose>]

        2.4. Let me see class generated

        class Person {

        Long   id
        String fullName
        Date   birthdayDate
        int    numberOfChildren
        Double avgAnnualIncome

        static mapping = {
        table 'tab_person'
        columns{
        id               column: 'person_id'
        fullName         column: 'full_name'
        birthdayDate     column: 'birthday_date'
        numberOfChildren column: 'number_of_children'
        avgAnnualIncome  column: 'avg_annual_income'
        }
        version false
        }

        static constraints = {
        id               ()
        fullName         (blank:false, unique:true, maxSize: 255)
        birthdayDate     (nullable:true)
        numberOfChildren (nullable:true)
        avgAnnualIncome  (nullable:true, scale: 2)

        }

        String toString() {
        return "${fullName}"
        }

        }

        /*

          tab_person
            person_id          int(10) PRIMARY KEY[PK__tab_pers__543848DF628582E0]
            full_name          varchar(255) UNIQUE[ak_tab_person]
            birthday_date      date(0) NOT NULL
            number_of_children smallint(5) NOT NULL
            avg_annual_income  decimal(18,2) NOT NULL

         */

        3. Examples

        3.1. SQLServer database reverse engineering to Grails Class

        Step#1. Supose you have a SQL SERVER table called "tab_person" like this:


        CREATE TABLE [dbo].[tab_person](
         [person_id] [int] IDENTITY(1,1) NOT NULL,
         [full_name] [varchar](255) NOT NULL,
         [birthday_date] [date] NULL,
         [number_of_children] [smallint] NULL,
         [avg_annual_income] [decimal](18, 2) NULL,
         PRIMARY KEY CLUSTERED 
         (
           [person_id]
         )
        )
        GO 

        ALTER TABLE [dbo].[tab_person]
        ADD CONSTRAINT ak_tab_person UNIQUE ( [full_name] ); 
        GO


        Step#2. Supose you've downloaded DatabaseToGrailsReverseEngineer , JSAP-2.0a.jar requried library and SQL Server driver compatible with your server (sqljdbc4.jar

        C:\Users\Josemar Silva\Google Drive\My Projects\database-to-grails-reverse-engin
        eer\dist>dir
         O volume na unidade C é INMETRICS
         O Número de Série do Volume é FC43-483A

         Pasta de C:\Users\Josemar Silva\Google Drive\My Projects\database-to-grails-rev
        erse-engineer\dist

        01/08/2013  11:06    <DIR>          .
        01/08/2013  11:06    <DIR>          ..
        01/08/2013  09:38             9.885 databasetograilsreverseengineer.jar
        22/01/2012  17:10            69.315 JSAP-2.0a.jar
        17/01/2012  12:56           789.885 mysql-connector-java-5.1.18-bin.jar
        13/04/2012  10:39         2.714.189 ojdbc6.jar
        01/08/2013  10:50               827 run_me_by_sample.bat
        24/07/2013  21:39           584.207 sqljdbc4.jar


        Step#3. Now, you user DatabaseToGrailsReverseEngineer to generate Grails Class from SQL SERVER table 'tab_person', ignoring prefix "tab_", 

        C:\> java -cp .;databasetograilsreverseengineer.jar;JSAP-2.0a.jar;sqljdbc4.jar br.com.databasetograilsreverseengineer.ClassBuilder -d sqlserver -u jdbc:sqlserver://127.0.0.1:1433;databaseName=grails -l grails -x grails -o dbo -t tab_person -i tab -v 0

        C:\> dir Person_Sqlserver.groovy
        01/08/2013  11:22               863 Person_Sqlserver.groovy


        Step#4. Observe the result typing Grails class file generated <ClassName>.groovy

        C:\> type Person.groovy

         2.2. Oracle database reverse engineering to Grails Class

        Step#1. Supose you have a ORACLE table called "TAB_PERSON" like this:

        CREATE TABLE TAB_PERSON 
        (
          PERSON_ID NUMBER NOT NULL ENABLE, 
          FULL_NAME VARCHAR2(255) NOT NULL ENABLE, 
          BIRTHDAY_DATE DATE, 
          NUMBER_OF_CHILDREN NUMBER(*,0), 
          AVG_ANNUAL_INCOME NUMBER(18,2), 
          CONSTRAINT PK_PERSON PRIMARY KEY (PERSON_ID)
        ) ;

        ALTER TABLE TAB_PERSON ADD 
        CONSTRAINT AK_PERSON UNIQUE (FULL_NAME) ;


        Step#2. Supose you've downloaded DatabaseToGrailsReverseEngineer , JSAP-2.0a.jar requried library and SQL Server driver compatible with your server (ojdbc6.jar

        C:\Users\Josemar Silva\Google Drive\My Projects\database-to-grails-reverse-engin
        eer\dist>dir
         O volume na unidade C é INMETRICS
         O Número de Série do Volume é FC43-483A

         Pasta de C:\Users\Josemar Silva\Google Drive\My Projects\database-to-grails-rev
        erse-engineer\dist

        01/08/2013  11:06    <DIR>          .
        01/08/2013  11:06    <DIR>          ..
        01/08/2013  09:38             9.885 databasetograilsreverseengineer.jar
        22/01/2012  17:10            69.315 JSAP-2.0a.jar
        17/01/2012  12:56           789.885 mysql-connector-java-5.1.18-bin.jar
        13/04/2012  10:39         2.714.189 ojdbc6.jar
        01/08/2013  10:50               827 run_me_by_sample.bat
        24/07/2013  21:39           584.207 sqljdbc4.jar


        Step#3. Now, you user DatabaseToGrailsReverseEngineer to generate Grails Class from ORACLE table 'TAB_PERSON', ignoring prefix "TAB_", 

        C:\java -cp .;databasetograilsreverseengineer.jar;JSAP-2.0a.jar;ojdbc6.jar
         br.com.databasetograilsreverseengineer.ClassBuilder -d orcl -u jdbc:oracle:thin
        :@10.10.11.13:1521:PROD -l GRAILS -x GRAILS -o GRAILS -t TAB_PERSON -i TAB_

        C:\> dir Person.groovy
        01/08/2013  11:33               716 Person.groovy




        Step#4. Observe the result typing Grails class file generated <ClassName>.groovy

        C:\> type Person.groovy


         3. Downloads

        • DatabaseToGrailsReverseEngineer  is available for download here!
        • (SQLSERVER  || ORACLE || MYSQL || DB2-400 ) JDBC drivers, strongly recommended you download from factory.

        sexta-feira, 4 de janeiro de 2013

        RedMine Customization: Service Catalog, SLA's, KPI, Description Templates and Workflow Coordinator

        1. Introduction

        If you use or you know or you intend to use RedMine project to control Issues of your projects like a Service Desk Tool, you could be interested in this customization. The Issue functionality from Redmine was originally built to serve as a management tool for development bugs. I had a need for a tool to manage Service Desk, Incident, Problems and Change Management, something in line with ITIL. Before deciding the use of another tool, we decided to analyze the software architecture. So, we concluded that it could be easily customized to meet our needs. Now, I am sharing with you.

        2.  RedMine Customization Functional Requirement

        2.1. Functional Requirement - Service Catalog

        a) RedMine Issues category are now based on Services Catalogs. A Service Catalog of a project determine aspects like: 
        • Description: Offers a brief description of the service in customer (non-technical) terms
        • Standard Features: Describes features and functions of the service available to anyABC employee who receives the service . these are providedunder the ABC Base Service Level Agreement
        • Optional Features: Describes features and functions of the service only available to ABC employees and hierarchies upon special request . these are only provided under ABC Extended Service Level Agreements
        • Delivery Scope: Identifies which ABC hierarchies and business units are eligible to receive the service
        • Delivery Channels: Identifies which delivery channels the service may be receivedwith . examples might be:  a) Laptops; b) Workstations; c) Intranet; d) Mobile phone, etc.
        • Service Hours: Identifies timeframes and operating hours for which ABC employees can use the service
        • User Requirements: Indicates pre-requisites that a ABC employee should have inorder to successfully receive the service
        • Service Initiation: Identifies where ABC employees can go to obtain the service
        • Service Support: Identifies where ABC employees can go to receive help in theevent that problems occur with the service
        • Standard Costs: Indicates any ABC employee, division or business unit costsemployees with the Standard Features and Optional Features provided with the service
        • Service Targets: Describes expectations for delivery of the service in customer(non-technical) terms
        • Issue Description Template: User end best practice template of Issue Description
        • FAQ: Frequently Asked Questions
        • TechInfo: Techinical Information
        Example: Issue Categry entity now export foreign key to Service Catalog entity

        b) All Service Catalog information must be published and available for USER TEAM and SUPPORT TEAM everytime.
        Example: Project Wiki Home Page introduces user to a page where Service Catalog is detailed.

        c) Constraints of Required Information can be specified for field Description of a RedMine Issue.
        Example: Issue Category of "Request for new user creation on system X" is based on a item of Service Catalog that has at least two required informations: a) user-login; b) system X role;

        d) The user's life must be continually kept simple during issue creation
        Example: RedMine Issue automatically retrieves Issue Description Template when user creates an Issue.

        2.2. Functional Requirement - Measurements for SLA's, KPI's and SLO's

        a) RedMine should record all state transitions and support measurements for key performance indicators. 
        Example: We should know how long does a service "User account lock" take?

        b) Elapsed Time Measurements should be calculated in working hours and no-working hours
        Example: Some services are provided for business hours, so calculation should desconsider non-working hours

        c) RedMine should SLA's and SLO's should be monitored

        d) RedMine should classify data in the following dimensions: Time (Opened, Closed, Executed), Service Catalog, Author, Executor, Approver, SLA acomplishment, etc.

        2.3. Functional Request - Workflow Coordinator

        A Workflow Coordinator should, periodically and during status transitions, assess issue as a whole and perform automatic routing actions to improve efficiency to process:
        • Issue created with no category, should be assigned to author user in a status "Waiting for Information"
        • Issue in status "Execution Validation" for more than 7 days, should be updated to new status "Completed without validation" and author user be notified by e-mail;
        • Issue in status "Execution Validation" for less than 7 days, should remember author user by e-mail to change status to completed or rejected;
        • Issue in status "Waiting for Information" for more than 1 day, should remember assigned user by e-mail to complement information;
        • Issue in status "Waiting for Approval" for more than 1 day, should remember approver user by e-mail to approve it;
        • Issue in status "Completed" for more than 1 day, should remember user by e-mail to answer survey;

        3. RedMine Development Specifications

        3.1. RedMine Customization Deployment Diagram


        3.2. RedMine Customization Issue Statuses Configuration


        3.3. RedMine Customization Database Model Deployment



        5. RedMine Customizagion Deployment Guide

        5.1. Download package with "RedMine Customization - Service Catalog, SLA's, KPI, Description Template and WorkFlow Coordinator from my Google Drive

        • Download package scripts from my Google Drive on: 
        https://docs.google.com/folder/d/0Bw01CdQwHqwIeXZOQkJQdUlVNDA/edit

        5.2. Deploying RedMine Customization database schema and objects


        • Pre-Conditions: An existing instance of RedMine is already installed on "redmine_prod" database ( look for a post how to install RedMine )
        • Execute Step-by-Step de following Scripts
        # mysql -u root -pjosemarsilva redmine_prod < deploy_01_dml_redmine_custom.sql
        # mysql -u root -pjosemarsilva redmine_prod < deploy_02_ddl_redmine_custom_fields.sql
        # mysql -u root -pjosemarsilva redmine_prod < deploy_03_ddl_redmine_custom_ddl-tables_issue_service_catalog_sla_kpi.sql
        # mysql -u root -pjosemarsilva redmine_prod < deploy_04_dml_redmine_custom_dml-inserts_issue_service_catalog_sla_kpi.sql
        # mysql -u root -pjosemarsilva redmine_prod < deploy_05_ddl_redmine_custom_ddl-trigger_tr_b_i_er_issues.sql
        # mysql -u root -pjosemarsilva redmine_prod < deploy_06_ddl_redmine_custom_ddl-trigger_tr_b_u_er_issues.sql
        # mysql -u root -pjosemarsilva redmine_prod < deploy_07_ddl_redmine_custom_ddl-procedures_issue_service_catalog_sla_kpi.sql
        # mysql -u root -pjosemarsilva redmine_prod < deploy_08_ddl_redmine_custom_ddl_view_service_catalog_kpi.sql
        # mysql -u root -pjosemarsilva redmine_prod < deploy_09_ddl_redmine_custom-functions_issue_service_catalog_sla.sql
        #

        5.3. Deploying RedMine Worflow Coordinator

        • Copy RedMine Workflow Coordinator scripts to appropriated path
        root@SRV0004:~# ls  /root/python/redmine*
        /root/python/redmine_wfc_issue_alerts.log
        /root/python/redmine_wfc_issue_alerts.py
        /root/python/redmine_wfc_issue_alerts.sh

        • Add RedMine Workflow Coordinator to Crontab

        # crontab -e
             :
        #+------------------ 1: min(0-59)
        #| +--------------- 2: hour(0-23)
        #| | +------------ 3: day_of_month(1-31)
        #| | | +--------- 4: month_of_year(1-12)
        #| | | | +------ 5: day_of_week(0-6)(Sun-Sat)
        #| | | | | +--- 6: command-line
        #| | | | | |
        #v v v v v v
        30 23 * * 1-5 /root/python/redmine_wfc_issue_alerts.sh > /root/python/redmine_wfc_issue_alerts.log 2>&1
            :


        5.4. Configure a Project to use RedMine Customization Service Catalog, SLA's, Description Template and Workflow Coordinator 


        • Use the Excel Spreadsheet "RedMine_Service_Catalog_Configuration.xls" to configure off-line and generate .SQL scripts of your project
        • Configure Project List for customization updating key issue_transitions_project_list from table settings
        mysql> select id, name, description from projects where name = 'TI';
        +----+------+-----------------------+
        | id | name | description           |
        +----+------+-----------------------+
        |  1 | TI   | IT Support & HelpDesk |
        +----+------+-----------------------+
        1 row in set (0.00 sec)

        mysql> select * from settings where name = 'issue_transition_projects_list';
        +-----+--------------------------------+------------+---------------------+
        | id  | name                           | value      | updated_on          |
        +-----+--------------------------------+------------+---------------------+
        | 108 | issue_transition_projects_list | 1,32,33,34 | 2012-11-28 20:06:15 |
        +-----+--------------------------------+------------+---------------------+
        1 row in set (0.00 sec)

         6. Glossary

        • Service Catalog: List all detailed information from all services provided;
        • Workflow Coordinator: Software component responsible to assess issue, analyze and perform routing actions;