use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class ContentManagementServices method resequence.
public static Map<String, Object> resequence(DispatchContext dctx, Map<String, ? extends Object> context) throws GenericServiceException {
Map<String, Object> result = new HashMap<String, Object>();
Delegator delegator = dctx.getDelegator();
String contentIdTo = (String) context.get("contentIdTo");
Integer seqInc = (Integer) context.get("seqInc");
if (seqInc == null) {
seqInc = Integer.valueOf(100);
}
int seqIncrement = seqInc.intValue();
List<String> typeList = UtilGenerics.checkList(context.get("typeList"));
if (typeList == null) {
typeList = new LinkedList<String>();
}
String contentAssocTypeId = (String) context.get("contentAssocTypeId");
if (UtilValidate.isNotEmpty(contentAssocTypeId)) {
typeList.add(contentAssocTypeId);
}
if (UtilValidate.isEmpty(typeList)) {
typeList = UtilMisc.toList("PUBLISH_LINK", "SUB_CONTENT");
}
EntityCondition conditionType = EntityCondition.makeCondition("contentAssocTypeId", EntityOperator.IN, typeList);
EntityCondition conditionMain = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("contentIdTo", EntityOperator.EQUALS, contentIdTo), conditionType), EntityOperator.AND);
try {
List<GenericValue> listAll = EntityQuery.use(delegator).from("ContentAssoc").where(conditionMain).orderBy("sequenceNum", "fromDate", "createdDate").filterByDate().queryList();
String contentId = (String) context.get("contentId");
String dir = (String) context.get("dir");
int seqNum = seqIncrement;
String thisContentId = null;
for (int i = 0; i < listAll.size(); i++) {
GenericValue contentAssoc = listAll.get(i);
if (UtilValidate.isNotEmpty(contentId) && UtilValidate.isNotEmpty(dir)) {
// move targeted entry up or down
thisContentId = contentAssoc.getString("contentId");
if (contentId.equals(thisContentId)) {
if (dir.startsWith("up")) {
if (i > 0) {
// Swap with previous entry
try {
GenericValue prevValue = listAll.get(i - 1);
Long prevSeqNum = (Long) prevValue.get("sequenceNum");
prevValue.put("sequenceNum", Long.valueOf(seqNum));
prevValue.store();
contentAssoc.put("sequenceNum", prevSeqNum);
contentAssoc.store();
} catch (Exception e) {
return ServiceUtil.returnError(e.toString());
}
}
} else {
if (i < listAll.size()) {
// Swap with next entry
GenericValue nextValue = listAll.get(i + 1);
nextValue.put("sequenceNum", Long.valueOf(seqNum));
nextValue.store();
seqNum += seqIncrement;
contentAssoc.put("sequenceNum", Long.valueOf(seqNum));
contentAssoc.store();
// skip next one
i++;
}
}
} else {
contentAssoc.put("sequenceNum", Long.valueOf(seqNum));
contentAssoc.store();
}
} else {
contentAssoc.put("sequenceNum", Long.valueOf(seqNum));
contentAssoc.store();
}
seqNum += seqIncrement;
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.toString());
}
return result;
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class ContentManagementWorker method updateStatsTopDown.
public static int updateStatsTopDown(Delegator delegator, String contentId, List<String> typeList) throws GenericEntityException {
int subLeafCount = 0;
GenericValue thisContent = EntityQuery.use(delegator).from("Content").where("contentId", contentId).queryOne();
if (thisContent == null)
throw new RuntimeException("No entity found for id=" + contentId);
List<EntityCondition> conditionMain = new ArrayList<EntityCondition>();
conditionMain.add(EntityCondition.makeCondition("contentIdTo", contentId));
if (typeList.size() > 0) {
conditionMain.add(EntityCondition.makeCondition("contentAssocTypeId", EntityOperator.IN, typeList));
}
List<GenericValue> contentAssocs = EntityQuery.use(delegator).from("ContentAssoc").where(conditionMain).filterByDate().cache().queryList();
for (GenericValue contentAssoc : contentAssocs) {
String subContentId = contentAssoc.getString("contentId");
subLeafCount += updateStatsTopDown(delegator, subContentId, typeList);
}
// If no children, count this as a leaf
if (subLeafCount == 0)
subLeafCount = 1;
thisContent.put("childBranchCount", Long.valueOf(contentAssocs.size()));
thisContent.put("childLeafCount", Long.valueOf(subLeafCount));
thisContent.store();
return subLeafCount;
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class PayPalServices method parseStateProvinceGeoId.
private static String parseStateProvinceGeoId(String payPalShipToState, String countryGeoId, Delegator delegator) {
String lookupField = "geoName";
List<EntityCondition> conditionList = new LinkedList<EntityCondition>();
conditionList.add(EntityCondition.makeCondition("geoAssocTypeId", "REGIONS"));
if ("USA".equals(countryGeoId) || "CAN".equals(countryGeoId)) {
// PayPal returns two letter code for US and Canadian States/Provinces
String geoTypeId = "USA".equals(countryGeoId) ? "STATE" : "PROVINCE";
conditionList.add(EntityCondition.makeCondition("geoTypeId", geoTypeId));
lookupField = "geoCode";
}
conditionList.add(EntityCondition.makeCondition("geoIdFrom", countryGeoId));
conditionList.add(EntityCondition.makeCondition(lookupField, payPalShipToState));
EntityCondition cond = EntityCondition.makeCondition(conditionList);
GenericValue geoAssocAndGeoTo = null;
try {
geoAssocAndGeoTo = EntityQuery.use(delegator).from("GeoAssocAndGeoTo").where(cond).cache().queryFirst();
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
if (geoAssocAndGeoTo != null) {
return geoAssocAndGeoTo.getString("geoId");
}
return null;
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class PayPalServices method getExpressCheckout.
public static Map<String, Object> getExpressCheckout(DispatchContext dctx, Map<String, Object> context) {
Locale locale = (Locale) context.get("locale");
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
ShoppingCart cart = (ShoppingCart) context.get("cart");
GenericValue payPalConfig = getPaymentMethodGatewayPayPal(dctx, context, null);
if (payPalConfig == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPayPalPaymentGatewayConfigCannotFind", locale));
}
NVPEncoder encoder = new NVPEncoder();
encoder.add("METHOD", "GetExpressCheckoutDetails");
String token = (String) cart.getAttribute("payPalCheckoutToken");
if (UtilValidate.isNotEmpty(token)) {
encoder.add("TOKEN", token);
} else {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPayPalTokenNotFound", locale));
}
NVPDecoder decoder;
try {
decoder = sendNVPRequest(payPalConfig, encoder);
} catch (PayPalException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (UtilValidate.isNotEmpty(decoder.get("NOTE"))) {
cart.addOrderNote(decoder.get("NOTE"));
}
if (cart.getUserLogin() == null) {
try {
GenericValue userLogin = EntityQuery.use(delegator).from("UserLogin").where("userLoginId", "anonymous").queryOne();
try {
cart.setUserLogin(userLogin, dispatcher);
} catch (CartItemModifyException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
boolean anon = "anonymous".equals(cart.getUserLogin().getString("userLoginId"));
// Even if anon, a party could already have been created
String partyId = cart.getOrderPartyId();
if (partyId == null && anon) {
// Check nothing has been set on the anon userLogin either
partyId = cart.getUserLogin() != null ? cart.getUserLogin().getString("partyId") : null;
cart.setOrderPartyId(partyId);
}
if (partyId != null) {
GenericValue party = null;
try {
party = EntityQuery.use(delegator).from("Party").where("partyId", partyId).queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
if (party == null) {
partyId = null;
}
}
Map<String, Object> inMap = new HashMap<String, Object>();
Map<String, Object> outMap = null;
// Create the person if necessary
boolean newParty = false;
if (partyId == null) {
newParty = true;
inMap.put("userLogin", cart.getUserLogin());
inMap.put("personalTitle", decoder.get("SALUTATION"));
inMap.put("firstName", decoder.get("FIRSTNAME"));
inMap.put("middleName", decoder.get("MIDDLENAME"));
inMap.put("lastName", decoder.get("LASTNAME"));
inMap.put("suffix", decoder.get("SUFFIX"));
try {
outMap = dispatcher.runSync("createPerson", inMap);
partyId = (String) outMap.get("partyId");
cart.setOrderPartyId(partyId);
cart.getUserLogin().setString("partyId", partyId);
inMap.clear();
inMap.put("userLogin", cart.getUserLogin());
inMap.put("partyId", partyId);
inMap.put("roleTypeId", "CUSTOMER");
dispatcher.runSync("createPartyRole", inMap);
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
// Create a new email address if necessary
String emailContactMechId = null;
String emailContactPurposeTypeId = "PRIMARY_EMAIL";
String emailAddress = decoder.get("EMAIL");
if (!newParty) {
EntityCondition cond = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition(UtilMisc.toMap("partyId", partyId, "contactMechTypeId", "EMAIL_ADDRESS")), EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("infoString"), EntityComparisonOperator.EQUALS, EntityFunction.UPPER(emailAddress))));
try {
GenericValue matchingEmail = EntityQuery.use(delegator).from("PartyAndContactMech").where(cond).orderBy("fromDate").filterByDate().queryFirst();
if (matchingEmail != null) {
emailContactMechId = matchingEmail.getString("contactMechId");
} else {
// No email found so we'll need to create one but first check if it should be PRIMARY or just BILLING
long primaryEmails = EntityQuery.use(delegator).from("PartyContactWithPurpose").where("partyId", partyId, "contactMechTypeId", "EMAIL_ADDRESS", "contactMechPurposeTypeId", "PRIMARY_EMAIL").filterByDate("contactFromDate", "contactThruDate", "purposeFromDate", "purposeThruDate").queryCount();
if (primaryEmails > 0)
emailContactPurposeTypeId = "BILLING_EMAIL";
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
}
if (emailContactMechId == null) {
inMap.clear();
inMap.put("userLogin", cart.getUserLogin());
inMap.put("contactMechPurposeTypeId", emailContactPurposeTypeId);
inMap.put("emailAddress", emailAddress);
inMap.put("partyId", partyId);
inMap.put("roleTypeId", "CUSTOMER");
// Going to assume PayPal has taken care of this for us
inMap.put("verified", "Y");
inMap.put("fromDate", UtilDateTime.nowTimestamp());
try {
outMap = dispatcher.runSync("createPartyEmailAddress", inMap);
emailContactMechId = (String) outMap.get("contactMechId");
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
cart.addContactMech("ORDER_EMAIL", emailContactMechId);
// Phone number
String phoneNumber = decoder.get("PHONENUM");
String phoneContactId = null;
if (phoneNumber != null) {
inMap.clear();
if (phoneNumber.startsWith("+")) {
// International, format is +XXX XXXXXXXX which we'll split into countryCode + contactNumber
String[] phoneNumbers = phoneNumber.split(" ");
inMap.put("countryCode", StringUtil.removeNonNumeric(phoneNumbers[0]));
inMap.put("contactNumber", phoneNumbers[1]);
} else {
// U.S., format is XXX-XXX-XXXX which we'll split into areaCode + contactNumber
inMap.put("countryCode", "1");
String[] phoneNumbers = phoneNumber.split("-");
inMap.put("areaCode", phoneNumbers[0]);
inMap.put("contactNumber", phoneNumbers[1] + phoneNumbers[2]);
}
inMap.put("userLogin", cart.getUserLogin());
inMap.put("partyId", partyId);
try {
outMap = dispatcher.runSync("createUpdatePartyTelecomNumber", inMap);
phoneContactId = (String) outMap.get("contactMechId");
cart.addContactMech("PHONE_BILLING", phoneContactId);
} catch (GenericServiceException e) {
Debug.logError(e, module);
}
}
// Create a new Postal Address if necessary
String postalContactId = null;
boolean needsShippingPurpose = true;
// if the cart for some reason already has a billing address, we'll leave it be
boolean needsBillingPurpose = (cart.getContactMech("BILLING_LOCATION") == null);
Map<String, Object> postalMap = new HashMap<String, Object>();
postalMap.put("toName", decoder.get("SHIPTONAME"));
postalMap.put("address1", decoder.get("SHIPTOSTREET"));
postalMap.put("address2", decoder.get("SHIPTOSTREET2"));
postalMap.put("city", decoder.get("SHIPTOCITY"));
String countryGeoId = PayPalServices.getCountryGeoIdFromGeoCode(decoder.get("SHIPTOCOUNTRYCODE"), delegator);
postalMap.put("countryGeoId", countryGeoId);
postalMap.put("stateProvinceGeoId", parseStateProvinceGeoId(decoder.get("SHIPTOSTATE"), countryGeoId, delegator));
postalMap.put("postalCode", decoder.get("SHIPTOZIP"));
if (!newParty) {
// We want an exact match only
EntityCondition cond = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition(postalMap), EntityCondition.makeCondition(UtilMisc.toMap("attnName", null, "directions", null, "postalCodeExt", null, "postalCodeGeoId", null)), EntityCondition.makeCondition("partyId", partyId)));
try {
GenericValue postalMatch = EntityQuery.use(delegator).from("PartyAndPostalAddress").where(cond).orderBy("fromDate").filterByDate().queryFirst();
if (postalMatch != null) {
postalContactId = postalMatch.getString("contactMechId");
List<GenericValue> postalPurposes = EntityQuery.use(delegator).from("PartyContactMechPurpose").where("partyId", partyId, "contactMechId", postalContactId).filterByDate().queryList();
List<Object> purposeStrings = EntityUtil.getFieldListFromEntityList(postalPurposes, "contactMechPurposeTypeId", false);
if (UtilValidate.isNotEmpty(purposeStrings) && purposeStrings.contains("SHIPPING_LOCATION")) {
needsShippingPurpose = false;
}
if (needsBillingPurpose && UtilValidate.isNotEmpty(purposeStrings) && purposeStrings.contains("BILLING_LOCATION")) {
needsBillingPurpose = false;
}
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
}
if (postalContactId == null) {
postalMap.put("userLogin", cart.getUserLogin());
postalMap.put("fromDate", UtilDateTime.nowTimestamp());
try {
outMap = dispatcher.runSync("createPartyPostalAddress", postalMap);
postalContactId = (String) outMap.get("contactMechId");
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
if (needsShippingPurpose || needsBillingPurpose) {
inMap.clear();
inMap.put("userLogin", cart.getUserLogin());
inMap.put("contactMechId", postalContactId);
inMap.put("partyId", partyId);
try {
if (needsShippingPurpose) {
inMap.put("contactMechPurposeTypeId", "SHIPPING_LOCATION");
dispatcher.runSync("createPartyContactMechPurpose", inMap);
}
if (needsBillingPurpose) {
inMap.put("contactMechPurposeTypeId", "BILLING_LOCATION");
dispatcher.runSync("createPartyContactMechPurpose", inMap);
}
} catch (GenericServiceException e) {
// Not the end of the world, we'll carry on
Debug.logInfo(e.getMessage(), module);
}
}
// Load the selected shipping method - thanks to PayPal's less than sane API all we've to work with is the shipping option label
// that was shown to the customer
String shipMethod = decoder.get("SHIPPINGOPTIONNAME");
if ("Calculated Offline".equals(shipMethod)) {
cart.setAllCarrierPartyId("_NA_");
cart.setAllShipmentMethodTypeId("NO_SHIPPING");
} else {
String[] shipMethodSplit = shipMethod.split(" - ");
cart.setAllCarrierPartyId(shipMethodSplit[0]);
String shippingMethodTypeDesc = StringUtils.join(shipMethodSplit, " - ", 1, shipMethodSplit.length);
try {
GenericValue shipmentMethod = EntityQuery.use(delegator).from("ProductStoreShipmentMethView").where("productStoreId", cart.getProductStoreId(), "partyId", shipMethodSplit[0], "roleTypeId", "CARRIER", "description", shippingMethodTypeDesc).queryFirst();
cart.setAllShipmentMethodTypeId(shipmentMethod.getString("shipmentMethodTypeId"));
} catch (GenericEntityException e1) {
Debug.logError(e1, module);
}
}
// Get rid of any excess ship groups
List<CartShipInfo> shipGroups = cart.getShipGroups();
for (int i = 1; i < shipGroups.size(); i++) {
Map<ShoppingCartItem, BigDecimal> items = cart.getShipGroupItems(i);
for (Map.Entry<ShoppingCartItem, BigDecimal> entry : items.entrySet()) {
cart.positionItemToGroup(entry.getKey(), entry.getValue(), i, 0, false);
}
}
cart.cleanUpShipGroups();
cart.setAllShippingContactMechId(postalContactId);
Map<String, Object> result = ShippingEvents.getShipGroupEstimate(dispatcher, delegator, cart, 0);
if (result.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR)) {
return ServiceUtil.returnError((String) result.get(ModelService.ERROR_MESSAGE));
}
BigDecimal shippingTotal = (BigDecimal) result.get("shippingTotal");
if (shippingTotal == null) {
shippingTotal = BigDecimal.ZERO;
}
cart.setItemShipGroupEstimate(shippingTotal, 0);
CheckOutHelper cho = new CheckOutHelper(dispatcher, delegator, cart);
try {
cho.calcAndAddTax();
} catch (GeneralException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
// Create the PayPal payment method
inMap.clear();
inMap.put("userLogin", cart.getUserLogin());
inMap.put("partyId", partyId);
inMap.put("contactMechId", postalContactId);
inMap.put("fromDate", UtilDateTime.nowTimestamp());
inMap.put("payerId", decoder.get("PAYERID"));
inMap.put("expressCheckoutToken", token);
inMap.put("payerStatus", decoder.get("PAYERSTATUS"));
try {
outMap = dispatcher.runSync("createPayPalPaymentMethod", inMap);
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
String paymentMethodId = (String) outMap.get("paymentMethodId");
cart.clearPayments();
BigDecimal maxAmount = cart.getGrandTotal().setScale(2, RoundingMode.HALF_UP);
cart.addPaymentAmount(paymentMethodId, maxAmount, true);
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class SagePayPaymentServices method ccRefund.
public static Map<String, Object> ccRefund(DispatchContext ctx, Map<String, Object> context) {
Debug.logInfo("SagePay - Entered ccRefund", module);
Debug.logInfo("SagePay ccRefund context : " + context, module);
Locale locale = (Locale) context.get("locale");
GenericValue orderPaymentPreference = (GenericValue) context.get("orderPaymentPreference");
GenericValue captureTransaction = PaymentGatewayServices.getCaptureTransaction(orderPaymentPreference);
if (captureTransaction == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentTransactionAuthorizationNotFoundCannotRefund", locale));
}
Debug.logInfo("SagePay ccRefund captureTransaction : " + captureTransaction, module);
GenericValue creditCard = null;
try {
creditCard = orderPaymentPreference.getRelatedOne("CreditCard", false);
} catch (GenericEntityException e) {
Debug.logError(e, "Error getting CreditCard for OrderPaymentPreference : " + orderPaymentPreference, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingPaymentUnableToGetCCInfo", locale) + " " + orderPaymentPreference);
}
context.put("creditCard", creditCard);
context.put("captureTransaction", captureTransaction);
List<GenericValue> authTransactions = PaymentGatewayServices.getAuthTransactions(orderPaymentPreference);
EntityCondition authCondition = EntityCondition.makeCondition("paymentServiceTypeEnumId", "PRDS_PAY_AUTH");
List<GenericValue> authTransactions1 = EntityUtil.filterByCondition(authTransactions, authCondition);
GenericValue authTransaction = EntityUtil.getFirst(authTransactions1);
Timestamp authTime = authTransaction.getTimestamp("transactionDate");
Calendar authCal = Calendar.getInstance();
authCal.setTimeInMillis(authTime.getTime());
Timestamp nowTime = UtilDateTime.nowTimestamp();
Calendar nowCal = Calendar.getInstance();
nowCal.setTimeInMillis(nowTime.getTime());
Calendar yesterday = Calendar.getInstance();
yesterday.set(nowCal.get(Calendar.YEAR), nowCal.get(Calendar.MONTH), nowCal.get(Calendar.DATE), 23, 59, 59);
yesterday.add(Calendar.DAY_OF_YEAR, -1);
Map<String, Object> response = null;
if (authCal.before(yesterday)) {
Debug.logInfo("SagePay - Calling Refund for Refund", module);
response = processCardRefundPayment(ctx, context);
} else {
Calendar cal = Calendar.getInstance();
cal.set(nowCal.get(Calendar.YEAR), nowCal.get(Calendar.MONTH), nowCal.get(Calendar.DATE), 23, 49, 59);
if (authCal.before(cal)) {
Debug.logInfo("SagePay - Calling Void for Refund", module);
response = processCardVoidPayment(ctx, context);
} else {
Debug.logInfo("SagePay - Calling Refund for Refund", module);
response = processCardRefundPayment(ctx, context);
}
}
Debug.logInfo("SagePay ccRefund response : " + response, module);
return response;
}
Aggregations