Skip to content

Commit

Permalink
Updated doGet and minor changes #138
Browse files Browse the repository at this point in the history
  • Loading branch information
unaibrrgn committed Sep 29, 2022
1 parent 567be96 commit 13e8214
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<RegItemproposed> regItemProposeds = regItemproposedManager.getAll(regActionForCache);

RegAction regActionForCache;
List<RegItemproposed> regItemProposeds;
try{
regActionForCache = regActionManager.get(formRegActionUuid);
regItemProposeds = regItemproposedManager.getAll(regActionForCache);
}catch(Exception e){
regItemProposeds = Collections.emptyList();
}

HashSet<String> parentsList = new HashSet <String>();
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item> optItem = getItemByUuid(regItem, languageCode.getIso6391code(), itemSupplier);

} catch (javax.persistence.PersistenceException | org.eclipse.persistence.exceptions.DatabaseException | org.postgresql.util.PSQLException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public Set<String> 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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public interface ItemCache {
public Set<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RegItemhistory> 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<RegItemhistory> 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();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,70 +208,27 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp)
masterLanguage, languageCode);
ItemproposedSupplier itemproposedSupplier = new ItemproposedSupplier(em, masterLanguage, languageCode);

Optional<Item> 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<Item> 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);
Expand Down Expand Up @@ -444,7 +401,7 @@ private Optional<Item> getItemProposedByUriAndStatus(String uri, String itemStat
}

private Optional<Item> 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);
}
Expand Down Expand Up @@ -515,4 +472,124 @@ private Optional<Item> getItemStatusByUri(String uri, String language, StatusSup
cache.add(language, item, null);
return Optional.of(item);
}

private Optional<Item> searchForItemByUuid(String uuid, String lang, ItemSupplier itemSupplier, ItemproposedSupplier itemproposedSupplier, ItemHistorySupplier itemHistorySupplier) {
Optional <Item> 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<Item> searchForItemByURI(String uri, Integer version, String lang, ItemSupplier itemSupplier, ItemproposedSupplier itemproposedSupplier, ItemHistorySupplier itemHistorySupplier) {

Optional<Item> 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<Item> searchForItemByURIStatus(String uri, String lang, Integer version, String status, ItemSupplier itemSupplier, ItemproposedSupplier itemproposedSupplier, ItemHistorySupplier itemHistorySupplier) {

Optional<Item> 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;
}
}

0 comments on commit 13e8214

Please sign in to comment.