Skip to content

Instantly share code, notes, and snippets.

@SergioLarios
Created October 4, 2016 14:48
Show Gist options
  • Save SergioLarios/c0abe0c22646856dec1413120ade4f28 to your computer and use it in GitHub Desktop.
Save SergioLarios/c0abe0c22646856dec1413120ade4f28 to your computer and use it in GitHub Desktop.
package com.vass;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.struts.BaseStrutsAction;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import com.liferay.portal.kernel.xml.Document;
import com.liferay.portal.kernel.xml.Element;
import com.liferay.portal.kernel.xml.Node;
import com.liferay.portal.kernel.xml.SAXReaderUtil;
import com.liferay.portal.service.ClassNameLocalServiceUtil;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
import com.liferay.portlet.dynamicdatamapping.storage.Field;
import com.liferay.portlet.dynamicdatamapping.storage.Fields;
import com.liferay.portlet.journal.model.JournalArticle;
import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
import com.liferay.portlet.journal.util.JournalConverterUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestAction extends BaseStrutsAction {
@Override
public String execute(HttpServletRequest request,
HttpServletResponse response) throws Exception {
long gId = 20182;
String strcKey = "21331";
try {
List<JournalArticle> jas = JournalArticleServiceUtil.
getArticlesByStructureId(gId, strcKey, -1, -1, null);
for (JournalArticle ja : jas) {
double latestVersion = JournalArticleServiceUtil.getLatestArticle(
gId, ja.getArticleId(), WorkflowConstants.STATUS_APPROVED).getVersion();
if (Validator.equals(ja.getVersion(), latestVersion)) {
System.out.println("Contenido - " + ja.getTitle(ja.getDefaultLanguageId()) +
" - version:" + ja.getVersion());
checkConferenceArticle(ja);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return super.execute(request, response);
}
private void checkConferenceArticle(JournalArticle ja) throws Exception {
long gId = 20182;
String strcKey = "21331";
List<String> profesores = null;
List<Node> ponentes = null;
String content = ja.getContentByLocale(LanguageUtil.getLanguageId(LocaleUtil.SPAIN));
Document document = SAXReaderUtil.read(content);
Element root = document.getRootElement();
profesores = getTextFieldJaRepetible(root, "profesor");
ponentes = getJaRepetibleNodes(root, "Ponentes");
int prIndex = 0;
int ponenteIndx = 0;
for (Node node : ponentes) {
String nPon = getTextFieldJa((Element) node, "nombrePonente");
if (Validator.isBlank(nPon)) {
if (prIndex >= profesores.size()) {
((Element)node).add(createEl("profesor", "ddm-web-content", DEFAULT, ponenteIndx));
}
else {
String pVal = getTextFieldJa((Element)node, "profesor");
if (Validator.isBlank(pVal)) {
((Element)node).add(createEl("profesor", "ddm-web-content", profesores.get(prIndex), ponenteIndx));
prIndex ++;
}
}
}
else {
((Element)node).add(createEl("profesor", "ddm-web-content", DEFAULT, ponenteIndx));
}
ponenteIndx ++;
}
if (prIndex < profesores.size()) {
for (int i = prIndex; i < profesores.size(); i++) {
root.add(createPonente(profesores.get(i), ponenteIndx));
ponenteIndx ++;
}
}
// Delete parent prof
List<Node> ndPrf = root.selectNodes("dynamic-element[@name='profesor']");
if (Validator.isNotNull(ndPrf)) {
for (Node node : ndPrf) {
root.remove(node);
}
}
// ----
long cni = ClassNameLocalServiceUtil.getClassNameId(JournalArticle.class);
DDMStructure strc = DDMStructureLocalServiceUtil.getStructure(gId, cni, strcKey);
Fields existingFields = JournalConverterUtil.getDDMFields(
strc, document.asXML());
for (Field field : existingFields) {
Map<Locale, List<Serializable>> vMap = field.getValuesMap();
List<Serializable> val = vMap.containsKey(LocaleUtil.SPAIN) ? vMap.get(LocaleUtil.SPAIN) : vMap.get(LocaleUtil.US);
List<Serializable> def = new ArrayList<Serializable>();
def.add(StringPool.BLANK);
val = Validator.isNull(val) || val.size() <= 0 || Validator.isNull(val.get(0))? def : val;
Map<Locale, List<Serializable>> newMap = new HashMap<Locale, List<Serializable>>();
newMap.put(LocaleUtil.SPAIN, val);
newMap.put(LocaleUtil.US, val);
field.setValuesMap(newMap);
}
ServiceContext scja = new ServiceContext();
scja.setUserId(ja.getUserId());
scja.setAssetCategoryIds(
AssetCategoryLocalServiceUtil.getCategoryIds(JournalArticle.class.getName(), ja.getResourcePrimKey()));
scja.setScopeGroupId(ja.getGroupId());
scja.setModifiedDate(new Date());
scja.setCreateDate(ja.getCreateDate());
JournalArticle journalArticle = JournalArticleLocalServiceUtil.updateArticle(ja.getUserId(), ja.getGroupId(), ja.getFolderId(), ja.getArticleId(), ja.getVersion(), ja.getTitleMap(), ja.getDescriptionMap(),
JournalConverterUtil.getContent(strc, existingFields), ja.getLayoutUuid(), scja);
System.out.println(journalArticle.getTitle(LocaleUtil.SPAIN) + " -> "+journalArticle.getVersion());
}
private Element createPonente(String string, int ponenteIndx) throws Exception {
return SAXReaderUtil.read(String.format(BASE_PONENTE, String.valueOf(ponenteIndx), string), false).getRootElement();
}
public static String getTextFieldJa(Element root, String fieldName){
try {
Node nodo = root.selectSingleNode(
"dynamic-element[@name='" + fieldName + "']/dynamic-content");
String out = nodo.getText();
return out;
} catch (Exception e) {
System.out.println(fieldName + " no encontrado" );
return StringPool.BLANK;
}
}
public static List<String> getTextFieldJaRepetible(Element root, String fieldName){
List<String> out = new ArrayList<String>();
try {
List<Node> nodos = root.selectNodes(
"dynamic-element[@name='" + fieldName + "']/dynamic-content");
for (Node node : nodos) {
out.add(node.getText());
}
return out;
} catch (Exception e) {
e.printStackTrace();
return new ArrayList<String>();
}
}
public static List<Node> getJaRepetibleNodes(Element root, String fieldName) {
List<Node> out = new ArrayList<Node>();
try {
out = root.selectNodes(
"dynamic-element[@name='" + fieldName + "']");
return out;
} catch (Exception e) {
e.printStackTrace();
return new ArrayList<Node>();
}
}
private static Element createEl(String name, String type, String content, int index) throws Exception {
Element contentEl = SAXReaderUtil.createElement("dynamic-content");
contentEl.addAttribute("language-id", "es_ES");
contentEl.addCDATA(content);
Element result = SAXReaderUtil.createElement("dynamic-element");
result.addAttribute("name", name);
result.addAttribute("type", type);
result.addAttribute("index-type", "");
result.addAttribute("index", "" + index);
result.add(contentEl);
return result;
}
private static final String DEFAULT = "{\"defaultStructure\":{\"key\":\"21321\",\"name\":\"Profesor\"}}";
private static final String BASE_PONENTE =
"<dynamic-element name=\"Ponentes\" type=\"selection_break\" index-type=\"\" index=\"%1$s\">" +
" <dynamic-element name=\"nombrePonente\" index=\"%1$s\" type=\"text\" index-type=\"text\">" +
" <dynamic-content language-id=\"es_ES\"><![CDATA[]]></dynamic-content>" +
" </dynamic-element>" +
" <dynamic-element name=\"descripcionPonente\" index=\"%1$s\" type=\"text_area\" index-type=\"text\">" +
" <dynamic-content language-id=\"es_ES\"><![CDATA[]]></dynamic-content>" +
" </dynamic-element>" +
" <dynamic-element name=\"tipoPonente\" index=\"%1$s\" type=\"list\" index-type=\"\">" +
" <dynamic-content language-id=\"es_ES\"><![CDATA[ST0002]]></dynamic-content>" +
" </dynamic-element>" +
" <dynamic-element name=\"imagenPonente\" index=\"%1$s\" type=\"ddm-web-content\" index-type=\"keyword\">" +
" <dynamic-content language-id=\"es_ES\"><![CDATA[{\"defaultStructure\":{\"key\":\"21305\",\"name\":\"Imagen\"}}]]></dynamic-content>" +
" </dynamic-element>" +
" <dynamic-element name=\"profesor\" index=\"%1$s\" type=\"ddm-web-content\" index-type=\"keyword\">" +
" <dynamic-content language-id=\"es_ES\"><![CDATA[%2$s]]></dynamic-content>" +
" </dynamic-element>" +
"</dynamic-element>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment