Friday, July 25, 2014

[HTML/CSS/Javascript] Floating header/scrollable body

So you want to build something like this....

Header

Scrollable Body

When content vertically overflows, the expected outcome is this (red is the scrollbar):

Header

Scrollable Body

So in the early versions of Internet Explorer, it expresses evilness like so:

Header

Scrollable Body

So yea. The only way to make IE behave properly is to use conditional CSS properties and native javascript. I would use jQuery because I'm noob, but I got too lazy to find the CDN link.

Here is the code below:

<!DOCTYPE html>
<!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
<!--[if IE 7 ]>    <html class="ie7"> <![endif]-->
<!--[if IE 8 ]>    <html class="ie8"> <![endif]-->
<!--[if (gt IE 8)|!(IE)]><!-->
<html class="">
<!--<![endif]-->

<head>
    <title>Floating Header</title>
    <style>
        html,
        body {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
        }
        #container {
            width: 100%;
            height: 100%;
            position: relative;
        }
        #header {
            width: 100%;
            height: 120px;
            background: blue;
            position: absolute;
            top: 0;
            left: 0
        }
        #content-scroll-container {
            overflow: auto;
            overflow-x: hidden;
            background: black;
            width: 100%;
            height: 100%;
            padding: 0;
        }
        #content {
            margin-top: 120px;
            color: white;
        }
        .tbl-whole-span {
            width: 100%;
            margin: 0;
            padding: 0;
        }
        .tbl-whole-span td {
            background: gray;
        }
    </style>
</head>

<body>

    <div id="container">
        <div id="header"></div>
        <div id="content-scroll-container">
            <div id="content">
                First line of destruction
                <br/>
                <input id="btnAddLine" type="button" value="Add Line" onclick="addLine()" />
                <br/>
                <table class="tbl-whole-span">
                    <tr>
                        <td>
                            Some table contents here.
                            <span style="float:right">Text to the right</span>
                        </td>
                    </tr>
                </table>
                Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>Some content, some content
                <br/>
            </div>
        </div>
    </div>
    <script>
        ;
         // Adjust content width when vert scrollbar appears
        function adjustWidth() {
            var scrollDiv = document.getElementById("content-scroll-container");
            var contentDiv = document.getElementById("content");
            var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
            var newWidth = scrollDiv.offsetWidth - scrollbarWidth;
            var percentageWidth = newWidth * 100.00 / scrollDiv.offsetWidth;
            contentDiv.style.width = percentageWidth + "%";
        }

         // Function to add content. This function relies on calling
         // adjustWidth.
        function addLine() {
            var content = document.getElementById("content");
            content.innerHTML += "Line Added<br />";

            // Detect version of IE here. Modify if needed. 
            var html = document.getElementsByTagName("html")[0];
            if (html.className != "")
                adjustWidth(); // adjust width only if class is IE
        }
    </script>
</body>

</html>

Wednesday, July 16, 2014

[MySQL] Grouping Intervals and counting columns

Say you have table like this...

idcodedateactivity
12002014/06/04
22002014/05/04
35002014/04/04
45042014/05/28

...and now you want to get the past 7 days but count how many times 200 (success code) comes up and how many times a number other than 200 (failure code) comes up. So the idea is simple. Get total of success + failure codes, left outer join for success, left outer join for failure. The filter where the date is more the the current day minus 7 days. At the end, you should return a row that has the day, total count, success count, and failure count. the Got it? Yes you do. You're a trooper. Here is a sample:

select STR_TO_DATE(a.period, '%m-%d-%y') as period, ifnull(a.cnt,0) total, ifnull(b.cnt,0) success, ifnull(c.cnt,0) failure 
from
(
select
date_format(dateactivity , '%m-%d-%y') period,
count(*) cnt
from mytable
where dateactivity >= DATE_ADD(NOW(), INTERVAL -7 DAY)
group by date_format(dateactivity , '%m-%d-%y')
) a 
left outer join
(
select
date_format(dateactivity , '%m-%d-%y') period,
count(*) cnt
from mytable
where code = '200'
and dateactivity >= DATE_ADD(NOW(), INTERVAL -7 DAY)
group by date_format(dateactivity , '%m-%d-%y')
) b on a.period = b.period 
left outer join
(
select
date_format(dateactivity , '%m-%d-%y') period,
count(*) cnt
from mytable
where (code != '200' or code is null)
and dateactivity >= DATE_ADD(NOW(), INTERVAL -7 DAY)
group by date_format(dateactivity , '%m-%d-%y')
) c on a.period = c.period
;

I know that there are probably different ways of doing this. If you know how, please share. Sharing is caring. And I care for all.

Tuesday, July 15, 2014

[Java/Spring/Maven] Making JSTL work!!!!

Here are some settings to check to make sure JSTL works in your project. I'm using JSTL 1.1.2 and

