Friday, June 22, 2012

piwik stats for your website

After setting up piwik on your server, here is some simple code to get your website to send some usage data. In the .jsp load the piwik script:
<script type="text/javascript">
      var pkBaseURL = (("https:" == document.location.protocol) ?"https://[your-server-name]/" : "http://[your-server-name]/");
      document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script>
Then, to tack something in you .javascirpt, the code will look something like this:
var siteId = 1;  // check piwik website settings to get siteId needed for the app
if (typeof pkBaseURL === 'string') {
   try{
 // setup any variables you want to track
 var customVariable1 = 'test';
 // can track up to 5 custom variables
          var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", siteId);
          piwikTracker.setCustomVariable (1, 'customVariable1', customVariable1, 'page');
          piwikTracker.setCustomVariable (2, 'customVariable1', customVariable1, 'page');
          piwikTracker.setCustomVariable (3, 'customVariable1', customVariable1, 'page');
          piwikTracker.trackPageView();
          piwikTracker.enableLinkTracking();
   } catch( err ) {}
}
That's it, pretty simple...

Thursday, June 21, 2012

Image + Text in iText PdfPCell

Recently, while working on the project involving creating PDFs with iText library, I needed to create PDF with cells that have image and text inside of them. After some searching and playing around, the following piece of code solved the problem:
Paragraph paragraph = new Paragraph();
if (image != null) {
      image.scalePercent(90f);
      paragraph.add(new Chunk(image, -1f, 1f));
}
font.setColor(WebColors.getRGBColor(fontColor));
paragraph.add(new Phrase(text, font));
PdfPCell cell = new PdfPCell(paragraph);
cell.setBorder(Rectangle.LEFT);
cell.setBorder(Rectangle.RIGHT);
cell.setBorderColor(WebColors.getRGBColor(BORDER_COLOR));
cell.setBackgroundColor(WebColors.getRGBColor(backgroundColor));
table.addCell(cell);
One thing to notice, initially, I had an issue with image stretching to the height of the cell. The fix for that problem is in RED in the code above.

Wednesday, October 19, 2011

Solving log4j error on the deploy

Recently, I got the following error while deploying my Web-Service .war on Tomcat 6:
log4j:ERROR Could not instantiate class [org.jboss.logging.util.OnlyOnceErrorHandler].
java.lang.ClassNotFoundException: org.jboss.logging.util.OnlyOnceErrorHandler
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.apache.log4j.helpers.Loader.loadClass(Loader.java:160)
at org.apache.log4j.helpers.OptionConverter.instantiateByClassName(OptionConverter.java:309)
at org.apache.log4j.xml.DOMConfigurator.parseErrorHandler(DOMConfigurator.java:227)
at org.apache.log4j.xml.DOMConfigurator.parseAppender(DOMConfigurator.java:193)
at org.apache.log4j.xml.DOMConfigurator.findAppenderByName(DOMConfigurator.java:140)
at org.apache.log4j.xml.DOMConfigurator.findAppenderByReference(DOMConfigurator.java:153)
at org.apache.log4j.xml.DOMConfigurator.parseChildrenOfLoggerElement(DOMConfigurator.java:415)
at org.apache.log4j.xml.DOMConfigurator.parseRoot(DOMConfigurator.java:384)
at org.apache.log4j.xml.DOMConfigurator.parse(DOMConfigurator.java:783)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:666)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:616)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:602)
at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:460)
at org.apache.log4j.LogManager.(LogManager.java:113)
at org.apache.log4j.Logger.getLogger(Logger.java:94)
After little research, I found out that it was caused by the custom appender for log4j I have included in my project:
    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
        <param name="Target" value="System.out"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p [%c{6}] %m%n"/>
        </layout>
    </appender>
The easy solution to the problem was to add the following Maven dependency in my pom.xml:
    <dependency>
        <groupId>org.dbunit</groupId>
        <artifactId>dbunit</artifactId>
        <version>2.4.8</version>
    </dependency>

Tuesday, August 30, 2011

Drop tables in Oracle

Just ran into the situation where I needed to drop all the tables in my test Oracle Database for my user. Of course, one of the ways would be to drop the user all together. But if for whatever reason you don't want to do that (you want to keep your procedures, packages, sequences, triggers, etc. etc.) you can just drop the tables. Run the following command:
select 'drop table '||table_name||' cascade constraints;' from user_tables;
and then execute the results of the query.
EASY!!!!