use of org.apache.ofbiz.base.util.StringUtil.StringWrapper in project ofbiz-framework by apache.
the class CatalogUrlSeoTransform method makeProductUrl.
/**
* Make product url according to the configurations.
*
* @return String a catalog url
*/
public static String makeProductUrl(HttpServletRequest request, String productId, String currentCategoryId, String previousCategoryId) {
Delegator delegator = (Delegator) request.getAttribute("delegator");
if (!isCategoryMapInitialed()) {
initCategoryMap(request);
}
String contextPath = request.getContextPath();
StringBuilder urlBuilder = new StringBuilder();
GenericValue product = null;
urlBuilder.append((request.getSession().getServletContext()).getContextPath());
if (urlBuilder.length() == 0 || urlBuilder.charAt(urlBuilder.length() - 1) != '/') {
urlBuilder.append("/");
}
if (UtilValidate.isNotEmpty(productId)) {
try {
product = EntityQuery.use(delegator).from("Product").where("productId", productId).cache().queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, "Error looking up product info for productId [" + productId + "]: " + e.toString(), module);
}
}
if (product != null) {
urlBuilder.append(CatalogUrlServlet.PRODUCT_REQUEST + "/");
}
if (UtilValidate.isNotEmpty(currentCategoryId)) {
List<String> trail = CategoryWorker.getTrail(request);
trail = CategoryWorker.adjustTrail(trail, currentCategoryId, previousCategoryId);
if (!SeoConfigUtil.isCategoryUrlEnabled(contextPath)) {
for (String trailCategoryId : trail) {
if ("TOP".equals(trailCategoryId)) {
continue;
}
urlBuilder.append("/");
urlBuilder.append(trailCategoryId);
}
} else {
if (trail.size() > 1) {
String lastCategoryId = trail.get(trail.size() - 1);
if (!"TOP".equals(lastCategoryId)) {
if (SeoConfigUtil.isCategoryNameEnabled()) {
String categoryName = CatalogUrlSeoTransform.getCategoryIdNameMap().get(lastCategoryId);
if (UtilValidate.isNotEmpty(categoryName)) {
urlBuilder.append(categoryName);
if (product != null) {
urlBuilder.append(URL_HYPHEN);
}
}
}
}
}
}
}
if (UtilValidate.isNotEmpty(productId)) {
if (product != null) {
String productName = product.getString("productName");
productName = SeoUrlUtil.replaceSpecialCharsUrl(productName);
if (UtilValidate.isNotEmpty(productName)) {
urlBuilder.append(productName + URL_HYPHEN);
} else {
ProductContentWrapper wrapper = new ProductContentWrapper(product, request);
StringWrapper alternativeUrl = wrapper.get("ALTERNATIVE_URL", "url");
if (UtilValidate.isNotEmpty(alternativeUrl) && UtilValidate.isNotEmpty(alternativeUrl.toString())) {
productName = SeoUrlUtil.replaceSpecialCharsUrl(alternativeUrl.toString());
if (UtilValidate.isNotEmpty(productName)) {
urlBuilder.append(productName + URL_HYPHEN);
}
}
}
}
try {
urlBuilder.append(productId);
} catch (Exception e) {
urlBuilder.append(productId);
}
}
if (!urlBuilder.toString().endsWith("/") && UtilValidate.isNotEmpty(SeoConfigUtil.getCategoryUrlSuffix())) {
urlBuilder.append(SeoConfigUtil.getCategoryUrlSuffix());
}
return urlBuilder.toString();
}
Aggregations