Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.44 KB | None | 0 0
  1. package com.dshare.kolumbus.gui.deployment.mediaset.listener.sportmediaset;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. import javax.jms.Message;
  9. import javax.jms.MessageListener;
  10.  
  11. import org.apache.activemq.command.ActiveMQTextMessage;
  12. import org.apache.commons.httpclient.HttpClient;
  13. import org.apache.commons.httpclient.HttpMethod;
  14. import org.apache.commons.httpclient.methods.GetMethod;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.beans.factory.annotation.Value;
  19.  
  20. import it.elemedia.kpm3.client.Kpm3Client;
  21. import it.elemedia.kpm3.client.Kpm3ClientException;
  22. import it.elemedia.kpm3.core.model.Categories;
  23. import it.elemedia.kpm3.core.model.Category;
  24. import it.elemedia.kpm3.core.model.Entry;
  25. import it.elemedia.kpm3.core.model.Feed;
  26. import it.elemedia.kpm3.core.model.Link;
  27. import it.elemedia.kpm3.core.model.Status;
  28. import it.elemedia.kpm3.core.model.Type;
  29.  
  30. public class MediaVideoMessageReceiver implements MessageListener {
  31.  
  32.  
  33.  
  34. @Autowired
  35. private Kpm3Client kpm3Client;
  36.  
  37. private static Logger logger = LoggerFactory.getLogger(MediaVideoMessageReceiver.class);
  38.  
  39. @Value("${sportmediaset.video.mediaset.url}")
  40. private String mediaVideoUrl;
  41.  
  42. @Value("${mediavideo.cms.host}")
  43. private String cmsHsot;
  44.  
  45.  
  46. @Value("${mediavideo.hp.max.size}")
  47. private int numberOfHpMediavideoSize;
  48.  
  49. @Value("${mediavideo.start.sequence}")
  50. private int numberOfSequenceDefault;
  51.  
  52.  
  53. @Value("${mediavideo.strillo.template}")
  54. private String templateStrillo;
  55.  
  56. @Value("${mediavideo.category.term}")
  57. private String categoryTermMediavideo;
  58. private final String ZONEMEDIAVIDEO="mediavideo";
  59. private final String CATEGORY_TERM = "categoryTerm";
  60. private final String DRAFT = "draft";
  61. private final String CATEGORY = "category";
  62. private final String TEMPLATE = "template";
  63. private final String ZONE = "zone";
  64. private final String collectionName="sportmediaset";
  65.  
  66. @Override
  67. public void onMessage(Message message) {
  68. logger.info("BEGIN NewsPublishedMessageReceiver.onMessage");
  69.  
  70. if (message instanceof ActiveMQTextMessage) {
  71. try {
  72. ActiveMQTextMessage textMessage = (ActiveMQTextMessage) message;
  73. Map<String, Object> mapJsonResult = textMessage.getProperties();
  74. if (mapJsonResult.size() != 0) {
  75.  
  76. logger.info("id: " + mapJsonResult.get("id"));
  77. Long id = (Long) mapJsonResult.get("id");
  78.  
  79. Map params=new HashMap();
  80. params.put("ids", id);
  81.  
  82. Feed feed = kpm3Client.getContents(params);
  83.  
  84.  
  85. if (feed != null && feed.getEntries() != null
  86. && !feed.getEntries().isEmpty()) {
  87.  
  88. for (Entry entry: feed.getEntries()) {
  89.  
  90. Entry completeEntry = kpm3Client.getContent(entry.getId()).getEntries().get(0);
  91.  
  92. logger.info("enumediavideopag: " + completeEntry.get("enumediavideopag"));
  93. if(completeEntry.get("enumediavideopag")!=null && "Pag.tlv".equalsIgnoreCase((String)completeEntry.get("enumediavideopag"))) {
  94. //Mediavideo attivo
  95. logger.info("update HP Mediavideo sportmediaset");
  96. Map<String, String> paramsHp = new HashMap<String, String>();
  97. paramsHp.put("categoryTerm",categoryTermMediavideo);
  98. Categories categories = kpm3Client.getCategories(collectionName, paramsHp);
  99.  
  100. if (categories != null && categories.getCategories() != null && categories.getCategories().size() > 0) {
  101. Feed feedMediaVideo=getCover(collectionName,categoryTermMediavideo,false,null,null);
  102. if(feedMediaVideo!=null){
  103. List<Entry> listOfEntryInToZone = feedMediaVideo.getEntries();
  104. List<Entry> storicoMediaVideo=listOfEntryInToZone;
  105.  
  106. //pub new strillo
  107. Entry strillo=this.createStrilloFromEntry(completeEntry,"4",listOfEntryInToZone);
  108. if(strillo!=null){
  109. logger.info("STRILLO CREATO CON ID:"+strillo.getKpm3id());
  110.  
  111. //check se esistono strilli nel HP
  112. if(storicoMediaVideo!=null){
  113. if(storicoMediaVideo.size()==0){
  114. //add strillo prima pos
  115. storicoMediaVideo.add(strillo);
  116. }
  117. else{
  118. //shifto lista
  119. storicoMediaVideo=shiftList(storicoMediaVideo,numberOfHpMediavideoSize,strillo);
  120. }
  121. //metodo che verifica se aggiornare sequenza degli strilli
  122. verifyAndUpdateSequenceOfStrilli(storicoMediaVideo);
  123. //pub lista strilli
  124. publishListOfStrilliZoneMediavideo(storicoMediaVideo);
  125. }
  126. }
  127. }
  128. }
  129.  
  130. //TODO: aggiornare la HP aggiungendo in cima l'articolo corrente (ed eventualmente eliminando l'ultimo)
  131.  
  132. //TODO: recuperare i primi 5 strilli nella HP e inviarli al bercgi per chiamare il controller che genera il bacino
  133.  
  134. //TODO: recuperare i successivi 10 strilli nella HP e inviarli al bercgi per chiamare il controller che genera il secondo bacino
  135.  
  136.  
  137. //TODO per ogni strillo presente nella HP chiamare la cgi per generare la singola pagina
  138.  
  139.  
  140. // String collectionName = Utility.getCollectionNameByIdContent(feed.getEntries().get(0).getId());
  141. //
  142. // String path = completeEntry.getLink(Link.REL_ALTERNATE).getPath();
  143. // path = path.replaceAll(".shtml", "");
  144. // String sectPath = path.substring(1, path.lastIndexOf("/"));
  145. // String pagName = path.substring(path.lastIndexOf("/")+1, path.length());
  146. // String mode="p";
  147. // call(String.format(mediaVideoUrl, cmsHsot, collectionName+"/mediavideo", collectionName, sectPath, pagName, completeEntry.getKpm3id(),mode));
  148.  
  149.  
  150. //modifico il valore del flag mediavideo su NO
  151. completeEntry.set("enumediavideopag",null);
  152. kpm3Client.modifyContent(completeEntry);
  153. }
  154. }
  155.  
  156. }
  157. }
  158. } catch (Exception e) {
  159. logger.error(e.getMessage(), e);
  160. }
  161. }
  162.  
  163. logger.info("END NewsPublishedMessageReceiver.onMessage ");
  164.  
  165. }
  166.  
  167. /**
  168. * Metodo che verifica se updatare lista strilli HP mediavideo
  169. * @param storicoMediaVideo
  170. */
  171. private void verifyAndUpdateSequenceOfStrilli(List<Entry> storicoMediaVideo) {
  172. List<Entry> updatedStrilliList=new ArrayList<Entry>();
  173. boolean update=false;
  174. //controllo se primo strilli(pag 201) contiene sequenza=2, se si aggiornare tutti strilli con num sequenza agg
  175. if (storicoMediaVideo!=null && storicoMediaVideo.size()>0) {
  176. Entry firstStrillo=storicoMediaVideo.get(0);
  177. if(firstStrillo!=null && firstStrillo.getCategories("true")!=null && firstStrillo.getCategories("true").size()>0){
  178. String sequence=firstStrillo.getCategories("true").get(0).getSequence();
  179. if("2".equalsIgnoreCase(sequence)){
  180. update=true;
  181. }
  182. }
  183. }
  184. //se update =true aggiorno lista strilli
  185. if(update){
  186. int startSequence=numberOfSequenceDefault;
  187. for (Entry strlToUpdate : storicoMediaVideo) {
  188. if(strlToUpdate!=null && strlToUpdate.getCategories("true")!=null && strlToUpdate.getCategories("true").size()>0){
  189. //add 50 al proprio num di sequenza
  190. String sequence=strlToUpdate.getCategories("true").get(0).getSequence();
  191.  
  192. int nowSequence = Integer.parseInt(sequence);
  193. nowSequence=nowSequence+startSequence;
  194. String updateSequence=Integer.toString(nowSequence);
  195. strlToUpdate.getCategories("true").get(0).setSequence(updateSequence);
  196. updatedStrilliList.add(strlToUpdate);
  197.  
  198. }
  199. }
  200.  
  201. //update entry Strilli
  202. updateStrilli( updatedStrilliList);
  203. }
  204.  
  205.  
  206. }
  207.  
  208.  
  209. /**
  210. * Metodo che aggiorna strilli nel HP mediavideo
  211. * @param storicoMediaVideo
  212. */
  213. private void updateStrilli(List<Entry> storicoMediaVideo) {
  214. for (Entry strlToUpdate : storicoMediaVideo) {
  215. Entry res =null;
  216. try{
  217. strlToUpdate.setUpdated(strlToUpdate.getCreated());
  218. res=kpm3Client.modifyContent(strlToUpdate);
  219.  
  220. }catch(Exception e){
  221. logger.info("errore nella pubblciazone"+ e);
  222. }
  223. if (res != null) {
  224. logger.info("Success updated strillo nella hp mediavideo: "+res.getId());
  225. }else{
  226. logger.error("Error when updated strillo nella hp mediavideo: "+res.getKpm3id());
  227. }
  228. }
  229.  
  230. }
  231.  
  232. /**
  233. * Rimuovo strilli, per non visualizzarli nel hp mediavideo
  234. * @param storicoMediaVideo
  235. */
  236. private void clearZoneMediaVideoAndRemoveStrilli(List<Entry> storicoMediaVideo){
  237. for (Entry strlToRemove : storicoMediaVideo) {
  238. Entry res =null;
  239. try{
  240. res=kpm3Client.deleteContent(strlToRemove.getId());
  241.  
  242. }catch(Exception e){
  243. logger.info("errore nella pubblciazone"+ e);
  244. }
  245. if (res != null) {
  246. logger.info("Success removed strillo nella hp mediavideo: "+strlToRemove.getId());
  247. }else{
  248. logger.error("Error when removed strillo nella hp mediavideo: "+strlToRemove.getKpm3id());
  249. }
  250. }
  251. }
  252.  
  253.  
  254. /**
  255. * Metodo che pubblica lista storico mediavideo
  256. * @param storicoMediaVideo
  257. */
  258. private void publishListOfStrilliZoneMediavideo(List<Entry> storicoMediaVideo) {
  259. for (Entry strillo : storicoMediaVideo) {
  260. Entry res =null;
  261. try{
  262. //setto data update, per rippubblicare strillo
  263. strillo.setUpdated(strillo.getCreated());
  264. res=kpm3Client.modifyContent(strillo);
  265. }catch(Exception e){
  266. logger.info("errore nella pubblciazone"+ e);
  267. }
  268. if (res != null) {
  269. logger.info("Success publish strillo nella hp mediavideo: "+strillo.getId());
  270. }else{
  271. logger.error("Error when publish strillo nella hp mediavideo: "+strillo.getKpm3id());
  272. }
  273. }
  274.  
  275. }
  276.  
  277. /**
  278. * Metodo che ritorna tutti i feed all'internzo della zona
  279. * @param collectionName
  280. * @param category
  281. * @param draft
  282. * @param template
  283. * @param zone
  284. * @return
  285. * @throws Exception
  286. */
  287. private Feed getCover(String collectionName, String category, boolean draft,
  288. String template, String zone) throws Exception {
  289.  
  290. Feed feed = null;
  291.  
  292. try {
  293. HashMap<String, String> map = new HashMap<String, String>();
  294. map.put(CATEGORY_TERM, category);
  295. map.put(DRAFT, Boolean.toString(draft));
  296. map.put(CATEGORY, Boolean.TRUE.toString());
  297.  
  298. if(template!=null && !"".equals(template)
  299. && zone!=null && !"".equals(zone)) {
  300. map.put(TEMPLATE, template);
  301. map.put(ZONE, zone);
  302. }
  303. feed = kpm3Client.getContents(collectionName, map);
  304.  
  305. } catch (Exception ex) {
  306. throw new Exception("Impossibile ottenere il contenuto",ex);
  307. }
  308.  
  309. return feed;
  310. }
  311.  
  312. /**
  313. * Metodo che ritorna entry di tipo strillo,partendo da entry di tipo articolo
  314. * @param e entry
  315. * @return
  316. */
  317. private Entry createStrilloFromEntry(Entry e,String idBlock,List<Entry> listOfEntryInToZone){
  318. //logica gestione sequenza degli strilli
  319. String sequenceOfStrillo=Integer.toString(this.numberOfSequenceDefault);
  320. //se esiste gia contenuto , calcolo sequenza corretta
  321. if(listOfEntryInToZone!=null && listOfEntryInToZone.size()>0){
  322. Entry firstStrillo=listOfEntryInToZone.get(0);
  323. if(firstStrillo!=null && firstStrillo.getCategories("true")!=null && firstStrillo.getCategories("true").size()>0){
  324.  
  325. Category cat=firstStrillo.getCategories("true").get(0);
  326. //get ultimo strillo con sequenza minore(pagina 201) e rimuovo 1 per inserire il nuovo strillo in prima pos nel HP
  327. String tempSequnce=cat.getSequence();
  328. int initSequence = Integer.parseInt(tempSequnce);
  329. int newValueOfSequence=initSequence-1;
  330. sequenceOfStrillo=Integer.toString(newValueOfSequence);
  331. }
  332.  
  333. }
  334.  
  335.  
  336. Entry strillo=new Entry();
  337. strillo.setType(Type.TYPE_STRILLO);
  338. if(e.get("titlemv")!=null){
  339. strillo.setTitle(e.get("titlemv").toString());
  340. }else{
  341. strillo.setTitle(e.getTitle());
  342. }
  343. strillo.setSummary(e.getSummary());
  344. Category cat =null;
  345. try{
  346. cat = getCategory(categoryTermMediavideo);
  347. }
  348. catch(Exception ecc){
  349. logger.info("Errore nella creazione dellac categoria dello strillo "+ecc.getMessage());
  350. }
  351.  
  352. if(cat!=null && e.getCategories()!=null && e.getCategories("true")!=null && e.getCategories("true").size()>0){
  353. cat.setTemplate(templateStrillo);
  354. cat.setPrimary("true");
  355. cat.setZone(this.ZONEMEDIAVIDEO);
  356. cat.setBlock(idBlock);
  357. cat.setEnabled("true");
  358. cat.setSequence(sequenceOfStrillo);
  359. cat.setPubFromDate(getKolumbusPubFromDateFromCategory(e.getCategories("true").get(0)));
  360. strillo.addCategory(cat);
  361. strillo.setCreated(getKolumbusPubFromDateFromCategory(e.getCategories("true").get(0)));
  362. strillo.setDraft(false);
  363. strillo.setStatus(Status.PUBBLICATO);
  364.  
  365. Link redEdit=e.getLink(Link.REL_EDIT);
  366. if(redEdit==null){
  367. logger.error("ERRORE GRAVE il Link rel-edit is empty from entry"+e.getKpm3id());
  368. }
  369. //creo link con entry principale
  370. Link linkEdit=new Link();
  371. linkEdit.setRel(Link.REL_RELATED);
  372. linkEdit.setHref(redEdit.getHref());
  373. linkEdit.setCollection("sportmediaset");
  374. linkEdit.setPath(this.CATEGORY_TERM);
  375. linkEdit.setPos(1);
  376. //add link with principal entry
  377. strillo.addLink(linkEdit);
  378.  
  379. try {
  380. return kpm3Client.publishContent(strillo);
  381. } catch (Kpm3ClientException ecc) {
  382. logger.info("Errore nella creazione dello strillo "+ecc.getMessage());
  383. }
  384. }
  385.  
  386.  
  387. return null;
  388. }
  389.  
  390. /**
  391. * Metodo che shifta lista verso sx
  392. * @param listOfEntryInToZone
  393. * @param maxSizeOfList
  394. * @param strilloToPub
  395. * @return
  396. */
  397. private List<Entry> shiftList(List<Entry> listOfEntryInToZone,int maxSizeOfList,Entry strilloToPub){
  398. List<Entry> storico=new ArrayList<Entry>();
  399. List<Entry> listEntryToRemove=new ArrayList<Entry>();
  400.  
  401. //add new strillo to list
  402. storico.add(0, strilloToPub);
  403. storico.addAll(1, listOfEntryInToZone.subList(0,listOfEntryInToZone.size()));
  404.  
  405. //se numero strilli = max,allora shift a sx eliminando ultimo
  406. if(storico.size()>maxSizeOfList){
  407. List<Entry> storicoUpdated=new ArrayList<Entry>();
  408. //copio tutti gli strilli da rimuovere
  409. storicoUpdated.addAll(0,storico.subList(0,maxSizeOfList));
  410.  
  411. //creo lista con elementi da rimuovere
  412. listEntryToRemove.addAll(0,storico.subList(maxSizeOfList,maxSizeOfList+1));
  413. storico=storicoUpdated;
  414. //rimuovo lista di strilli in eccesso
  415. clearZoneMediaVideoAndRemoveStrilli(listEntryToRemove);
  416. }
  417. return storico;
  418. }
  419.  
  420. public Integer call(String url) throws Exception {
  421. logger.info("calling: " + url);
  422. String body = "";
  423. HttpClient httpClient = new HttpClient();
  424. HttpMethod httpMethod = new GetMethod(url);
  425. int responseCode = httpClient.executeMethod(httpMethod);
  426. byte[] responseBody = httpMethod.getResponseBody();
  427. body = new String(responseBody, "UTF-8");
  428. httpMethod.releaseConnection();
  429. logger.info("response : " + responseCode);
  430. return responseCode;
  431. }
  432.  
  433. /**
  434. * Metodo che trasforma le date lette dal xml in date accettate da kolumbus
  435. * @param dateToParse
  436. * @return
  437. */
  438. private String getKolumbusPubFromDateFromCategory(Category cat){
  439. return cat.getPubFromDate();
  440. }
  441.  
  442.  
  443. /**
  444. * Metodo che restituisce categoria
  445. * @param categoryToPublish
  446. * @return
  447. * @throws Exception
  448. */
  449. private Category getCategory(String categoryToPublish) throws Exception{
  450. Map<String, String> params=new HashMap<String, String>();
  451. params.put("categoryTerm", categoryToPublish);
  452. Categories newCategories = null;
  453. try {
  454. String collection = categoryToPublish.split("/")[1];
  455. newCategories = kpm3Client.getCategories(collection, params);
  456. } catch (Kpm3ClientException e) {
  457. logger.error(e.getMessage(),e);
  458. }
  459.  
  460. if (newCategories != null && newCategories.getCategories() != null && newCategories.getCategories().size() > 0) {
  461. return (Category) newCategories.getCategories().get(0);
  462. }
  463. throw new Exception("category not found => "+categoryToPublish);
  464. }
  465. // /**
  466. // * Riordino la sequenza di tutte le entry del Hp, per tenerla aggiornata
  467. // * @param listOfEntryInToZone
  468. // * @return
  469. // */
  470. // private List<Entry> reorderSequence(List<Entry> listOfEntryInToZone) {
  471. // List<Entry> tempListStorico=new ArrayList<Entry>();
  472. //
  473. // for(Entry entry:listOfEntryInToZone){
  474. // if(entry.getCategories()!=null && entry.getCategories().size()>0){
  475. // Category cat = entry.getCategories().get(0);
  476. // int nowSequence=Integer.parseInt(cat.getSequence())+2;
  477. // cat.setSequence(Integer.toString(nowSequence));
  478. // List<Category> list=new ArrayList<Category>();
  479. // list.add(cat);
  480. // entry.setCategories(list);
  481. // tempListStorico.add(entry);
  482. // }
  483. // }
  484. // return tempListStorico;
  485. // }
  486. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement