In Liferay 6.1, is it possible to search by date only on a datetime field for a ServiceBuilder portlet?

StackOverflow https://stackoverflow.com/questions/12150274

  •  28-06-2021
  •  | 
  •  

Domanda

I have created a portlet, via Service Builder, that stores some user data, as well as a date.

relevant portion of my service.xml file

<entity name="ServiceAgreement" local-service="true" remote-service="false">
    <column name="agreementId" type="long" primary="true"></column>
    <column name="agreementVersion" type="double"></column>
    <column name="userId" type="long"></column>
    <column name="agreementTimestamp" type="Date"></column><!-- column w/ the trouble -->
    <column name="groupId" type="long"></column>
    <column name="companyId" type="long"></column>
    <order by="asc">
        <order-column name="agreementTimestamp"></order-column>
    </order>
    <finder name="G_AT" return-type="Collection">
        <finder-column name="groupId"></finder-column>
        <finder-column name="agreementTimestamp"></finder-column>
    </finder>
    <finder name="SignedDate" return-type="Collection">
        <finder-column name="agreementTimestamp"></finder-column>
    </finder>
...

I am currently writing another portlet that will be a reporting portlet for the data stored in this first portlet.

The trouble I'm having is when I perform a search by date, using the tag, I am not getting any responses from the DB for dates that I know are in there. I have tracked the problem down to the fact that they 'findbysignedDate' method that ServiceBuilder built, searching by date, down to the millisecond!

The record I want to retrieve: Record to be retrieved For example, my reporting portlet contains:

int month = Integer.parseInt(request.getParameter("dateMonth"));
int day = Integer.parseInt(request.getParameter("dateDay"));
int year = Integer.parseInt(request.getParameter("dateYear"));
GregorianCalendar cal = new GregorianCalendar();
cal.clear();
cal.set(year, month, day);
Date signedDate = cal.getTime();

try {
   if(signedDate == null || signedDate.getTime() < 0) {
     throw new PortalException("Invalid date input provided.");
} else {
    _log.debug("Signed Date: " + signedDate.toString());

    //get TOS data
    JSONArray tosArray = LiferayHelper.getTOSsBySignedDate(signedDate);
...

And nothing is returned in 'tosArray'. However, if I write it like this:

int month = Integer.parseInt(request.getParameter("dateMonth"));
int day = Integer.parseInt(request.getParameter("dateDay"));
int year = Integer.parseInt(request.getParameter("dateYear"));
GregorianCalendar cal = new GregorianCalendar();
cal.clear();
cal.set(year, month, day,18,49,15); //getting REALLY specific here...
cal.add(Calendar.MILLISECOND, 958);
Date signedDate = cal.getTime();

try {
   if(signedDate == null || signedDate.getTime() < 0) {
     throw new PortalException("Invalid date input provided.");
} else {
    _log.debug("Signed Date: " + signedDate.toString());

    //get TOS data
    JSONArray tosArray = LiferayHelper.getTOSsBySignedDate(signedDate);
...

Then tosArray contains the correct data.

I just want to pull back all data for a single day (24-hour period). Does anyone know if there is a built-in way around this? Or do I need to rewrite my service.xml file? Am I stuck having to write my own search method?

Thanks.

È stato utile?

Soluzione

The answer is to use the custom queries that Service Builder will build for you. Found this at the end of Chapter 7 of Liferay in Action.

A brief overview is available on the Liferay site.
http://www.liferay.com/community/wiki/-/wiki/Main/Service+Builder+Finders

Just be aware of the strict naming convention contract that Liferay enforces for these Finders. I struggled for almost a whole day trying to get service generator to automatically build my new finder classes, only to discover that it was failing because I didn't name my finder what Service Builder was expecting.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top