Monthly Archives: June 2014

Web

OWL Carousel integration with AngularJS

owl
To up to work owl carousel (or other jQuery plugin) with AngularJS you need to perform some actions.
1. Download and include all plugin files:

<html>
<head>
    <link href="css/style.css" rel="stylesheet"/>
    <link href="vendor/owl-carousel/owl.carousel.css" rel="stylesheet"/>
    <link href="vendor/owl-carousel/owl.theme.css" rel="stylesheet"/>

    <script src="vendor/jquery.min.js"></script>
    <script src="vendor/owl-carousel/owl.carousel.js"></script>
</head>
<body>
    ...
</body>
</html>

2. Create directive in AngularJS:

app.directive('wrapOwlcarousel', function () {
    return {
        restrict: 'E',
        link: function (scope, element, attrs) {
            var options = scope.$eval($(element).attr('data-options'));
            $(element).owlCarousel(options);
        }
    };
});

3. Place new tag in your page:

<wrap-owlcarousel class="owl-carousel" 
    data-options="{autoPlay: 5000, stopOnHover: true, 
        slideSpeed : 300, paginationSpeed : 400, singleItem : true}">
    <div>
        <img src="img/1.jpg"/>
    </div>
    <div>
        <img src="img/2.jpg"/>
    </div>
</wrap-owlcarousel>

See more about OWL Carousel.

Run multiple bat files from single bat file

Windows batch file allows to run another batch file inside of it. There are 2 common way to do it:
1. Using CALL command:
bat_call

2. Using START command:
bat_start

The difference between them is the START command runs asynchronously, i.e. it doesn’t wait for finishing previous execution!
Instead of CALL command which is ok for sequential execution.

Thanx to http://stackoverflow.com/a/1104009

Using binding in JSF

jsflife
You may heard about binding term in JSF. But it can be used?
Binding can be involved in validation procedure.

1. Add form to JSF page:

<h:form>
    <h:inputText id="email" 
        value="#{SomeBaen.emailValue}" 
        binding="#{SomeBaen.emailComponent}" />
    <h:message for="email" />

    <h:commandButton value="Check" action="#{SomeBaen.doCheck}" />
</h:form>

2. Create bean:

public class SomeBean implements Serializable {
    private static final long serialVersionUID = 1L;
    
    private String emailValue;
    private UIComponent emailComponent;
    
    public String doCheck() {
        if (isValid(emailValue)) {
            return "ok";
        }
        
        // set invalid message
        FacesMessage message = new FacesMessage("Invalid email");
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(emailComponent.getClientId(context), message);
        
        return null;
    }
    
    public void setEmailValue(String emailValue) {
        this.emailValue = emailValue;
    }
    
    public String getEmailValue() {
        return emailValue;
    }
    
    public void setEmailComponent(UIComponent emailComponent) {
        this.emailComponent = emailComponent;
    }
    
    public UIComponent getEmailComponent() {
        return emailComponent;
    }
}

So, if validation failed you’ll see a message near inputText control.
Also example http://stackoverflow.com/a/321722

Passing command line arguments to ant script file

ant-promt
You may know such ant task as Input – http://ant.apache.org/manual/Tasks/input.html.
It promts user to enter some stuff. But what if you want to run script without promt but using command line arguments only?
Ant presents the awesome solution: just use -D option!
See more at http://ant.apache.org/manual/running.html

Consider we have ant script run.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>

<project name="someProject" default="someTask" basedir=".">
    <taskdef resource="net/sf/antcontrib/antlib.xml" />

    <target name="someTask">
        <input message="Enter first argument" addproperty="arg.first" />
        <input message="Enter second argument" addproperty="arg.second" />
        
        <!-- your code here... -->
        <copy file="arg.first" tofile="${arg.second}" />
    </target>
</project>

To run it in batch mode use:

  ant -buildfile run.xml someTask -Darg.first=111.txt -Darg.second=111_copy.txt

Thanx to http://stackoverflow.com/a/14309773

Primefaces ajax update on rendered attribute

rendered
If you are using rendered attribute on some component and want to update it on ajax request, you be noticed error.
So, that is normal because if rendered=”false” – this component will not be added to DOM tree.
That’s why it cannot be found on update callback.

For more info see
https://weblogs.java.net/blog/mriem/archive/2014/06/03/jsf-tip-62-understanding-renderedfalse.

To fix the problem wrap component into another container and make update on it.
For ex.

<p:outputPanel id="containerToUpdate">
    <p:panel rendered="#{SomeBean.isOk}">
        <!-- your code here -->
    </p:panel>
<p:outputPanel>

<p:commandButton update="containerToUpdate"/>

Thanx to http://stackoverflow.com/a/4525710.

Read version of package from META-INF in Java JSF Bean

vers
Often you need to show the software version in JSF page.
How to make it as follows:

1. Create JSF Bean

public class PropertiesBean implements Serializable {

    private static final long serialVersionUID = 8607941655014578684L;

    private static final String METAINF_LOCATION = "META-INF/MANIFEST.MF";
    private static final String VERSION_PREFIX = "Implementation-Version: ";
    private static final Pattern VERSION_PATTERN = Pattern.compile("(\\d+\\.\\d+\\.\\d+)");
    
    public String getVersion() {
        try {
            InputStream is = null;
            Scanner scanner = null;
            try {
                is = this.getClass().getClassLoader().getResourceAsStream(METAINF_LOCATION);
                scanner = new Scanner(is);
                while (scanner.hasNext()) {
                    String line = scanner.nextLine();
                    if (line.startsWith(VERSION_PREFIX)) {
                        Matcher matcher = VERSION_PATTERN.matcher(line);
                        if (matcher.find()) {
                            String group = matcher.group();
                            return group;
                        }
                    }
                }
            } finally {
                if (scanner != null) {
                    scanner.close();
                }
                if (is != null) {
                    is.close();
                }
            }
        } catch (Exception e) {
            // TODO: log error!
        }
        return null;
    }
}

2. Register bean in faces-config.xml

    <managed-bean>
        <managed-bean-name>PropertiesBean</managed-bean-name>
        <managed-bean-class>com.someproject.presentation.PropertiesBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>

3. Use it on JSF page:

<h:outputText value="[v#{PropertiesBean.version}]" rendered="#{PropertiesBean.version != null}" />

Profit!

See also, http://stackoverflow.com/a/4782124