The best place to *find* answers to programming/development questions, imo, however it's the *worst* place to *ask* questions (if your first question/comment doesn't get any up-rating/response, then u can't ask anymore questions--ridiculously unrealistic), but again, a great reference for *finding* answers.

My Music (Nickleus)

20121031

enhanced for loop (for each) for java.util.collection - Type mismatch: cannot convert from element type Object to String

i had the following code that i wanted to convert to the new enhanced for loop:
private String myMethod(Collection c){
    for(Iterator<String> it = c.iterator(); it.hasNext();){
        String s = it.next();
        ...
    }
}





so i rewrote it like this:
private String myMethod(Collection c){
    for(String s : c){
        ...
    }
}

but that code gave me the following "error" in eclipse:
Type mismatch: cannot convert from element type Object to String



here's the final, error-free solution:
private String myMethod(Collection<String> c){
    for(String s : c){
        ...
    }
}

20121025

java ejb ql: SELECT count(*) from X where x IN (SELECT DISTINCT x FROM Y WHERE z)

when a user with the buyer role logs into the system, we want to count the distinct number of tours that have trqs (transport requests) registered for that buyer/user.

a Tour is composed of 0 to many Trqs. Trqs can only be associated with 1 Tour.

Tour table:

CREATE TABLE TOUR
(
   ID decimal(20) PRIMARY KEY NOT NULL,

...
);

Trq table:

CREATE TABLE TRQ
(
...

   BUYERID varchar2(100) NOT NULL,
   TOURID decimal(20),

...
);



the relevant TourServicesBean java code will look like this:

public int findCountByMemberId(String memberId) {
  String s = "SELECT count(t) from Tour t where t.id IN (SELECT DISTINCT tr.tourId FROM Trq tr WHERE tr.buyerId = '"+memberId+"' and tr.tourId = t.id)";
  Query q = em.createQuery(s);
  Long l = (Long) q.getSingleResult();
  return l.intValue();
}


the inner SELECT can return multiple, identical tourIds so we use DISTINCT to only get unique tourIds. then the main SELECT counts the number of unique tourids.

20121024

how to output an array of strings to a comma separated list of strings

import java.util.Arrays;

public class StringTest {
    private static String[] colIds = {"abc","def","ghi"};
 

    public static void main(String[] args) {
        String s = Arrays.toString(colIds);
         // remove leading and trailing brackets, i.e. "[" and "]"
        System.out.println(s.substring(1, s.length()-1));
    }
}



the output will look like this:
abc, def, ghi

if you take away the substring code, then the output will look like this:
[abc, def, ghi]

sucks that geany doesn't appear to have negative lookahead regex capabilities :(

as of version 0.21 in ubuntu 12.04

e.g.
(?!rich)faces
doesn't work (i.e. match strings ending in faces that aren't preceded by rich)

whereas this works:
(?:rich)faces
doesn't work (i.e. match strings ending in faces that are preceded by rich)


20121015

rich:calendar button icon not rendering or getting rerendered

i have a rich:calendar inside a rich:column in a rich:dataTable. when the table gets rendered i only see an input field for the rich:calendar, no calendar icon. when i sort the column, then the icon becomes visible.

the workaround is to define buttonIcon, e.g.:

    <rich:column sortable="true" sortBy="#{trip.startDate}"
        filterBy="#{trip.startDate}" filterEvent="onkeyup">
        <f:facet name="header">
            <h:outputText value="#{msg['label.startDate']}" />
        </f:facet>
            <h:outputText value="#{trip.getStartDateDate()}"
                style="width: 80%;text-align: center" rendered="#{!tripMgrBean.isEditable(trip.id)}">
                <f:convertDateTime type="date" dateStyle="short" timeZone="Europe/Oslo" pattern="#{msg['calendar.datePattern']}"/>
            </h:outputText>
            <rich:calendar value="#{trip.startDate}" buttonIcon="/images/icons/calendar.gif"
                enableManualInput="true"
                converterMessage="#{msg['label.wrong']} #{msg['label.dateformat']}"
                locale="#{userMenuBean.userLocale}"
                direction="auto"
                popup="true"
                showInput="true"
                showApplyButton="false"
                datePattern="#{msg['calendar.datePattern']}"
                inputSize="13"
                cellWidth="24px"
                cellHeight="22px"
                rendered="#{tripMgrBean.isEditable(trip.id)}"/>
    </rich:column>

then it gets rendered every time =)
set the value of buttonIcon to whatever you like.
note, you can also use a text if you like, instead, e.g.: buttonLabel="Choose"

20121014

jboss 4.2.2.GA how to inject ejb3 stateless session bean into jsf 1.2 managed backing bean

##########
// JSF 1.2 backing bean

@org.ajax4jsf.model.KeepAlive
public class TripManagerBean {
  @EJB(name="earName/TripBean/local")
  private TripLocal tripBean;

  public TripManagerBean() {
...
}
##########

earName is foo if the EAR is called foo.ear.

##########
// EJB3 bean

@Stateless
public class TripBean implements TripLocal {
  @PersistenceContext
  private EntityManager em;
...
}
##########

##########
// EJB3 bean local interface

public interface TripLocal {
...
}
##########

thanks to this page about jndi bindings in ejb3

and here's how to lookup an ejb3 stateless session bean using jndi from the backing bean.

jboss 4.2.2.GA how to get ejb3 stateless session bean using jndi in jsf 1.2 managed backing bean

##########
// JSF 1.2 backing bean

@org.ajax4jsf.model.KeepAlive
public class TripManagerBean {
  private TripLocal tripBean;
  public TripManagerBean() {
...
    initTripBean();
  }

  private void initTripBean() {
    if(tripBean == null) {
      try {
        tripBean = (TripLocal) InitialContext.doLookup("earName/TripBean/local");
      } catch (NamingException e) {
        e.printStackTrace();
      }          
    }
  }
...
}
##########

earName is foo if the EAR is called foo.ear.

##########
// EJB3 bean

@Stateless
public class TripBean implements TripLocal {
  @PersistenceContext
  private EntityManager em;
...
}
##########

##########
// EJB3 bean local interface

public interface TripLocal {
...
}
##########

thanks to this page about jndi bindings in ejb3

and here's how to inject an ejb3 stateless session bean into a jsf managed backing bean using the ejb annotation

eclipse not creating class files

when i save a change in a java file, its class file in the bin output folder wasn't getting updated with the change.

i found i had to enabled "build automatically" in preferences:
window > preferences > general > workspace > check build automatically

thanks to this thread:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=23076

20121008

eclipse keyboard shortcut: maximize/minimize/restore/hide/show visual/source view tab/panel: Shift+F6

if you're editing an xhtml/jsf file in eclipse you have views like:
visual/source
source
preview

well if you're in the visual/source view (tab) you can toggle the visual part of the view by doing the following keyboard shortcut:
Shift+F6