Monday, January 19, 2015

Hide placeholder text on focus with CSS


1. Add the below CSS to your page..

.placeholder-fix:focus::-webkit-input-placeholder {color:transparent;} .placeholder-fix:focus::-moz-placeholder {color:transparent;} .placeholder-fix:-moz-placeholder {color:transparent;}

2. add the class "placeholder-fix" to your input or textarea elements.

<textarea rows="4" class="placeholder-fix" name="comments" placeholder='Enter your text here..'></textarea>

Thats all :)

Full reference

Sunday, January 18, 2015

Java-R-Integration with JRI (ARIMA Prediction)

public static double[] getArima(double[] trainingData, int forcastSize){

Rengine rengine = REngineUtil.getRengine();
rengine.eval("library(forecast)");
rengine.assign("value", trainingData);
rengine.eval("traindatats<-ts(value)");
rengine.eval("loopMinLag<-0");
rengine.eval("loopMaxLag<-8");
rengine.eval("best.order <- c(loopMinLag, 1, loopMinLag)");
rengine.eval("best.aic = Inf");
rengine.eval("for(P in loopMinLag:loopMaxLag) { for(Q in loopMinLag:loopMaxLag) { myModel <- arima(traindatats, order=c(P,1,Q), method='ML'); fit.aic <- myModel$aic; if(fit.aic < best.aic) { best.order <- c(P, 1, Q); best.aic <- fit.aic; } } }");
rengine.eval("model <- list()");
rengine.eval("model$P <- best.order[1]");
rengine.eval("model$D <- 1");
rengine.eval("model$Q <- best.order[3]");
rengine.eval("ar<-arima(traindatats,order=c(model$P,model$D,model$Q))");
rengine.eval("f<-predict(ar,n.ahead=12)");

return predictArima(rengine,trainingData, (int)rengine.eval("model$P").asDouble(), (int)rengine.eval("model$D").asDouble(), (int)rengine.eval("model$Q").asDouble(), forcastSize);
}

public static double[] predictArima(Rengine rengine,double[] abcd, int p, int d, int q, int forcastSize){
double forecast[] = new double[forcastSize];
 try {
  rengine.assign("x", abcd);
  rengine.eval("ar<-arima(x=x,order=c(" + p + "," + d + "," + q
    + "))");
  rengine.eval("f<-predict(ar,n.ahead=" + 12+ ")");
  for (int i = 0,j=1; i < forcastSize; i++,j++) {
   forecast[i] = rengine.eval("f$pred[" + j + "]").asDouble();
   System.out.print(" "+forecast[i]);
  }
 } catch (Exception e) {
  e.printStackTrace();
 }
return forecast;
}
}

Thursday, January 15, 2015

Examples where we can use the Actions interface

/**
 *   Examples where we can use the Actions interface
 */


import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class ActionExample {
    private static WebDriver driver;

    @BeforeClass
    public void setUp() {
        driver = new FirefoxDriver();
    }

    @AfterClass
    public void tearDown() {
        driver.close();
        driver.quit();
    }

    @Test
    public void draggable() {
        driver.get("http://jqueryui.com/demos/draggable/");

        WebElement draggable = driver.findElement(By.id("draggable"));
        new Actions(driver).dragAndDropBy(draggable, 120, 120).build().perform();
    }

    @Test
    public void droppable() {
        driver.get("http://jqueryui.com/demos/droppable/");

        WebElement draggable = driver.findElement(By.id("draggable"));
        WebElement droppable = driver.findElement(By.id("droppable"));
        new Actions(driver).dragAndDrop(draggable, droppable).build().perform();
    }

    @Test
    public void selectMultiple() throws InterruptedException {
        driver.get("http://jqueryui.com/demos/selectable/");

        List<WebElement> listItems = driver.findElements(By.cssSelector("ol#selectable *"));

        Actions builder = new Actions(driver);
        builder.clickAndHold(listItems.get(1)).clickAndHold(listItems.get(2)).click();

        Action selectMultiple = builder.build();
        selectMultiple.perform();
    }

    @Test
    public void sliding() {
        driver.get("http://jqueryui.com/demos/slider/");

        WebElement draggable = driver.findElement(By.className("ui-slider-handle"));
        new Actions(driver).dragAndDropBy(draggable, 120, 0).build().perform();
    }
}

Converting a Java array of strings to Java Script array in JSP


Firstly, Place the below scriptlet anywhere in your JSP

<%!
  public String toJavascriptArray(String[] arr){
   StringBuffer sb = new StringBuffer();
   sb.append("[");
   for(int i=0; i<arr.length; i++){
       sb.append("\"").append(arr[i]).append("\"");
       if(i+1 < arr.length){
           sb.append(",");
       }
   }
   sb.append("]");
   return sb.toString();
}
 %>
and finally,  pass java array object to toJavascriptArray() functon to get Java Script array.

var myArray = <%= toJavascriptArray(javaArray) %>;

That's all :)

Downloading PDFs in Java

Document document = new Document();
StringBuffer sb = new StringBuffer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try{
  PdfWriter.getInstance(document, baos);
}
catch (DocumentException e){
      e.printStackTrace();
}
document.open();

htmlWorker.parse(new StringReader(sb.toString()));
document.close();
res.setContentType("application/pdf");
res.addProperty(HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");
res.setContentLength(baos.size());
OutputStream out = (OutputStream) resourseRepsonse.getPortletOutputStream();
baos.writeTo(out);
out.flush();
out.close();

Navigate (Back and Forward) in browser using Selenium WebDriver



/**
 *   Program to navigate(Back and Forward) in browser
 */
package com.seleniumExamples;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class browserBackAndForwardNavigation {
       public static void main(String[] args) throws InterruptedException {

              WebDriver driver = new FirefoxDriver();
              driver.get("https://www.google.co.in/?gfe_rd=cr&ei=IA2cU83dEKKl8wehm4GgBA&gws_rd=ssl");
              driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
              driver.findElement(By.xpath("//a[text()='Gmail']")).click();
              Thread.sleep(3000);
              driver.navigate().back();
              Thread.sleep(3000);
              driver.navigate().forward();
              Thread.sleep(3000);
              driver.close();
              driver.quit();
       }
}

How to get document media file size and file type in Liferay 6.2 Velocity Template?

#set($url = $httpUtil.decodeURL($cur_file4.getData(), true))
#set($parts = $stringUtil.split($url, "/"))
#set($group_id = $getterUtil.getLong($parts.get(2)))
#set($folder_id = $getterUtil.getLong($parts.get(3)))
#set($doc_name = $parts.get(4))
#set($type = $stringUtil.split($doc_name, "."))
#set($docService = $serviceLocator.findService("com.liferay.portlet.documentlibrary.service.DLAppLocalService"))
#set($fileEntry = $docService.getFileEntry($group_id, $folder_id, $doc_name))
#set($size = $fileEntry.size /1024)
$size kb)


Note:

Before using this code you have to insert "velocity.engine.restricted.variables=" this line in "portal-ext.properties