use of org.apache.ofbiz.entity.GenericEntityException in project ofbiz-framework by apache.
the class ImageManagementServices method renameImage.
public static Map<String, Object> renameImage(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
Locale locale = (Locale) context.get("locale");
GenericValue userLogin = (GenericValue) context.get("userLogin");
String imageServerPath = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.path", delegator), context);
String imageServerUrl = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.url", delegator), context);
String productId = (String) context.get("productId");
String contentId = (String) context.get("contentId");
String filenameToUse = (String) context.get("drDataResourceName");
String imageType = filenameToUse.substring(filenameToUse.lastIndexOf('.'));
String imgExtension = filenameToUse.substring(filenameToUse.length() - 3, filenameToUse.length());
String imageUrl = imageServerUrl + "/" + productId + "/" + filenameToUse;
try {
GenericValue productContent = EntityQuery.use(delegator).from("ProductContentAndInfo").where("productId", productId, "contentId", contentId, "productContentTypeId", "IMAGE").queryFirst();
String dataResourceName = (String) productContent.get("drDataResourceName");
String mimeType = filenameToUse.substring(filenameToUse.lastIndexOf('.'));
if (imageType.equals(mimeType)) {
BufferedImage bufImg = ImageIO.read(new File(imageServerPath + "/" + productId + "/" + dataResourceName));
ImageIO.write(bufImg, imgExtension, new File(imageServerPath + "/" + productId + "/" + filenameToUse));
File file = new File(imageServerPath + "/" + productId + "/" + dataResourceName);
if (!file.delete()) {
Debug.logError("File :" + file.getName() + ", couldn't be deleted", module);
}
Map<String, Object> contentUp = new HashMap<>();
contentUp.put("contentId", contentId);
contentUp.put("contentName", filenameToUse);
contentUp.put("userLogin", userLogin);
try {
Map<String, Object> serviceResult = dispatcher.runSync("updateContent", contentUp);
if (ServiceUtil.isError(serviceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
GenericValue content = null;
try {
content = EntityQuery.use(delegator).from("Content").where("contentId", contentId).queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (content != null) {
GenericValue dataResource = null;
try {
dataResource = content.getRelatedOne("DataResource", false);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (dataResource != null) {
Map<String, Object> dataResourceCtx = new HashMap<>();
dataResourceCtx.put("dataResourceId", dataResource.getString("dataResourceId"));
dataResourceCtx.put("objectInfo", imageUrl);
dataResourceCtx.put("dataResourceName", filenameToUse);
dataResourceCtx.put("userLogin", userLogin);
try {
Map<String, Object> serviceResult = dispatcher.runSync("updateDataResource", dataResourceCtx);
if (ServiceUtil.isError(serviceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
}
List<GenericValue> contentAssocList = EntityQuery.use(delegator).from("ContentAssoc").where("contentId", contentId, "contentAssocTypeId", "IMAGE_THUMBNAIL").queryList();
if (contentAssocList.size() > 0) {
for (int i = 0; i < contentAssocList.size(); i++) {
GenericValue contentAssoc = contentAssocList.get(i);
List<GenericValue> dataResourceAssocList = EntityQuery.use(delegator).from("ContentDataResourceView").where("contentId", contentAssoc.get("contentIdTo")).queryList();
GenericValue dataResourceAssoc = EntityUtil.getFirst(dataResourceAssocList);
String drDataResourceNameAssoc = (String) dataResourceAssoc.get("drDataResourceName");
String filenameToUseAssoc = filenameToUse.substring(0, filenameToUse.length() - 4) + "-" + contentAssoc.get("mapKey") + imageType;
String imageUrlAssoc = imageServerUrl + "/" + productId + "/" + filenameToUseAssoc;
BufferedImage bufImgAssoc = ImageIO.read(new File(imageServerPath + "/" + productId + "/" + drDataResourceNameAssoc));
ImageIO.write(bufImgAssoc, imgExtension, new File(imageServerPath + "/" + productId + "/" + filenameToUseAssoc));
File fileAssoc = new File(imageServerPath + "/" + productId + "/" + drDataResourceNameAssoc);
if (!fileAssoc.delete()) {
Debug.logError("File :" + fileAssoc.getName() + ", couldn't be deleted", module);
}
Map<String, Object> contentAssocMap = new HashMap<>();
contentAssocMap.put("contentId", contentAssoc.get("contentIdTo"));
contentAssocMap.put("contentName", filenameToUseAssoc);
contentAssocMap.put("userLogin", userLogin);
try {
Map<String, Object> serviceResult = dispatcher.runSync("updateContent", contentAssocMap);
if (ServiceUtil.isError(serviceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
GenericValue contentAssocUp = null;
try {
contentAssocUp = EntityQuery.use(delegator).from("Content").where("contentId", contentAssoc.get("contentIdTo")).queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (contentAssocUp != null) {
GenericValue dataResourceAssocUp = null;
try {
dataResourceAssocUp = contentAssocUp.getRelatedOne("DataResource", false);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (dataResourceAssocUp != null) {
Map<String, Object> dataResourceAssocMap = new HashMap<>();
dataResourceAssocMap.put("dataResourceId", dataResourceAssocUp.getString("dataResourceId"));
dataResourceAssocMap.put("objectInfo", imageUrlAssoc);
dataResourceAssocMap.put("dataResourceName", filenameToUseAssoc);
dataResourceAssocMap.put("userLogin", userLogin);
try {
Map<String, Object> serviceResult = dispatcher.runSync("updateDataResource", dataResourceAssocMap);
if (ServiceUtil.isError(serviceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
}
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
}
}
}
}
} catch (IOException | IllegalArgumentException | GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
String successMsg = UtilProperties.getMessage(resource, "ProductRenameImageSuccessfully.", locale);
return ServiceUtil.returnSuccess(successMsg);
}
use of org.apache.ofbiz.entity.GenericEntityException in project ofbiz-framework by apache.
the class ReplaceImage method replaceImageToExistImage.
public static Map<String, Object> replaceImageToExistImage(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
Locale locale = (Locale) context.get("locale");
GenericValue userLogin = (GenericValue) context.get("userLogin");
String imageServerPath = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.management.path", delegator), context);
String productId = (String) context.get("productId");
String contentIdExist = (String) context.get("contentIdExist");
String contentIdReplace = (String) context.get("contentIdReplace");
String dataResourceNameExist = (String) context.get("dataResourceNameExist");
String dataResourceNameReplace = (String) context.get("dataResourceNameReplace");
if (UtilValidate.isNotEmpty(dataResourceNameExist)) {
if (UtilValidate.isNotEmpty(contentIdReplace)) {
if (contentIdExist.equals(contentIdReplace)) {
String errMsg = UtilProperties.getMessage(resourceError, "ProductCannotReplaceBecauseBothImagesAreTheSameImage", locale);
Debug.logError(errMsg, module);
return ServiceUtil.returnError(errMsg);
}
} else {
String errMsg = UtilProperties.getMessage(resourceError, "ProductPleaseChooseImageToReplace", locale);
Debug.logError(errMsg, module);
return ServiceUtil.returnError(errMsg);
}
} else {
String errMsg = UtilProperties.getMessage(resourceError, "ProductPleaseChooseReplacementImage", locale);
Debug.logError(errMsg, module);
return ServiceUtil.returnError(errMsg);
}
try {
BufferedImage bufImg = ImageIO.read(new File(imageServerPath + "/" + productId + "/" + dataResourceNameReplace));
ImageIO.write(bufImg, "jpg", new File(imageServerPath + "/" + productId + "/" + dataResourceNameExist));
List<GenericValue> contentAssocReplaceList = EntityQuery.use(delegator).from("ContentAssoc").where("contentId", contentIdReplace, "contentAssocTypeId", "IMAGE_THUMBNAIL").queryList();
if (contentAssocReplaceList.size() > 0) {
for (int i = 0; i < contentAssocReplaceList.size(); i++) {
GenericValue contentAssocReplace = contentAssocReplaceList.get(i);
GenericValue dataResourceAssocReplace = EntityQuery.use(delegator).from("ContentDataResourceView").where("contentId", contentAssocReplace.get("contentIdTo")).queryFirst();
GenericValue contentAssocExist = EntityQuery.use(delegator).from("ContentAssoc").where("contentId", contentIdExist, "contentAssocTypeId", "IMAGE_THUMBNAIL", "mapKey", contentAssocReplace.get("mapKey")).queryFirst();
GenericValue dataResourceAssocExist = EntityQuery.use(delegator).from("ContentDataResourceView").where("contentId", contentAssocExist.get("contentIdTo")).queryFirst();
if (UtilValidate.isNotEmpty(dataResourceAssocExist)) {
BufferedImage bufImgAssocReplace = ImageIO.read(new File(imageServerPath + "/" + productId + "/" + dataResourceAssocReplace.get("drDataResourceName")));
ImageIO.write(bufImgAssocReplace, "jpg", new File(imageServerPath + "/" + productId + "/" + dataResourceAssocExist.get("drDataResourceName")));
} else {
BufferedImage bufImgAssocReplace = ImageIO.read(new File(imageServerPath + "/" + productId + "/" + dataResourceAssocReplace.get("drDataResourceName")));
ImageIO.write(bufImgAssocReplace, "jpg", new File(imageServerPath + "/" + productId + "/" + dataResourceNameExist.substring(0, dataResourceNameExist.length() - 4) + "-" + contentAssocReplace.get("mapKey") + ".jpg"));
}
}
}
GenericValue productContent = EntityQuery.use(delegator).from("ProductContent").where("productId", productId, "contentId", contentIdReplace, "productContentTypeId", "IMAGE").queryFirst();
if (productContent != null) {
Map<String, Object> productContentCtx = new HashMap<>();
productContentCtx.put("productId", productId);
productContentCtx.put("contentId", contentIdReplace);
productContentCtx.put("productContentTypeId", "IMAGE");
productContentCtx.put("fromDate", productContent.get("fromDate"));
productContentCtx.put("userLogin", userLogin);
Map<String, Object> serviceResult = dispatcher.runSync("removeProductContentAndImageFile", productContentCtx);
if (ServiceUtil.isError(serviceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
}
}
} catch (IOException | IllegalArgumentException | GenericEntityException | GenericServiceException e) {
String errMsg = UtilProperties.getMessage(resourceError, "ProductCannotReplaceImage", locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
String successMsg = UtilProperties.getMessage(resource, "ProductReplaceImageSuccessfully", locale);
return ServiceUtil.returnSuccess(successMsg);
}
use of org.apache.ofbiz.entity.GenericEntityException in project ofbiz-framework by apache.
the class WorkEffortKeywordIndex method indexKeywords.
public static void indexKeywords(GenericValue workEffort) throws GenericEntityException {
if (workEffort == null) {
return;
}
Delegator delegator = workEffort.getDelegator();
if (delegator == null) {
return;
}
String workEffortId = workEffort.getString("workEffortId");
String separators = KeywordSearchUtil.getSeparators();
String stopWordBagOr = KeywordSearchUtil.getStopWordBagOr();
String stopWordBagAnd = KeywordSearchUtil.getStopWordBagAnd();
boolean removeStems = KeywordSearchUtil.getRemoveStems();
Set<String> stemSet = KeywordSearchUtil.getStemSet();
Map<String, Long> keywords = new TreeMap<>();
List<String> strings = new LinkedList<>();
int widWeight = 1;
try {
widWeight = EntityUtilProperties.getPropertyAsInteger("workeffort", "index.weight.WorkEffort.workEffortId", 1).intValue();
} catch (Exception e) {
Debug.logWarning("Could not parse weight number: " + e.toString(), module);
}
keywords.put(workEffort.getString("workEffortId").toLowerCase(Locale.getDefault()), Long.valueOf(widWeight));
addWeightedKeywordSourceString(workEffort, "workEffortName", strings);
addWeightedKeywordSourceString(workEffort, "workEffortTypeId", strings);
addWeightedKeywordSourceString(workEffort, "currentStatusId", strings);
if (!"0".equals(EntityUtilProperties.getPropertyValue("workeffort", "index.weight.WorkEffortNoteAndData.noteInfo", "1", delegator))) {
List<GenericValue> workEffortNotes = EntityQuery.use(delegator).from("WorkEffortNoteAndData").where("workEffortId", workEffortId).queryList();
for (GenericValue workEffortNote : workEffortNotes) {
addWeightedKeywordSourceString(workEffortNote, "noteInfo", strings);
}
}
// WorkEffortAttribute
if (!"0".equals(EntityUtilProperties.getPropertyValue("workeffort", "index.weight.WorkEffortAttribute.attrName", "1", delegator)) || !"0".equals(EntityUtilProperties.getPropertyValue("workeffort", "index.weight.WorkEffortAttribute.attrValue", "1", delegator))) {
List<GenericValue> workEffortAttributes = EntityQuery.use(delegator).from("WorkEffortAttribute").where("workEffortId", workEffortId).queryList();
for (GenericValue workEffortAttribute : workEffortAttributes) {
addWeightedKeywordSourceString(workEffortAttribute, "attrName", strings);
addWeightedKeywordSourceString(workEffortAttribute, "attrValue", strings);
}
}
String workEffortContentTypes = EntityUtilProperties.getPropertyValue("workeffort", "index.include.WorkEffortContentTypes", delegator);
for (String workEffortContentTypeId : workEffortContentTypes.split(",")) {
int weight = 1;
try {
weight = EntityUtilProperties.getPropertyAsInteger("workeffort", "index.weight.WorkEffortContent." + workEffortContentTypeId, 1).intValue();
} catch (Exception e) {
Debug.logWarning("Could not parse weight number: " + e.toString(), module);
}
List<GenericValue> workEffortContentAndInfos = EntityQuery.use(delegator).from("WorkEffortContentAndInfo").where("workEffortId", workEffortId, "workEffortContentTypeId", workEffortContentTypeId).queryList();
for (GenericValue workEffortContentAndInfo : workEffortContentAndInfos) {
addWeightedDataResourceString(workEffortContentAndInfo, weight, strings, delegator, workEffort);
List<GenericValue> alternateViews = workEffortContentAndInfo.getRelated("ContentAssocDataResourceViewTo", UtilMisc.toMap("caContentAssocTypeId", "ALTERNATE_LOCALE"), UtilMisc.toList("-caFromDate"), false);
alternateViews = EntityUtil.filterByDate(alternateViews, UtilDateTime.nowTimestamp(), "caFromDate", "caThruDate", true);
for (GenericValue thisView : alternateViews) {
addWeightedDataResourceString(thisView, weight, strings, delegator, workEffort);
}
}
}
for (String str : strings) {
// call process keywords method here
KeywordSearchUtil.processKeywordsForIndex(str, keywords, separators, stopWordBagAnd, stopWordBagOr, removeStems, stemSet);
}
List<GenericValue> toBeStored = new LinkedList<>();
for (Map.Entry<String, Long> entry : keywords.entrySet()) {
if (entry.getKey().length() < 60) {
// ignore very long strings, cannot be stored anyway
GenericValue workEffortKeyword = delegator.makeValue("WorkEffortKeyword", UtilMisc.toMap("workEffortId", workEffort.getString("workEffortId"), "keyword", entry.getKey(), "relevancyWeight", entry.getValue()));
toBeStored.add(workEffortKeyword);
}
}
if (toBeStored.size() > 0) {
if (Debug.verboseOn()) {
Debug.logVerbose("WorkEffortKeywordIndex indexKeywords Storing " + toBeStored.size() + " keywords for workEffortId " + workEffort.getString("workEffortId"), module);
}
delegator.storeAll(toBeStored);
}
}
use of org.apache.ofbiz.entity.GenericEntityException in project ofbiz-framework by apache.
the class WorkEffortSearch method getAllSubWorkEffortIds.
public static void getAllSubWorkEffortIds(String workEffortId, Set<String> workEffortIdSet, Delegator delegator, Timestamp nowTimestamp) {
if (nowTimestamp == null) {
nowTimestamp = UtilDateTime.nowTimestamp();
}
// first make sure the current id is in the Set
workEffortIdSet.add(workEffortId);
// now find all sub-categories, filtered by effective dates, and call this routine for them
try {
// Find WorkEffortAssoc, workEffortAssocTypeId=WORK_EFF_BREAKDOWN
List<GenericValue> workEffortAssocList = EntityQuery.use(delegator).from("WorkEffortAssoc").where("workEffortIdFrom", workEffortId, "workEffortAssocTypeId", "WORK_EFF_BREAKDOWN").cache(true).queryList();
for (GenericValue workEffortAssoc : workEffortAssocList) {
String subWorkEffortId = workEffortAssoc.getString("workEffortIdTo");
if (workEffortIdSet.contains(subWorkEffortId)) {
// if this category has already been traversed, no use doing it again; this will also avoid infinite loops
continue;
}
// do the date filtering in the loop to avoid looping through the list twice
if (EntityUtil.isValueActive(workEffortAssoc, nowTimestamp)) {
getAllSubWorkEffortIds(subWorkEffortId, workEffortIdSet, delegator, nowTimestamp);
}
}
// Find WorkEffort where current workEffortId = workEffortParentId; only select minimal fields to keep the size low
List<GenericValue> childWorkEffortList = EntityQuery.use(delegator).select("workEffortId", "workEffortParentId").from("WorkEffort").where("workEffortParentId", workEffortId).cache(true).queryList();
for (GenericValue childWorkEffort : childWorkEffortList) {
String subWorkEffortId = childWorkEffort.getString("workEffortId");
if (workEffortIdSet.contains(subWorkEffortId)) {
// if this category has already been traversed, no use doing it again; this will also avoid infinite loops
continue;
}
// do the date filtering in the loop to avoid looping through the list twice
getAllSubWorkEffortIds(subWorkEffortId, workEffortIdSet, delegator, nowTimestamp);
}
} catch (GenericEntityException e) {
Debug.logError(e, "Error finding sub-categories for workEffort search", module);
}
}
use of org.apache.ofbiz.entity.GenericEntityException in project ofbiz-framework by apache.
the class WorkEffortWorker method getLowestLevelWorkEfforts.
public static List<GenericValue> getLowestLevelWorkEfforts(Delegator delegator, String workEffortId, String workEffortAssocTypeId, String left, String right) {
if (left == null) {
left = "workEffortIdFrom";
}
if (right == null) {
right = "workEffortIdTo";
}
List<GenericValue> workEfforts = new LinkedList<>();
try {
List<GenericValue> childWEAssocsLevelFirst = EntityQuery.use(delegator).from("WorkEffortAssoc").where(left, workEffortId, "workEffortAssocTypeId", workEffortAssocTypeId).cache(true).queryList();
for (GenericValue childWEAssocLevelFirst : childWEAssocsLevelFirst) {
List<GenericValue> childWEAssocsLevelNext = EntityQuery.use(delegator).from("WorkEffortAssoc").where(left, childWEAssocLevelFirst.get(right), "workEffortAssocTypeId", workEffortAssocTypeId).cache(true).queryList();
while (UtilValidate.isNotEmpty(childWEAssocsLevelNext)) {
List<GenericValue> tempWorkEffortList = new LinkedList<>();
for (GenericValue childWEAssocLevelNext : childWEAssocsLevelNext) {
List<GenericValue> childWEAssocsLevelNth = EntityQuery.use(delegator).from("WorkEffortAssoc").where(left, childWEAssocLevelNext.get(right), "workEffortAssocTypeId", workEffortAssocTypeId).cache(true).queryList();
if (UtilValidate.isNotEmpty(childWEAssocsLevelNth)) {
tempWorkEffortList.addAll(childWEAssocsLevelNth);
}
workEfforts.add(childWEAssocLevelNext);
}
childWEAssocsLevelNext = tempWorkEffortList;
}
workEfforts.add(childWEAssocLevelFirst);
}
} catch (GenericEntityException e) {
Debug.logWarning(e, module);
}
return workEfforts;
}
Aggregations