• Primefaces: Close All Lightboxs

    I had a datatable which contains in each row a link to a different iFrame p:lighbox, and on the same page I had to repeat the same datatables with different objects modules and they also have iFrame p:lightbox.

    I came to a place where I had to close the p:lightbox from a backing bean according to some logic validation,
    and in the same time I can’t know the widgetvar of the current opened p:lightbox, so I had to write some generic function to close all the Lightboxes inside the page.

    First I had to know how primefaces generate the widget vars, after some search I found a function in primefaces which takes an ID of a component and generate a proper widgetvar for it.

    #{p:widgetVar('componentId')}
    

    Basically it does the following it gets the id of the component, for example formId:componentId and replaces the : with _ and append a widget_ to the beginning of the var so it becomes widget_formId_componentId.

    So I came with the following function of jQuery to close all my lightboxs in the page

    function closeLightBoxes() {	
    	$('.ui-lightbox').each(function() {
    		var widgetVar = 'widget_' + $(this).attr('id').replace(":", "_").replace('_panel','');		
    		window[widgetVar].hide();
    	});
    }
    

    First I select each .ui-lightbox class, which is the div of the iFrame and then I do the simple formation of primefaces widgetvar generation, but primefaces adds _panel to the end of the ID of that div, so I had to remove it.

    Note: do not specify any widgetvar to your lightboxs, not even dynamic ones.

  • Using Prettyfaces With Primefaces Upload

     

    <h:form id="upload" enctype="multipart/form-data">           
        <p:fileUpload fileUploadListener="#{fileUploadBean.handleFileUpload}" /> 
    </h:form>

    Well to get this to work smoothly with prettyfaces you must play with the configuration a bit.

    First in order to make sure that the primefaces upload works, you must make sure that you have these:

    • commons-fileupload and commons-io are present in the webapp’s runtime classpath (just dropping the JARs in /WEB-INF/lib ought to be sufficient).
    • The FileUploadFilter is mapped on the exact <servlet-name> of the FacesServlet as is been definied in your web.xml. If you’ve given it a <servlet-name> of for examplefacesServlet, then you need to edit it in the <filter-mapping> example as well.
    • The enctype of the <h:form> needs to be set to multipart/form-data.
    • In simple file upload with mode="simple", ajax must be disabled on any PrimeFaces command buttons/links by ajax="false".

    Now in the web.xml configure the fileupload filter which parses the multipart request. FileUpload filter should map to Faces Servlet:

    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>
            org.primefaces.webapp.filter.FileUploadFilter
        </filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    <!-- Make sure that your servlet-name is the same in primefaces <filter-mapping> -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>

    Notice that I didn’t configure any related Prettyfaces servlet filter!
    If you are using a Servlet 3.0 compliant container, PrettyFaces will automatically register the required Servlet Filter; otherwise, make sure PrettyFilter is the first filter in your web.xml file. (The dispatcher elements are required to ensure PrettyFaces intercepts both internal and external requests.)

    The trick in the config is the dispatcher inside filter-mapping of the primefaces upload filter

    <dispatcher>FORWARD</dispatcher>

    That’s it, you should now be able to upload with Prettyfaces URLs.

    Case built with:
    Primefaces 3.5
    prettyfaces 2.0.4
    commons-io 2.4
    commons-fileupload 1.3

    A helpful stackoverflow post

  • Primefaces p:dialog minWidth, minHeight. Bug

    p:dialog is a handy JSF component (by Primefaces), especially with a lot of controls available (ajax integration etc.).
    I ran into a small tiny bug with minWidth and minHeight, the component won’t set these values into the style.

    it’s always auto unless I define a width and height which is not my target, my primefaces version is 3.5.

    so I had to let jQuery handle this issue by selecting the class of my dialog and add these missing style attributes.

    First make sure you set the styleClass into “dialog-fix” or whatever you like (this class name is used by jQuery selector)

    
    ....
    
    
    

    And here’s the jQuery function

    /**
    * this js function would fix p:dialog minWidth and minHieght Bug in primefaces
    * just don't forget to include styleClass into p:dialog as dialog-fix for jQuery Selectors
    * for more info, I issued a bug ticket https://code.google.com/p/primefaces/issues/detail?id=5769
    * Hatem Alimam
    */
    
    $(function() {
    $('.dialog-fix').css("min-width", "400px");
    });
    
    

    In that way I overcome this bug (at least for now).

    For more info I issued a bug ticket

  • Netbeans won’t work on Mac OS X since apple dropt support for Java updates

    netbeans java 7 mac

    Apple, which has previously included Java with installations of Mac OS X, announced the move on its support site. It said that customers need to obtain Java directly from Oracle if they want to access web content written the widely used programming language.

    And now Oracle releases Java SE Development Kit 7 (JDK 1.7u4) for Mac OS X. It is the first official Oracle release for Mac OS X. See Release Notes.

    In case you type

    java -version

    you are going to notice that your mac still has 1.6 JVM.

    Too many forums suggest to edit your java environment variables, but this is a very bad solution and maybe a temp one.
    As I searched I noticed that Oracle has released a Netbeans 7.3 along with JDK 7 which would solve the problem.

    Here’s the link: JDK 7u17 with NetBeans 7.3 – x64

    After you install the new JDK all your previous Netbeans versions will work normally.

  • Exit Button In Android App

    Android Exit

    A lot of developers wondering about the exit button and how they can apply it efficiently, well there are many discussions on stackoverflow or other forums suggest to use this piece of code.

    finish();
    System.exit(0);
    

    This code will only “finish” the current activity and never finishes the rest of the activities or take them to the finalize stage.

    So is there a way to exit an android app completely ?

    the answer is very simple: never and never allow the user to exit the app on his own.

    WHY ?

    That’s against Android nature and best practices, and yet the system handles this automatically. That’s what the activity lifecycle (especially onPause/onStop/onDestroy) is for. No matter what you do, do not put a “close” or “exit” application button.

    Do Users Really Want It?

    Let me posit this assertion: no they don’t.

    Of the apps that ship with the device (Gmail, Contacts, Maps, Gallery, etc.) exactly none of them include an exit button, and most users are comfortable with that. Nonetheless many developers are adamant, “I added it after users demanded it!”.
    if you have to add this button, try not to, just explain why to whom ever demands this functionality, as I’m trying to do always when this case comes around, and it always works.

    Here’s a good video which explains the case:

  • Yii and instagram API

    Yii and instagram API PHP

    Instagram is a free photo-sharing program and social network that was launched in October 2010. The service enables users to take a photo, apply a digital filter to it, and then share it with other Instagram users they are connected to on the social network as well as on a variety of social networking services.

    Too many developed great apps for this platform such as statigr.am which is a great implementation of the public API.

    instagram gives developers a great API, however there’s a php API of course based on the original instagram API but much more simpler to use in your PHP project.

    This API called PHP-Instagram-API .

    To use this API in your Yii project please download it from here.

    extract to downloaded ZIP file. You will see three folders “Examples”, “Instagram”, “Tests”.
    Just take “Instagram” folder and place it under “protected/components/”.

    I created a nice class and called it InstaConnect, which you can place all your functions inside it (in components folder).
    Read more

  • Android: WebView unable to show ‘%’ percentage sign

    When you use WebView and pass HTML to it if that HTML code contains “%” – percentage sign you would have “Web Page not Available” error.

    Webpage Not Available

    % ERROR

    let’s see this simple code

    String message = "Show Percent: 38% ";
    WebView mWebView = (WebView) activity.findViewById(R.id.my_web_view);
    mWebView.loadData(message, "text/html", "en_US");

    simply the error still exists.
    after searching the internet, I found a nice and easy solution.

    String message = "Show Percent: 38% ";
    message = message.replaceAll("%", "&#37");
    WebView mWebView = (WebView) activity.findViewById(R.id.my_web_view);
    mWebView.loadData(message, "text/html", "en_US");

    the solution is to replace all the % sings in the HTML code with

    &#37

    (How HTML sees the %).

    message = message.replaceAll("%", "&#37");
  • MAMP, phpMyAdmin “The server is not responding” Error.

    I’m using MAC OSX Lion 10.7.3, I had a problem with MAMP, in general starting mysql and apache along with previous installed mysql on the system.

    I managed to start mysql (included in MAMP) by executing the following command in terminal: sudo killall -9 mysqld

    anyway after struggling in getting mysql to work, of course with a different port on the system which is 8889, I had another problem when going to phpMyAdmin:

    It seems that many users tracked the problem to the browser cache. The consensus is to clear out the browser cache when this problem occurs. I have tried this several times already, and it is doing the trick. Each time I get the red error screen in phpMyAdmin, I clear out the cache in Firefox, close down the browser. and then reopen the MAMP admin page. I can once again access the phpMyAdmin panel.

    for a bad day I had another bad issue after upgrading my Firefox to 10.0.1.


    after deleting my MAMP 2.0.5 and reinstall MAMP 1.9.4 problem still exists !!!!!!!!
    after an hour searching I tried access phpMyAdmin from Safari guess what it worked !!!!!!
    firefox 10.0.1 has problems with phpMyAdmin, for some reason some scripts is broken on it.
    so my bad day ends in hating firefox new update which is 10.0.1, and switching to Safari for the time being.

  • JPA Criteria and JPQL

    JPA2 introduced its “Criteria” queries, providing an API for typesafe query generation without the need to hardcode field names etc in queries; it built on the approach of Hibernate Criteria.
    In these examples we have two classes, Inventory and Product, where Inventory has a set of products.

    Select of persistable objects with simple filter

    JPQL single-string would be

    SELECT p FROM Product p WHERE p.name = 'MP3 Extra'

    JPA Criteria would be

    CriteriaQuery criteria = builder.createQuery(Product.class);
    Root productRoot = criteria.from(Product.class);
    criteria.select(productRoot);
    criteria.where(builder.equal(productRoot.get(Product_.name), "MP3 Extra"));
    List products = em.createQuery(criteria).getResultList();

    JPQL single-string would be

    SELECT p.value, p.manufacturer FROM Product p WHERE p.name = 'MP3 Extra'

    JPA Criteria would be

    CriteriaQuery criteria = builder.createQuery();
    Root productRoot = criteria.from(Product.class);
    criteria.multiselect(productRoot.get(Product_.value),
    productRoot.get(Product_.manufacturer);
    criteria.where(builder.equal(productRoot.get(Product_.name), "MP3 Extra"));
    List <Object[]> results = em.createQuery(criteria).getResultList();

    Select of aggregate of attribute of persistable objects

    JPQL single-string would be
    Read more

  • mod_proxy vs. mod_jk, or direct access ?


    Let me visualize the pros/cons comparison for those modules exists on http://blog.jboss.org/

    mod_proxy

    * Pros:
    o No need for a separate module compilation and maintenance. mod_proxy,
    mod_proxy_http, mod_proxy_ajp and mod_proxy_balancer comes as part of
    standard Apache 2.2+ distribution
    o Ability to use http https or AJP protocols, even within the same
    balancer.
    * Cons:
    o mod_proxy_ajp does not support large 8K+ packet sizes.
    o Basic load balancer
    o Does not support Domain model clustering

    mod_jk

    * Pros:
    o Advanced load balancer
    o Advanced node failure detection
    o Support for large AJP packet sizes
    * Cons:
    o Need to build and maintain a separate module

    —————————————————————————————————–

    after trying both it turned out that both are very good if you have to host on apache and glassfish at the same time.
    but when it comes to hosting only on glassfish(make it listen on port 80) it’s absolutely the DIRECT ACCESS .

    Glassfish is one of the best application servers worldwide, and it’s free to use.
    and it has the ability to host multi-domain names on the same server IP by defining a Virtual Server for each domain/application on a domain.
    in case if you have multi-domain names on the same application it supports it too, in the Virtual Server Creation/Edit form just define your list of domain names or hosts or IPs comma-separated in Hosts field. and define your HTTP Listeners(which is the Port 80) and define your Default Web Module(application).

    and that’s it !!

    Why to take the direct access to glassfish rather then apache >> galssfish … simply it’s much more faster.
    and glassfish cash just works fine.