diff --git a/sources/Re3gistry2/src/main/java/eu/europa/ec/re3gistry2/web/controller/ControlBody.java b/sources/Re3gistry2/src/main/java/eu/europa/ec/re3gistry2/web/controller/ControlBody.java index 599a9434..fd682f68 100644 --- a/sources/Re3gistry2/src/main/java/eu/europa/ec/re3gistry2/web/controller/ControlBody.java +++ b/sources/Re3gistry2/src/main/java/eu/europa/ec/re3gistry2/web/controller/ControlBody.java @@ -53,10 +53,13 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.NoResultException; +import javax.persistence.Persistence; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @@ -164,17 +167,32 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp request.setAttribute(BaseConstants.ATTRIBUTE_CACHE_KEY, cache); } if (formRegActionUuid != null){ - RegAction regActionForCache = regActionManager.get(formRegActionUuid); - List regItemProposeds = regItemproposedManager.getAll(regActionForCache); - + RegAction regActionForCache; + List regItemProposeds; + try{ + regActionForCache = regActionManager.get(formRegActionUuid); + regItemProposeds = regItemproposedManager.getAll(regActionForCache); + }catch(Exception e){ + regItemProposeds = Collections.emptyList(); + } + + HashSet parentsList = new HashSet (); for (RegItemproposed regItemProposed : regItemProposeds) { - EntityManager emCache = PersistenceFactory.getEntityManagerFactory().createEntityManager(); - CacheAll cacheAll = new CacheAll(emCache, cache, null); - String uuid = regItemProposed.getRegAction().getRegItemRegister().getUuid(); + if(regItemProposed.getRegAction() != null && regItemProposed.getRegAction().getRegItemRegister() != null){ + if(regItemProposed.getRegAction().getRegItemRegister().getRegItemclass() != null && regItemProposed.getRegAction().getRegItemRegister().getRegItemclass().getUuid() != null){ + String uuid = regItemProposed.getRegAction().getRegItemRegister().getRegItemclass().getUuid(); + parentsList.add(uuid); + } + } + } + EntityManager emCache = PersistenceFactory.getEntityManagerFactory().createEntityManager(); + CacheAll cacheAll = new CacheAll(emCache, cache, null); + for (String uuid : parentsList) { +// EntityManager emCache = Persistence.createEntityManagerFactory(BaseConstants.KEY_PROPERTY_PERSISTENCE_UNIT_NAME).createEntityManager(); cacheAll.run(uuid); } } - + //XXX // Getting the submitting organization RegRole diff --git a/sources/Re3gistry2JavaAPI/src/main/java/eu/europa/ec/re3gistry2/javaapi/cache/CacheAll.java b/sources/Re3gistry2JavaAPI/src/main/java/eu/europa/ec/re3gistry2/javaapi/cache/CacheAll.java index 23de7544..78ed96fa 100644 --- a/sources/Re3gistry2JavaAPI/src/main/java/eu/europa/ec/re3gistry2/javaapi/cache/CacheAll.java +++ b/sources/Re3gistry2JavaAPI/src/main/java/eu/europa/ec/re3gistry2/javaapi/cache/CacheAll.java @@ -89,8 +89,14 @@ public void run(String itemclassID) throws Exception { try { logger.trace("CACHE ALL - regItem: " + regItemclass.getLocalid() + " - regItem: " + regItem.getLocalid() + ", language: " + languageCode.getIso6391code() + " - @ " + new Date()); System.out.println("CACHE ALL - regItem: " + regItemclass.getLocalid() + " - regItem: " + regItem.getLocalid() + ", language: " + languageCode.getIso6391code() + " - @ " + new Date()); - + if (!em.isOpen()) { + em = em.getEntityManagerFactory().createEntityManager(); + } + if (!em.getTransaction().isActive()) { + em.getTransaction().begin(); + } ItemSupplier itemSupplier = new ItemSupplier(em, masterLanguage, languageCode); +// cache.remove(languageCode.getIso6391code(), regItemclass.getUuid()); Optional optItem = getItemByUuid(regItem, languageCode.getIso6391code(), itemSupplier); } catch (javax.persistence.PersistenceException | org.eclipse.persistence.exceptions.DatabaseException | org.postgresql.util.PSQLException e) { diff --git a/sources/Re3gistry2JavaAPI/src/main/java/eu/europa/ec/re3gistry2/javaapi/cache/EhCache.java b/sources/Re3gistry2JavaAPI/src/main/java/eu/europa/ec/re3gistry2/javaapi/cache/EhCache.java index 2771d575..f8ecdfae 100644 --- a/sources/Re3gistry2JavaAPI/src/main/java/eu/europa/ec/re3gistry2/javaapi/cache/EhCache.java +++ b/sources/Re3gistry2JavaAPI/src/main/java/eu/europa/ec/re3gistry2/javaapi/cache/EhCache.java @@ -53,6 +53,10 @@ public Set getLanguages() { return Collections.unmodifiableSet(languages); } + +// public Item getByUuid(String language, String uuid) { +// return getByUuid(language, uuid); +// } @Override public Item getByUuid(String language, String uuid) { synchronized (sync) { @@ -83,9 +87,22 @@ public Item getByUuid(String language, String uuid) { } } } - + @Override - public Item getByUrl(String language, String url, Integer version) { + public Item getByUrl(String language, String url, Integer version) { + return getByUrl(language, url, version, null); + } + + public Item getByUrl(String language, String url, Integer version, String itemStatus) { + if (itemStatus == null + || itemStatus.equalsIgnoreCase("valid") + || itemStatus.equalsIgnoreCase("invalid") + || itemStatus.equalsIgnoreCase("superseded") + || itemStatus.equalsIgnoreCase("retired")) { + + } else { + return null; + } synchronized (sync) { // Get configuration properties final Properties properties = Configuration.getInstance().getProperties(); @@ -117,6 +134,19 @@ public Item getByUrl(String language, String url, Integer version) { @Override public void add(String language, Item item, Integer version) { + add(language, item, version, null); + } + + public void add(String language, Item item, Integer version, String itemStatus) { + if (itemStatus == null + || itemStatus.equalsIgnoreCase("valid") + || itemStatus.equalsIgnoreCase("invalid") + || itemStatus.equalsIgnoreCase("superseded") + || itemStatus.equalsIgnoreCase("retired")) { + + } else { + return; + } synchronized (sync) { // Get configuration properties final Properties properties = Configuration.getInstance().getProperties(); diff --git a/sources/Re3gistry2JavaAPI/src/main/java/eu/europa/ec/re3gistry2/javaapi/cache/ItemCache.java b/sources/Re3gistry2JavaAPI/src/main/java/eu/europa/ec/re3gistry2/javaapi/cache/ItemCache.java index 8f312747..15cacad6 100644 --- a/sources/Re3gistry2JavaAPI/src/main/java/eu/europa/ec/re3gistry2/javaapi/cache/ItemCache.java +++ b/sources/Re3gistry2JavaAPI/src/main/java/eu/europa/ec/re3gistry2/javaapi/cache/ItemCache.java @@ -35,6 +35,7 @@ public interface ItemCache { public Set getLanguages(); public Item getByUuid(String language, String uuid); + public Item getByUrl(String language, String url, Integer version, String itemStatus); public Item getByUrl(String language, String url, Integer version); public void add(String language, Item item, Integer version); diff --git a/sources/Re3gistry2JavaAPI/src/main/java/eu/europa/ec/re3gistry2/javaapi/cache/supplier/ItemproposedSupplier.java b/sources/Re3gistry2JavaAPI/src/main/java/eu/europa/ec/re3gistry2/javaapi/cache/supplier/ItemproposedSupplier.java index 18133f46..dbe906ed 100644 --- a/sources/Re3gistry2JavaAPI/src/main/java/eu/europa/ec/re3gistry2/javaapi/cache/supplier/ItemproposedSupplier.java +++ b/sources/Re3gistry2JavaAPI/src/main/java/eu/europa/ec/re3gistry2/javaapi/cache/supplier/ItemproposedSupplier.java @@ -1553,13 +1553,15 @@ private LocalizedProperty getLinksToRelatedItemsProposed(RegField field, private void setVersionAndHistoryItemproposed(RegItemproposed regItemproposed, ContainedItem item) throws Exception { RegItem regItemReference = regItemproposed.getRegItemReference(); - String uri = item.getUri(); - List itemHistory = regItemHistoryManager.getByRegItemReferenceProposed(regItemproposed); - - item.setVersion(new VersionInformation(0, uri + ":" + 0)); - if(regItemReference != null){ - int thisversion = itemHistory.size() + 2; - item.setVersion(new VersionInformation(thisversion, uri + ":" + thisversion)); + String uri = item.getUri(); + List itemHistory = regItemHistoryManager.getByRegItemReferenceProposed(regItemproposed); + + item.setVersion(new VersionInformation(0, null)); +// item.setVersion(new VersionInformation(0, uri + ":" + 0)); + if (regItemReference != null) { +// int thisversion = itemHistory.size() + 2; +// item.setVersion(new VersionInformation(thisversion, uri + ":" + thisversion)); + item.setVersion(new VersionInformation(0, null)); String localId = regItemproposed.getRegItemReference().getLocalid(); diff --git a/sources/Re3gistry2RestAPI/src/main/java/eu/europa/ec/re3gistry2/restapi/ItemsServlet.java b/sources/Re3gistry2RestAPI/src/main/java/eu/europa/ec/re3gistry2/restapi/ItemsServlet.java index 7c4515c1..59dba682 100644 --- a/sources/Re3gistry2RestAPI/src/main/java/eu/europa/ec/re3gistry2/restapi/ItemsServlet.java +++ b/sources/Re3gistry2RestAPI/src/main/java/eu/europa/ec/re3gistry2/restapi/ItemsServlet.java @@ -208,70 +208,27 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) masterLanguage, languageCode); ItemproposedSupplier itemproposedSupplier = new ItemproposedSupplier(em, masterLanguage, languageCode); - Optional optItem; - if (uuid != null) { - try { - optItem = getItemByUuid(uuid, lang, itemSupplier); - } catch (Exception ex) { - try { - optItem = getItemProposedByUuid(uuid, lang, itemproposedSupplier); - } catch (Exception e) { - optItem = getItemHistoryByUuid(uuid, lang, itemHistorySupplier); - } - } - } else { - - Integer version = getVersionFromUri(uri); - if(status != null){ - - if(version == 0){ - - try { - optItem = getItemProposedByUriAndStatus(uri.replace(":" + version, ""), status, itemproposedSupplier); - if(!optItem.isPresent()){ - optItem = getItemByUriAndStatus(uri.replace(":" + version, ""), lang, itemSupplier, status); - } - } catch (Exception e) { - optItem = getItemByUriAndStatus(uri.replace(":" + version, ""), lang, itemSupplier, status); - } - - - }else{ - int sizeHistory = itemHistorySupplier.sizeItemInHistory(uri); - - if (sizeHistory == 0 || sizeHistory + 1 == version) { - try { - optItem = getItemProposedByUriAndStatus(uri.replace(":" + version, ""), status, itemproposedSupplier); - } catch (Exception e) { - optItem = getItemByUriAndStatus(uri.replace(":" + version, ""), lang, itemSupplier, status); - } - } else { - optItem = getItemHistoryByUri(uri, lang, itemHistorySupplier, version); - } - } - }else{ - - //if the item contains no version reference in the URL or if the item contains :0 - if (version == 0) { - optItem = getItemByUri(uri.replace(":" + version, ""), lang, itemSupplier); - - if(!optItem.isPresent()){ - optItem = getItemProposedByUri(uri.replace(":" + version, ""), itemproposedSupplier); - } - - } else { - int sizeHistory = itemHistorySupplier.sizeItemInHistory(uri); - if (sizeHistory == 0 || sizeHistory + 1 == version) { - optItem = getItemByUri(uri.replace(":" + version, ""), lang, itemSupplier); - } else { - optItem = getItemHistoryByUri(uri, lang, itemHistorySupplier, version); - } - } - } + Optional optItem = Optional.empty(); + + Integer version; + try{ + version = getVersionFromUri(uri); + }catch(Exception uriError){ + version = 0; } - + + if(uuid != null && status != null ){ + throw new Exception(); + }else if (uuid != null){ + optItem = searchForItemByUuid(uuid, lang, itemSupplier, itemproposedSupplier, itemHistorySupplier); + }else if (uri != null && status != null){ + optItem = searchForItemByURIStatus(uri, lang, version, status, itemSupplier, itemproposedSupplier, itemHistorySupplier); + } else if (uri != null){ + optItem = searchForItemByURI(uri, version, lang, itemSupplier, itemproposedSupplier, itemHistorySupplier); + } + //try to see if is a status request - if (!optItem.isPresent()) { + if (!optItem.isPresent() && status == null) { StatusSupplier statusSupplier = new StatusSupplier(em, masterLanguage, languageCode); if (uuid != null) { optItem = getItemStatusByUuid(uuid, lang, statusSupplier); @@ -444,7 +401,7 @@ private Optional getItemProposedByUriAndStatus(String uri, String itemStat } private Optional getItemByUriAndStatus(String uri, String language, ItemSupplier itemSupplier, String itemStatus) throws Exception { - Item cached = cache.getByUrl(language, uri, null); + Item cached = cache.getByUrl(language, uri, null, itemStatus); if (cached != null) { return Optional.of(cached); } @@ -515,4 +472,124 @@ private Optional getItemStatusByUri(String uri, String language, StatusSup cache.add(language, item, null); return Optional.of(item); } + + private Optional searchForItemByUuid(String uuid, String lang, ItemSupplier itemSupplier, ItemproposedSupplier itemproposedSupplier, ItemHistorySupplier itemHistorySupplier) { + Optional optItem; + try { + optItem = getItemByUuid(uuid, lang, itemSupplier); + } catch (Exception ex) { + try { + optItem = getItemProposedByUuid(uuid, lang, itemproposedSupplier); + } catch (Exception e) { + try { + optItem = getItemHistoryByUuid(uuid, lang, itemHistorySupplier); + } catch (Exception ex1) { + optItem = Optional.empty(); + } + } + } + return optItem; + } + + private Optional searchForItemByURI(String uri, Integer version, String lang, ItemSupplier itemSupplier, ItemproposedSupplier itemproposedSupplier, ItemHistorySupplier itemHistorySupplier) { + + Optional optItem; + optItem = Optional.empty(); + + if (version == 0) { + + try { + optItem = getItemByUri(uri.replace(":" + version, ""), lang, itemSupplier); + } catch (Exception ex) { + optItem = Optional.empty(); + } + + if (!optItem.isPresent()) { + try { + optItem = getItemProposedByUri(uri.replace(":" + version, ""), itemproposedSupplier); + } catch (Exception ex) { + optItem = Optional.empty(); + } + } + + } else { + int sizeHistory; + sizeHistory = 0; + try { + sizeHistory = itemHistorySupplier.sizeItemInHistory(uri); + } catch (Exception ex) { + sizeHistory = 0; + } + if (sizeHistory != 0 || sizeHistory + 1 != version) { + try { + optItem = getItemHistoryByUri(uri, lang, itemHistorySupplier, version); + } catch (Exception ex) { + optItem = Optional.empty(); + } + } + } + + return optItem; + } + + private Optional searchForItemByURIStatus(String uri, String lang, Integer version, String status, ItemSupplier itemSupplier, ItemproposedSupplier itemproposedSupplier, ItemHistorySupplier itemHistorySupplier) { + + Optional optItem; + + if (version == 0) { + + if (status.equalsIgnoreCase("valid") + || status.equalsIgnoreCase("invalid") + || status.equalsIgnoreCase("superseded") + || status.equalsIgnoreCase("retired")) { + try { + optItem = getItemByUriAndStatus(uri.replace(":" + version, ""), lang, itemSupplier, status); + } catch (Exception ex) { + optItem = Optional.empty(); + } + }else{ + try { + optItem = getItemProposedByUriAndStatus(uri.replace(":" + version, ""), status, itemproposedSupplier); + } catch (Exception ex) { + optItem = Optional.empty(); + } + } + + } else { + int sizeHistory; + try { + sizeHistory = itemHistorySupplier.sizeItemInHistory(uri); + } catch (Exception ex) { + sizeHistory = 0; + } + + if (sizeHistory == 0 || sizeHistory + 1 == version) { + + if (status.equalsIgnoreCase("valid") + || status.equalsIgnoreCase("invalid") + || status.equalsIgnoreCase("superseded") + || status.equalsIgnoreCase("retired")) { + try { + optItem = getItemByUriAndStatus(uri.replace(":" + version, ""), lang, itemSupplier, status); + } catch (Exception ex) { + optItem = Optional.empty(); + } + }else{ + try { + optItem = getItemProposedByUriAndStatus(uri.replace(":" + version, ""), status, itemproposedSupplier); + } catch (Exception e) { + optItem = Optional.empty(); + } + } + } else { + try { + optItem = getItemHistoryByUri(uri, lang, itemHistorySupplier, version); + } catch (Exception ex) { + optItem = Optional.empty(); + } + } + } + + return optItem; + } }