use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class UploadContentAndImage method processContentUpload.
public static String processContentUpload(Map<String, Object> passedParams, String suffix, HttpServletRequest request) throws GenericServiceException {
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
Delegator delegator = (Delegator) request.getAttribute("delegator");
HttpSession session = request.getSession();
GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
Map<String, Object> ftlContext = new HashMap<String, Object>();
String contentPurposeString = (String) passedParams.get("contentPurposeString" + suffix);
if (UtilValidate.isEmpty(contentPurposeString)) {
contentPurposeString = (String) passedParams.get("contentPurposeString");
}
List<String> contentPurposeList = StringUtil.split(contentPurposeString, "|");
ftlContext.put("contentPurposeList", contentPurposeList);
String targetOperationString = (String) passedParams.get("targetOperationString" + suffix);
if (UtilValidate.isEmpty(targetOperationString)) {
targetOperationString = (String) passedParams.get("targetOperationString");
}
List<String> targetOperationList = StringUtil.split(targetOperationString, "|");
ftlContext.put("targetOperationList", targetOperationList);
ftlContext.put("userLogin", userLogin);
Object objSequenceNum = passedParams.get("caSequenceNum");
if (objSequenceNum != null) {
if (objSequenceNum instanceof String) {
Long sequenceNum = null;
try {
sequenceNum = Long.valueOf((String) objSequenceNum);
} catch (NumberFormatException e) {
String msg = "Caught an exception : " + e.toString();
Debug.logError(e, msg);
request.setAttribute("_ERROR_MESSAGE_", msg);
List<String> errorMsgList = UtilGenerics.checkList(request.getAttribute("_EVENT_MESSAGE_LIST_"));
if (errorMsgList == null) {
errorMsgList = new LinkedList<String>();
request.setAttribute("errorMessageList", errorMsgList);
}
errorMsgList.add(msg);
return "error";
}
passedParams.put("caSequenceNum", sequenceNum);
}
}
ModelEntity modelEntity = delegator.getModelEntity("ContentAssocDataResourceViewFrom");
List<String> fieldNames = modelEntity.getAllFieldNames();
Map<String, Object> ftlContext2 = new HashMap<String, Object>();
Map<String, Object> ftlContext3 = new HashMap<String, Object>();
for (String keyName : fieldNames) {
Object obj = passedParams.get(keyName + suffix);
ftlContext2.put(keyName, obj);
}
if (Debug.infoOn()) {
Debug.logInfo("[UploadContentStuff]ftlContext2:" + ftlContext2, module);
}
List<Object> errorMessages = new LinkedList<Object>();
Locale loc = Locale.getDefault();
try {
SimpleMapProcessor.runSimpleMapProcessor("component://content/minilang/ContentManagementMapProcessors.xml", "contentIn", ftlContext2, ftlContext3, errorMessages, loc);
SimpleMapProcessor.runSimpleMapProcessor("component://content/minilang/ContentManagementMapProcessors.xml", "contentOut", ftlContext3, ftlContext, errorMessages, loc);
ftlContext3 = new HashMap<String, Object>();
SimpleMapProcessor.runSimpleMapProcessor("component://content/minilang/ContentManagementMapProcessors.xml", "dataResourceIn", ftlContext2, ftlContext3, errorMessages, loc);
SimpleMapProcessor.runSimpleMapProcessor("component://content/minilang/ContentManagementMapProcessors.xml", "dataResourceOut", ftlContext3, ftlContext, errorMessages, loc);
ftlContext3 = new HashMap<String, Object>();
SimpleMapProcessor.runSimpleMapProcessor("component://content/minilang/ContentManagementMapProcessors.xml", "contentAssocIn", ftlContext2, ftlContext3, errorMessages, loc);
SimpleMapProcessor.runSimpleMapProcessor("component://content/minilang/ContentManagementMapProcessors.xml", "contentAssocOut", ftlContext3, ftlContext, errorMessages, loc);
} catch (MiniLangException e) {
throw new GenericServiceException(e.getMessage());
}
ftlContext.put("textData", passedParams.get("textData" + suffix));
byte[] bytes = (byte[]) passedParams.get("imageData" + suffix);
ftlContext.put("imageData", bytes);
if (Debug.infoOn()) {
Debug.logInfo("[UploadContentStuff]ftlContext:" + ftlContext, module);
}
Map<String, Object> ftlResults = null;
try {
ftlResults = dispatcher.runSync("persistContentAndAssoc", ftlContext);
} catch (ServiceAuthException e) {
String msg = e.getMessage();
request.setAttribute("_ERROR_MESSAGE_", msg);
List<String> errorMsgList = UtilGenerics.checkList(request.getAttribute("_EVENT_MESSAGE_LIST_"));
if (Debug.infoOn()) {
Debug.logInfo("[UploadContentStuff]errorMsgList:" + errorMsgList, module);
}
if (Debug.infoOn()) {
Debug.logInfo("[UploadContentStuff]msg:" + msg, module);
}
if (errorMsgList == null) {
errorMsgList = new LinkedList<String>();
request.setAttribute("errorMessageList", errorMsgList);
}
errorMsgList.add(msg);
return "error";
}
if (ServiceUtil.isError(ftlResults)) {
String msg = ServiceUtil.getErrorMessage(ftlResults);
request.setAttribute("_ERROR_MESSAGE_", msg);
List<String> errorMsgList = UtilGenerics.checkList(request.getAttribute("_EVENT_MESSAGE_LIST_"));
if (errorMsgList == null) {
errorMsgList = new LinkedList<String>();
request.setAttribute("errorMessageList", errorMsgList);
}
errorMsgList.add(msg);
return "error";
}
String returnedContentId = (String) ftlResults.get("contentId");
if (Debug.infoOn()) {
Debug.logInfo("returnedContentId:" + returnedContentId, module);
}
request.setAttribute("contentId" + suffix, ftlResults.get("contentId"));
request.setAttribute("caContentIdTo" + suffix, ftlResults.get("contentIdTo"));
request.setAttribute("caContentIdStart" + suffix, ftlResults.get("contentIdTo"));
request.setAttribute("caContentAssocTypeId" + suffix, ftlResults.get("contentAssocTypeId"));
request.setAttribute("caFromDate" + suffix, ftlResults.get("fromDate"));
request.setAttribute("drDataResourceId" + suffix, ftlResults.get("dataResourceId"));
request.setAttribute("caContentId" + suffix, ftlResults.get("contentId"));
String caContentIdTo = (String) passedParams.get("caContentIdTo");
if (UtilValidate.isNotEmpty(caContentIdTo)) {
Map<String, Object> resequenceContext = new HashMap<String, Object>();
resequenceContext.put("contentIdTo", caContentIdTo);
resequenceContext.put("userLogin", userLogin);
try {
ftlResults = dispatcher.runSync("resequence", resequenceContext);
if (ServiceUtil.isError(ftlResults)) {
String errorMessage = ServiceUtil.getErrorMessage(ftlResults);
request.setAttribute("_ERROR_MESSAGE_", errorMessage);
Debug.logError(errorMessage, module);
return "error";
}
} catch (ServiceAuthException e) {
String msg = e.getMessage();
request.setAttribute("_ERROR_MESSAGE_", msg);
List<String> errorMsgList = UtilGenerics.checkList(request.getAttribute("_EVENT_MESSAGE_LIST_"));
if (Debug.infoOn()) {
Debug.logInfo("[UploadContentStuff]errorMsgList:" + errorMsgList, module);
}
if (Debug.infoOn()) {
Debug.logInfo("[UploadContentStuff]msg:" + msg, module);
}
if (errorMsgList == null) {
errorMsgList = new LinkedList<String>();
request.setAttribute("errorMessageList", errorMsgList);
}
errorMsgList.add(msg);
return "error";
}
}
return "success";
}
use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class RenderSubContentCacheTransform method getWriter.
@SuppressWarnings("unchecked")
public Writer getWriter(final Writer out, Map args) {
final Environment env = Environment.getCurrentEnvironment();
final LocalDispatcher dispatcher = FreeMarkerWorker.getWrappedObject("dispatcher", env);
final Delegator delegator = FreeMarkerWorker.getWrappedObject("delegator", env);
final HttpServletRequest request = FreeMarkerWorker.getWrappedObject("request", env);
final HttpServletResponse response = FreeMarkerWorker.getWrappedObject("response", env);
final Map<String, Object> templateRoot = FreeMarkerWorker.createEnvironmentMap(env);
FreeMarkerWorker.getSiteParameters(request, templateRoot);
final Map<String, Object> savedValuesUp = new HashMap<String, Object>();
FreeMarkerWorker.saveContextValues(templateRoot, upSaveKeyNames, savedValuesUp);
FreeMarkerWorker.overrideWithArgs(templateRoot, args);
final GenericValue userLogin = FreeMarkerWorker.getWrappedObject("userLogin", env);
List<Map<String, ? extends Object>> trail = UtilGenerics.checkList(templateRoot.get("globalNodeTrail"));
String contentAssocPredicateId = (String) templateRoot.get("contentAssocPredicateId");
String strNullThruDatesOnly = (String) templateRoot.get("nullThruDatesOnly");
Boolean nullThruDatesOnly = (strNullThruDatesOnly != null && "true".equalsIgnoreCase(strNullThruDatesOnly)) ? Boolean.TRUE : Boolean.FALSE;
String thisSubContentId = (String) templateRoot.get("subContentId");
final boolean directAssocMode = UtilValidate.isNotEmpty(thisSubContentId) ? true : false;
GenericValue val = null;
try {
val = ContentWorker.getCurrentContent(delegator, trail, userLogin, templateRoot, nullThruDatesOnly, contentAssocPredicateId);
} catch (GeneralException e) {
throw new RuntimeException("Error getting current content. " + e.toString());
}
final GenericValue view = val;
String dataResourceId = null;
String subContentIdSub = null;
if (view != null) {
try {
dataResourceId = (String) view.get("drDataResourceId");
} catch (IllegalArgumentException e) {
dataResourceId = (String) view.get("dataResourceId");
}
subContentIdSub = (String) view.get("contentId");
}
// This order is taken so that the dataResourceType can be overridden in the transform arguments.
String subDataResourceTypeId = (String) templateRoot.get("subDataResourceTypeId");
if (UtilValidate.isEmpty(subDataResourceTypeId) && view != null) {
try {
subDataResourceTypeId = (String) view.get("drDataResourceTypeId");
} catch (IllegalArgumentException e) {
// view may be "Content"
}
// TODO: If this value is still empty then it is probably necessary to get a value from
// the parent context. But it will already have one and it is the same context that is
// being passed.
}
String mimeTypeId = ContentWorker.getMimeTypeId(delegator, view, templateRoot);
templateRoot.put("drDataResourceId", dataResourceId);
templateRoot.put("mimeTypeId", mimeTypeId);
templateRoot.put("dataResourceId", dataResourceId);
templateRoot.put("subContentId", subContentIdSub);
templateRoot.put("subDataResourceTypeId", subDataResourceTypeId);
return new Writer(out) {
@Override
public void write(char[] cbuf, int off, int len) {
}
@Override
public void flush() throws IOException {
out.flush();
}
@Override
public void close() throws IOException {
try {
renderSubContent();
FreeMarkerWorker.reloadValues(templateRoot, savedValuesUp, env);
} catch (IOException e) {
throw new IOException(e.getMessage());
}
}
public void renderSubContent() throws IOException {
List<Map<String, ? extends Object>> passedGlobalNodeTrail = UtilGenerics.checkList(templateRoot.get("globalNodeTrail"));
String editRequestName = (String) templateRoot.get("editRequestName");
GenericValue thisView = null;
if (view != null) {
thisView = view;
} else if (passedGlobalNodeTrail.size() > 0) {
Map<String, ? extends Object> map = UtilGenerics.checkMap(passedGlobalNodeTrail.get(passedGlobalNodeTrail.size() - 1));
if (Debug.infoOn()) {
Debug.logInfo("in Render(3), map ." + map, module);
}
if (map != null) {
thisView = (GenericValue) map.get("value");
}
}
String mimeTypeId = (String) templateRoot.get("mimeTypeId");
Locale locale = (Locale) templateRoot.get("locale");
if (locale == null)
locale = Locale.getDefault();
if (UtilValidate.isNotEmpty(editRequestName)) {
String editStyle = getEditStyle();
openEditWrap(out, editStyle);
}
if (thisView != null) {
String contentId = thisView.getString("contentId");
if (contentId != null) {
try {
ContentWorker.renderContentAsText(dispatcher, contentId, out, templateRoot, locale, mimeTypeId, null, null, true);
} catch (GeneralException e) {
Debug.logError(e, "Error rendering content", module);
throw new IOException("Error rendering thisView:" + thisView + " msg:" + e.toString());
}
}
}
if (UtilValidate.isNotEmpty(editRequestName)) {
closeEditWrap(out, editRequestName);
}
}
public void openEditWrap(Writer out, String editStyle) throws IOException {
String divStr = "<div class=\"" + editStyle + "\">";
out.write(divStr);
}
public void closeEditWrap(Writer out, String editRequestName) throws IOException {
String fullRequest = editRequestName;
String contentId = null;
String contentIdTo = null;
String contentAssocTypeId = null;
String mapKey = null;
String fromDate = null;
if (!directAssocMode) {
contentIdTo = (String) templateRoot.get("contentId");
contentAssocTypeId = (String) templateRoot.get("contentAssocTypeId");
mapKey = (String) templateRoot.get("mapKey");
fromDate = (String) templateRoot.get("fromDate");
if (Debug.infoOn())
Debug.logInfo("in Render(0), view ." + view, module);
if (view != null) {
ModelEntity modelEntity = view.getModelEntity();
if (UtilValidate.isEmpty(contentId) && modelEntity.getField("caContentId") != null)
contentId = view.getString("caContentId");
if (UtilValidate.isEmpty(contentId) && modelEntity.getField("contentId") != null)
contentId = view.getString("contentId");
if (UtilValidate.isEmpty(contentIdTo) && modelEntity.getField("caContentIdTo") != null)
contentIdTo = view.getString("caContentIdTo");
if (UtilValidate.isEmpty(contentIdTo) && modelEntity.getField("contentIdTo") != null)
contentIdTo = view.getString("contentIdTo");
if (UtilValidate.isEmpty(contentAssocTypeId) && modelEntity.getField("caContentAssocTypeId") != null)
contentAssocTypeId = view.getString("caContentAssocTypeId");
if (UtilValidate.isEmpty(contentAssocTypeId) && modelEntity.getField("contentAssocTypeId") != null)
contentAssocTypeId = view.getString("contentAssocTypeId");
if (UtilValidate.isEmpty(mapKey) && modelEntity.getField("caMapKey") != null)
mapKey = view.getString("caMapKey");
if (UtilValidate.isEmpty(mapKey) && modelEntity.getField("mapKey") != null)
mapKey = view.getString("mapKey");
if (UtilValidate.isEmpty(fromDate) && modelEntity.getField("caFromDate") != null)
fromDate = view.getString("caFromDate");
if (UtilValidate.isEmpty(fromDate) && modelEntity.getField("fromDate") != null)
fromDate = view.getString("fromDate");
}
} else {
contentId = (String) templateRoot.get("subContentId");
}
if (Debug.infoOn())
Debug.logInfo("in Render(0), contentIdTo ." + contentIdTo, module);
String delim = "?";
if (UtilValidate.isNotEmpty(contentId)) {
fullRequest += delim + "contentId=" + contentId;
delim = "&";
}
if (UtilValidate.isNotEmpty(contentIdTo)) {
fullRequest += delim + "contentIdTo=" + contentIdTo;
delim = "&";
}
if (UtilValidate.isNotEmpty(contentAssocTypeId)) {
fullRequest += delim + "contentAssocTypeId=" + contentAssocTypeId;
delim = "&";
}
if (UtilValidate.isNotEmpty(mapKey)) {
fullRequest += delim + "mapKey=" + mapKey;
delim = "&";
}
if (UtilValidate.isNotEmpty(fromDate)) {
fullRequest += delim + "fromDate=" + fromDate;
delim = "&";
}
if (Debug.infoOn())
Debug.logInfo("in Render(2), contentIdTo ." + contentIdTo, module);
out.write("<a href=\"");
ServletContext servletContext = request.getSession().getServletContext();
RequestHandler rh = (RequestHandler) servletContext.getAttribute("_REQUEST_HANDLER_");
out.append(rh.makeLink(request, response, "/" + fullRequest, false, false, true));
out.write("\">Edit</a>");
out.write("</div>");
}
public String getEditStyle() {
String editStyle = (String) templateRoot.get("editStyle");
if (UtilValidate.isEmpty(editStyle)) {
editStyle = UtilProperties.getPropertyValue("content", "defaultEditStyle");
}
if (UtilValidate.isEmpty(editStyle)) {
editStyle = "buttontext";
}
return editStyle;
}
};
}
use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class PartyHelper method formatPartyNameObject.
public static String formatPartyNameObject(GenericValue partyValue, boolean lastNameFirst) {
if (partyValue == null) {
return "";
}
StringBuilder result = new StringBuilder();
ModelEntity modelEntity = partyValue.getModelEntity();
if (modelEntity.isField("firstName") && modelEntity.isField("middleName") && modelEntity.isField("lastName")) {
if (lastNameFirst) {
if (!UtilFormatOut.checkNull(partyValue.getString("lastName")).isEmpty()) {
result.append(UtilFormatOut.checkNull(partyValue.getString("lastName")));
if (partyValue.getString("firstName") != null) {
result.append(", ");
}
}
result.append(UtilFormatOut.checkNull(partyValue.getString("firstName")));
} else {
result.append(UtilFormatOut.ifNotEmpty(partyValue.getString("firstName"), "", " "));
result.append(UtilFormatOut.ifNotEmpty(partyValue.getString("middleName"), "", " "));
result.append(UtilFormatOut.checkNull(partyValue.getString("lastName")));
}
}
if (modelEntity.isField("groupName") && partyValue.get("groupName") != null) {
result.append(partyValue.getString("groupName"));
}
return result.toString();
}
use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class CategoryContentWrapper method getProductCategoryContentAsText.
public static void getProductCategoryContentAsText(String productCategoryId, GenericValue productCategory, String prodCatContentTypeId, Locale locale, String mimeTypeId, Delegator delegator, LocalDispatcher dispatcher, Writer outWriter, boolean cache) throws GeneralException, IOException {
if (productCategoryId == null && productCategory != null) {
productCategoryId = productCategory.getString("productCategoryId");
}
if (delegator == null && productCategory != null) {
delegator = productCategory.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!");
}
List<GenericValue> categoryContentList = EntityQuery.use(delegator).from("ProductCategoryContent").where("productCategoryId", productCategoryId, "prodCatContentTypeId", prodCatContentTypeId).orderBy("-fromDate").cache(cache).filterByDate().queryList();
GenericValue categoryContent = EntityUtil.getFirst(categoryContentList);
if (categoryContent != null) {
// when rendering the category content, always include the Product Category and ProductCategoryContent records that this comes from
Map<String, Object> inContext = new HashMap<String, Object>();
inContext.put("productCategory", productCategory);
inContext.put("categoryContent", categoryContent);
ContentWorker.renderContentAsText(dispatcher, categoryContent.getString("contentId"), outWriter, inContext, locale, mimeTypeId, null, null, cache);
return;
}
String candidateFieldName = ModelUtil.dbNameToVarName(prodCatContentTypeId);
ModelEntity categoryModel = delegator.getModelEntity("ProductCategory");
if (categoryModel.isField(candidateFieldName)) {
if (productCategory == null) {
productCategory = EntityQuery.use(delegator).from("ProductCategory").where("productCategoryId", productCategoryId).cache().queryOne();
}
if (productCategory != null) {
String candidateValue = productCategory.getString(candidateFieldName);
if (UtilValidate.isNotEmpty(candidateValue)) {
outWriter.write(candidateValue);
return;
}
}
}
}
use of org.apache.ofbiz.entity.model.ModelEntity in project ofbiz-framework by apache.
the class ProductConfigItemContentWrapper method getProductConfigItemContentAsText.
public static void getProductConfigItemContentAsText(String configItemId, GenericValue productConfigItem, String confItemContentTypeId, Locale locale, String mimeTypeId, Delegator delegator, LocalDispatcher dispatcher, Writer outWriter, boolean cache) throws GeneralException, IOException {
if (configItemId == null && productConfigItem != null) {
configItemId = productConfigItem.getString("configItemId");
}
if (delegator == null && productConfigItem != null) {
delegator = productConfigItem.getDelegator();
}
if (UtilValidate.isEmpty(mimeTypeId)) {
mimeTypeId = EntityUtilProperties.getPropertyValue("content", "defaultMimeType", "text/html; charset=utf-8", delegator);
}
GenericValue productConfigItemContent = EntityQuery.use(delegator).from("ProdConfItemContent").where("configItemId", configItemId, "confItemContentTypeId", confItemContentTypeId).orderBy("-fromDate").cache(cache).filterByDate().queryFirst();
if (productConfigItemContent != null) {
// when rendering the product config item content, always include the ProductConfigItem and ProdConfItemContent records that this comes from
Map<String, Object> inContext = new HashMap<>();
inContext.put("productConfigItem", productConfigItem);
inContext.put("productConfigItemContent", productConfigItemContent);
ContentWorker.renderContentAsText(dispatcher, productConfigItemContent.getString("contentId"), outWriter, inContext, locale, mimeTypeId, null, null, cache);
return;
}
String candidateFieldName = ModelUtil.dbNameToVarName(confItemContentTypeId);
ModelEntity productConfigItemModel = delegator.getModelEntity("ProductConfigItem");
if (productConfigItemModel.isField(candidateFieldName)) {
if (productConfigItem == null) {
productConfigItem = EntityQuery.use(delegator).from("ProductConfigItem").where("configItemId", configItemId).cache().queryOne();
}
if (productConfigItem != null) {
String candidateValue = productConfigItem.getString(candidateFieldName);
if (UtilValidate.isNotEmpty(candidateValue)) {
outWriter.write(candidateValue);
return;
}
}
}
}
Aggregations