Skip to content

Instantly share code, notes, and snippets.

@SergioLarios
Created November 13, 2014 14:34
Show Gist options
  • Save SergioLarios/585ed26293c7239bb263 to your computer and use it in GitHub Desktop.
Save SergioLarios/585ed26293c7239bb263 to your computer and use it in GitHub Desktop.
package com.movistar.latam.colportal.helper;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.kernel.xml.Document;
import com.liferay.portal.kernel.xml.Node;
import com.liferay.portal.kernel.xml.SAXReaderUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portlet.asset.model.AssetEntry;
import com.liferay.portlet.asset.model.AssetLink;
import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
import com.liferay.portlet.asset.service.AssetLinkLocalServiceUtil;
import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
import com.liferay.portlet.journal.model.JournalArticle;
import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
import com.movistar.latam.colportal.form.PreferencesForm;
import com.movistar.latam.colportal.journal.OfertaJA;
import com.movistar.latam.colportal.journal.PlanJA;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.portlet.RenderRequest;
public class JournalHelper {
private static Log log = LogFactoryUtil.getLog(JournalHelper.class);
public static OfertaJA getOferta(Long idOferta, RenderRequest renderRequest) {
JournalArticle art = loadJA(idOferta, renderRequest);
OfertaJA result = loadOferta(art, renderRequest);
return result;
}
@SuppressWarnings(UNCHECKED)
public static String getPlanesStrcId(String strcName, long groupId) {
try {
DynamicQuery query = DynamicQueryFactoryUtil.forClass(
DDMStructure.class, PortalClassLoaderUtil.getClassLoader());
query.add(PropertyFactoryUtil.forName(FIELD_NAME).
like(PRE + strcName + POST));
List<DDMStructure> strcts = DDMStructureLocalServiceUtil.dynamicQuery(query);
if (Validator.isNotNull(strcts) && strcts.size() > 0 ) {
return strcts.get(0).getStructureKey();
}
} catch (Exception e) {
log.error(e);
}
return StringPool.BLANK;
}
private static JournalArticle loadJA(Long idOferta, RenderRequest renderRequest) {
JournalArticle art = null;
try {
ThemeDisplay themeDisplay= (ThemeDisplay) renderRequest.
getAttribute(WebKeys.THEME_DISPLAY);
long gId = themeDisplay.getLayout().getGroupId();
art = JournalArticleLocalServiceUtil.getArticle(gId, idOferta.toString());
} catch (Exception e) {
log.error(e);
}
return art;
}
private static OfertaJA loadOferta(JournalArticle art, RenderRequest renderRequest) {
OfertaJA result = null;
try {
if (Validator.isNotNull(art)) {
result = parseOfertaJA(art.getContentByLocale(art.getDefaultLanguageId()));
}
if (Validator.isNotNull(result)) {
result = loadPlanes(result, art, renderRequest);
}
} catch (Exception e) {
log.error(e);
}
return result;
}
private static OfertaJA parseOfertaJA(String content) throws Exception {
OfertaJA result = new OfertaJA();
Document document = SAXReaderUtil.read(content);
Node nombreNode = document.selectSingleNode(String.
format(NODE_TMPL, OfertaJA.NOMBRE));
Node valorNode = document.selectSingleNode(String.
format(NODE_TMPL, OfertaJA.VALOR));
Node valorOfertaNode = document.selectSingleNode(String.
format(NODE_TMPL, OfertaJA.VALOR_OFERTA));
Node mesesOfertaNode = document.selectSingleNode(String.
format(NODE_TMPL, OfertaJA.MESES_OFERTA));
Node idCamaleonNode = document.selectSingleNode(String.
format(NODE_TMPL, OfertaJA.ID_CAMALEON));
result.setNombre(nombreNode.getText());
result.setValor(GetterUtil.getInteger(valorNode.getText(), -1));
result.setValorOferta(GetterUtil.getInteger(valorOfertaNode.getText(), -1));
result.setMesesOferta(GetterUtil.getInteger(mesesOfertaNode.getText(), -1));
result.setIdCamaleon(GetterUtil.getInteger(idCamaleonNode.getText(), -1));
if (Validator.isNotNull(result.getValor()) && result.getValor() > 0) {
result.setValorStr(formatCLP(result.getValor()));
}
if (Validator.isNotNull(result.getValorOferta()) && result.getValorOferta() > 0) {
result.setValorOfertaStr(formatCLP(result.getValorOferta()));
}
if (Validator.isNotNull(result.getMesesOferta()) && result.getMesesOferta() > 0) {
String[] args = new String[2];
args[0] = result.getValorOfertaStr();
args[1] = result.getMesesOferta().toString();
result.setArgs(args);
}
return result;
}
private static String formatCLP(Integer val) {
return DecimalFormat.getCurrencyInstance(new Locale(ES, CL)).
format(val).replace(CH, StringPool.BLANK);
}
private static OfertaJA loadPlanes(OfertaJA result, JournalArticle art,
RenderRequest renderRequest) throws Exception {
List<PlanJA> planes = new ArrayList<PlanJA>();
AssetEntry asset = AssetEntryLocalServiceUtil.fetchEntry(
JournalArticle.class.getName(), art.getResourcePrimKey());
List<AssetLink> links = AssetLinkLocalServiceUtil.getDirectLinks(asset.getEntryId());
String planesStrcId = getPlanesStrcId(
FormHelper.getProp(renderRequest,PreferencesForm.PLANES_STRC_NAME),
art.getGroupId());
for (AssetLink assetLink : links) {
AssetEntry asset2 = AssetEntryLocalServiceUtil.getEntry(assetLink.getEntryId2());
JournalArticle planArt = JournalArticleLocalServiceUtil.
getLatestArticle(asset2.getClassPK());
if (planArt.getStructureId().equals(planesStrcId)) {
PlanJA plan = new PlanJA();
Document document = SAXReaderUtil.read(
planArt.getContentByLocale(planArt.getDefaultLanguageId()));
Node titularNode = document.selectSingleNode(String.
format(NODE_TMPL, PlanJA.TITULAR));
Node listadoNode = document.selectSingleNode(String.
format(NODE_TMPL2, PlanJA.DESCRIPCCION, PlanJA.LISTADO));
Node iconoNode = document.selectSingleNode(String.
format(NODE_TMPL2, PlanJA.ICO, PlanJA.ICO_NORMAL));
plan.setTitular(titularNode.getText());
plan.setListado(listadoNode.getText());
plan.setIcoNormal(iconoNode.getText());
planes.add(plan);
}
}
result.setPlanes(planes);
return result;
}
private static final String UNCHECKED = "unchecked";
private static final String BASE = "/root/dynamic-element[@name='%s']";
private static final String NODE_TMPL = BASE + "/dynamic-content";
private static final String NODE_TMPL2 = BASE + "/dynamic-element[@name='%s']/dynamic-content";
private static final String FIELD_NAME = "name";
private static final String PRE ="%>";
private static final String POST = "<%";
private static final String CH = "Ch$";
private static final String ES = "es";
private static final String CL = "CL";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment