use of org.apache.ofbiz.base.util.GeneralRuntimeException in project ofbiz-framework by apache.
the class OrderReturnServices method getReturnItemInitialCost.
// worker method which can be used in screen iterations
public static BigDecimal getReturnItemInitialCost(Delegator delegator, String returnId, String returnItemSeqId) {
if (delegator == null || returnId == null || returnItemSeqId == null) {
throw new IllegalArgumentException("Method parameters cannot contain nulls");
}
Debug.logInfo("Finding the initial item cost for return item : " + returnId + " / " + returnItemSeqId, module);
// the cost holder
BigDecimal itemCost = BigDecimal.ZERO;
// get the return item information
GenericValue returnItem = null;
try {
returnItem = EntityQuery.use(delegator).from("ReturnItem").where("returnId", returnId, "returnItemSeqId", returnItemSeqId).queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, module);
throw new GeneralRuntimeException(e.getMessage());
}
Debug.logInfo("Return item value object - " + returnItem, module);
// check for an orderItem association
if (returnItem != null) {
String orderId = returnItem.getString("orderId");
String orderItemSeqId = returnItem.getString("orderItemSeqId");
if (orderItemSeqId != null && orderId != null) {
Debug.logInfo("Found order item reference", module);
// locate the item issuance(s) for this order item
GenericValue issue = null;
try {
issue = EntityQuery.use(delegator).from("ItemIssuance").where("orderId", orderId, "orderItemSeqId", orderItemSeqId).queryFirst();
} catch (GenericEntityException e) {
Debug.logError(e, module);
throw new GeneralRuntimeException(e.getMessage());
}
if (issue != null) {
Debug.logInfo("Found item issuance reference", module);
// just use the first one for now; maybe later we can find a better way to determine which was the
// actual item being returned; maybe by serial number
GenericValue inventoryItem = null;
try {
inventoryItem = issue.getRelatedOne("InventoryItem", false);
} catch (GenericEntityException e) {
Debug.logError(e, module);
throw new GeneralRuntimeException(e.getMessage());
}
if (inventoryItem != null) {
Debug.logInfo("Located inventory item - " + inventoryItem.getString("inventoryItemId"), module);
if (inventoryItem.get("unitCost") != null) {
itemCost = inventoryItem.getBigDecimal("unitCost");
} else {
Debug.logInfo("Found item cost; but cost was null. Returning default amount (0.00)", module);
}
}
}
}
}
Debug.logInfo("Initial item cost - " + itemCost, module);
return itemCost;
}
use of org.apache.ofbiz.base.util.GeneralRuntimeException in project ofbiz-framework by apache.
the class PartyContentWrapper method getPartyContentAsText.
public static void getPartyContentAsText(String contentId, String partyId, GenericValue party, String partyContentTypeId, Locale locale, String mimeTypeId, Delegator delegator, LocalDispatcher dispatcher, Writer outWriter, boolean cache) throws GeneralException, IOException {
if (partyId == null && party != null) {
partyId = party.getString("partyId");
}
if (delegator == null && party != null) {
delegator = party.getDelegator();
}
if (UtilValidate.isEmpty(mimeTypeId)) {
mimeTypeId = EntityUtilProperties.getPropertyValue("content", "defaultMimeType", "text/html; charset=utf-8", delegator);
}
if (delegator == null) {
throw new GeneralRuntimeException("Unable to find a delegator to use!");
}
// Honor party content over Party entity fields.
GenericValue partyContent;
if (contentId != null) {
partyContent = EntityQuery.use(delegator).from("PartyContent").where("partyId", partyId, "contentId", contentId).cache(cache).queryOne();
} else {
partyContent = getFirstPartyContentByType(partyId, party, partyContentTypeId, delegator);
}
if (partyContent != null) {
// when rendering the product content, always include the Product and ProductContent records that this comes from
Map<String, Object> inContext = new HashMap<String, Object>();
inContext.put("party", party);
inContext.put("partyContent", partyContent);
ContentWorker.renderContentAsText(dispatcher, partyContent.getString("contentId"), outWriter, inContext, locale, mimeTypeId, null, null, cache);
return;
}
if (partyContentTypeId != null) {
String candidateFieldName = ModelUtil.dbNameToVarName(partyContentTypeId);
// first check for a person field
ModelEntity partyPersonModel = delegator.getModelEntity("PartyAndPerson");
if (partyPersonModel != null && partyPersonModel.isField(candidateFieldName)) {
if (party == null) {
party = EntityQuery.use(delegator).from("PartyAndPerson").where("partyId", partyId).cache().queryOne();
}
if (party != null) {
String candidateValue = party.getString(candidateFieldName);
if (UtilValidate.isNotEmpty(candidateValue)) {
outWriter.write(candidateValue);
return;
}
}
}
// next check for group field
ModelEntity partyGroupModel = delegator.getModelEntity("PartyAndGroup");
if (partyGroupModel != null && partyGroupModel.isField(candidateFieldName)) {
if (party == null) {
party = EntityQuery.use(delegator).from("PartyAndGroup").where("partyId", partyId).cache().queryOne();
}
if (party != null) {
String candidateValue = party.getString(candidateFieldName);
if (UtilValidate.isNotEmpty(candidateValue)) {
outWriter.write(candidateValue);
return;
}
}
}
}
}
use of org.apache.ofbiz.base.util.GeneralRuntimeException in project ofbiz-framework by apache.
the class HashCrypt method digestHash64.
public static String digestHash64(String hashType, byte[] bytes) {
if (hashType == null) {
hashType = "SHA";
}
try {
MessageDigest messagedigest = MessageDigest.getInstance(hashType);
messagedigest.update(bytes);
byte[] digestBytes = messagedigest.digest();
StringBuilder sb = new StringBuilder();
sb.append("{").append(hashType).append("}");
sb.append(Base64.encodeBase64URLSafeString(digestBytes).replace('+', '.'));
return sb.toString();
} catch (NoSuchAlgorithmException e) {
throw new GeneralRuntimeException("Error while computing hash of type " + hashType, e);
}
}
use of org.apache.ofbiz.base.util.GeneralRuntimeException in project ofbiz-framework by apache.
the class HashCrypt method pbkdf2HashCrypt.
public static String pbkdf2HashCrypt(String hashType, String salt, String value) {
char[] chars = value.toCharArray();
if (UtilValidate.isEmpty(salt)) {
salt = getSalt();
}
try {
PBEKeySpec spec = new PBEKeySpec(chars, salt.getBytes(UtilIO.getUtf8()), PBKDF2_ITERATIONS, 64 * 4);
SecretKeyFactory skf = SecretKeyFactory.getInstance(hashType);
byte[] hash = Base64.encodeBase64(skf.generateSecret(spec).getEncoded());
String pbkdf2Type = null;
switch(hashType) {
case "PBKDF2WithHmacSHA1":
pbkdf2Type = PBKDF2_SHA1;
break;
case "PBKDF2WithHmacSHA256":
pbkdf2Type = PBKDF2_SHA256;
break;
case "PBKDF2WithHmacSHA384":
pbkdf2Type = PBKDF2_SHA384;
break;
case "PBKDF2WithHmacSHA512":
pbkdf2Type = PBKDF2_SHA512;
break;
default:
pbkdf2Type = PBKDF2_SHA1;
}
StringBuilder sb = new StringBuilder();
sb.append("{").append(pbkdf2Type).append("}");
sb.append(PBKDF2_ITERATIONS).append("$");
sb.append(org.apache.ofbiz.base.util.Base64.base64Encode(salt)).append("$");
sb.append(new String(hash));
return sb.toString();
} catch (InvalidKeySpecException e) {
throw new GeneralRuntimeException("Error while creating SecretKey", e);
} catch (NoSuchAlgorithmException e) {
throw new GeneralRuntimeException("Error while computing SecretKeyFactory", e);
}
}
use of org.apache.ofbiz.base.util.GeneralRuntimeException in project ofbiz-framework by apache.
the class GenericDelegator method getNextSeqIdLong.
/* (non-Javadoc)
* @see org.apache.ofbiz.entity.Delegator#getNextSeqIdLong(java.lang.String, long)
*/
public Long getNextSeqIdLong(String seqName, long staggerMax) {
try {
SequenceUtil sequencer = this.AtomicRefSequencer.get();
if (sequencer == null) {
ModelEntity seqEntity = this.getModelEntity("SequenceValueItem");
sequencer = new SequenceUtil(this.getEntityHelperInfo("SequenceValueItem"), seqEntity, "seqName", "seqId");
if (!AtomicRefSequencer.compareAndSet(null, sequencer)) {
sequencer = this.AtomicRefSequencer.get();
}
}
ModelEntity seqModelEntity = null;
try {
seqModelEntity = getModelReader().getModelEntity(seqName);
} catch (GenericEntityException e) {
Debug.logInfo("Entity definition not found for sequence name " + seqName, module);
}
Long newSeqId = sequencer == null ? null : sequencer.getNextSeqId(seqName, staggerMax, seqModelEntity);
return newSeqId;
} catch (Exception e) {
String errMsg = "Failure in getNextSeqIdLong operation for seqName [" + seqName + "]: " + e.toString() + ". Rolling back transaction.";
Debug.logError(e, errMsg, module);
throw new GeneralRuntimeException(errMsg, e);
}
}
Aggregations