Skip to content

Commit

Permalink
Improved: Add possibility to filter by date on PartyContentWrapper class
Browse files Browse the repository at this point in the history
For method PartyContentWrapper::getFirstPartyContentByType add dateTime parameter to add possibility to retrieve a dated content

This is useful for document generated at date (like invoice, purchase order) on custom plugin
  • Loading branch information
nmalin committed Nov 19, 2024
1 parent 9bbe7f5 commit 2b684ba
Showing 1 changed file with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -34,6 +35,7 @@
import org.apache.ofbiz.base.util.GeneralException;
import org.apache.ofbiz.base.util.GeneralRuntimeException;
import org.apache.ofbiz.base.util.StringUtil;
import org.apache.ofbiz.base.util.UtilDateTime;
import org.apache.ofbiz.base.util.UtilHttp;
import org.apache.ofbiz.base.util.UtilValidate;
import org.apache.ofbiz.base.util.cache.UtilCache;
Expand All @@ -43,7 +45,6 @@
import org.apache.ofbiz.entity.GenericValue;
import org.apache.ofbiz.entity.condition.EntityCondition;
import org.apache.ofbiz.entity.util.EntityQuery;
import org.apache.ofbiz.entity.util.EntityUtil;
import org.apache.ofbiz.service.LocalDispatcher;

/**
Expand Down Expand Up @@ -287,6 +288,11 @@ public static List<String> getPartyContentTextList(GenericValue party, String pa
}

public static GenericValue getFirstPartyContentByType(String partyId, GenericValue party, String partyContentTypeId, Delegator delegator) {
return getFirstPartyContentByType(partyId, party, partyContentTypeId, delegator, UtilDateTime.nowTimestamp());
}

public static GenericValue getFirstPartyContentByType(String partyId, GenericValue party, String partyContentTypeId,
Delegator delegator, Timestamp date) {
if (partyId == null && party != null) {
partyId = party.getString("partyId");
}
Expand All @@ -298,24 +304,19 @@ public static GenericValue getFirstPartyContentByType(String partyId, GenericVal
if (delegator == null) {
throw new IllegalArgumentException("Delegator missing");
}

List<GenericValue> partyContentList = null;
try {
partyContentList = EntityQuery.use(delegator).from("PartyContent")
return EntityQuery.use(delegator).from("PartyContent")
.where("partyId", partyId, "partyContentTypeId", partyContentTypeId)
.orderBy("-fromDate")
.cache(true)
.queryList();
.filterByDate(date == null
? UtilDateTime.nowTimestamp()
: date)
.cache()
.queryFirst();
} catch (GeneralException e) {
Debug.logError(e, MODULE);
}

if (partyContentList != null) {
partyContentList = EntityUtil.filterByDate(partyContentList);
return EntityUtil.getFirst(partyContentList);
} else {
return null;
}
return null;
}

public static PartyContentWrapper makePartyContentWrapper(GenericValue party, HttpServletRequest request) {
Expand Down

0 comments on commit 2b684ba

Please sign in to comment.