Pom.xml dependencies ( Just dumping all dependencies including non-jstl related ones )

 <dependencies>
   <!-- Spring dependencies -->
 <dependency>
  <groupId>org.springframework</groupId>
 <artifactId>spring-aop</artifactId>
 <version>${spring.maven.artifact.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>${spring.maven.artifact.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${spring.maven.artifact.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>${spring.maven.artifact.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>${spring.maven.artifact.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>${spring.maven.artifact.version}</version> 
    </dependency>       
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.maven.artifact.version}</version>
    </dependency>       
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.maven.artifact.version}</version>
    </dependency>       
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.maven.artifact.version}</version>
    </dependency>      
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.maven.artifact.version}</version>
    </dependency>               
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.maven.artifact.version}</version>
    </dependency> 
   <!-- JSP, Servlet, JSTL dependencies -->
 <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>${version.javax.servlet}</version>
 </dependency>
 <dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>${version.javax.servlet.jsp}</version>
 </dependency>
 
 <dependency>
            <groupId>xmlbeans</groupId>
            <artifactId>xbean</artifactId>
            <version>2.2.0</version>
        </dependency>
 <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
  <version>${version.javax.servlet.jstl}</version>
 </dependency>
 <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
      <scope>compile</scope>
    </dependency>
   <!--  Codehaus Jackson dependencies -->
   <dependency>
        <groupId>org.codehaus.jackson</groupId> 
        <artifactId>jackson-core-asl</artifactId> 
       <version>1.9.1</version>
    </dependency>    
    <dependency>
        <groupId>org.codehaus.jackson</groupId> 
        <artifactId>jackson-jaxrs</artifactId> 
        <version>1.9.1</version>
    </dependency>    
    <dependency>
        <groupId>org.codehaus.jackson</groupId> 
        <artifactId>jackson-mapper-asl</artifactId> 
        <version>1.9.1</version>
    </dependency>    
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-lgpl</artifactId>
        <version>1.9.1</version>
    </dependency>
    <!-- Jettison -->
    <dependency>
        <groupId>org.codehaus.jettison</groupId>
        <artifactId>jettison</artifactId>
        <version>1.3.1</version>
    </dependency>
    <!-- Apache Datasource -->
    <dependency>
  <groupId>commons-dbcp</groupId>
  <artifactId>commons-dbcp</artifactId>
  <version>1.2.2</version>
 </dependency>
    <!--  Oracle Driver -->
     <dependency> 
         <groupId>com.oracle</groupId>
         <artifactId>ojdbc14</artifactId>
         <version>10.2.0.4.0</version>
     </dependency>
    
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>myspringproject</finalName>
  </build>
  <properties>
   <spring.maven.artifact.version>2.5.6</spring.maven.artifact.version>
   <version.javax.servlet>2.4</version.javax.servlet>
   <version.javax.servlet.jstl>1.1.2</version.javax.servlet.jstl>
   <version.javax.servlet.jsp>2.1</version.javax.servlet.jsp>
  </properties>

JSP taglibs/charset (utf-8)


<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %> 
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Postman</title>
</head>
<body>

web.xml
The web-app attribute to configure the version might start giving you an error saying that the Dynamic Web Module cannot convert to <some version>. If you encounter this, right-click your project in the Project Explorer, go to the Project Facets, uncheck the Dynamic Web Module, Hit OK, and Update Maven Project. If you have the code below, it will set it as the assign version I believe.


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

Wednesday, March 19, 2014

[.NET] For reference, StructureMap basic setup

The StructureMap xml should look like this for several mappings:

<?xml version="1.0" encoding="utf-8" ?>
<StructureMap>
  <DefaultInstance
    PluginType="LoginExample.Domain.Interfaces.Repositories.IUserRepository,
        LoginExample.Domain.Interfaces"
    PluggedType="LoginExample.Infrastructure.TestDataAccess.TestUserRepository,
        LoginExample.Infrastructure.TestDataAccess" />
  <DefaultInstance
    PluginType="LoginExample.Domain.Interfaces.Services.ILoginService,
        LoginExample.Domain.Interfaces"
    PluggedType="LoginExample.Services.LoginService,
        LoginExample.Services" />
  <DefaultInstance
    PluginType="LoginExample.Domain.Interfaces.Services.IAuthenticationService, LoginExample.Domain.Interfaces"
    PluggedType="LoginExample.Services.AuthenticationService, LoginExample.Services" />
</StructureMap>

Below are contents of the Bootstrapper. Use the Run method in Global.asax.

// StructureMap ContollerFactory
    public class StructureMapControllerFactory : DefaultControllerFactory
    {
        protected override IController
            GetControllerInstance(RequestContext requestContext,
            Type controllerType)
        {
            try
            {
                if ((requestContext == null) || (controllerType == null))
                    return null;

                return (Controller)ObjectFactory.GetInstance(controllerType);
            }
            catch (StructureMapException)
            {
                System.Diagnostics.Debug.WriteLine(ObjectFactory.WhatDoIHave());
                throw new Exception(ObjectFactory.WhatDoIHave());
            }
        }
    }

    public static class Bootstrapper
    {
        public static void Run()
        {
            ControllerBuilder.Current
                .SetControllerFactory(new StructureMapControllerFactory());

            ObjectFactory.Initialize(x =>
            {
                x.AddConfigurationFromXmlFile("StructureMap.xml");
            });
        }
    }