Skip to content

Commit

Permalink
#358: Implement modifications to ensure recaching occurs only for the…
Browse files Browse the repository at this point in the history
… updated languages.
  • Loading branch information
iratigarzon committed Mar 12, 2024
1 parent 675e65a commit 187e799
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import eu.europa.ec.re3gistry2.crudimplementation.RegItemhistoryManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegItemproposedManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegLanguagecodeManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegLocalizationManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegRoleManager;
import eu.europa.ec.re3gistry2.javaapi.cache.RecacheItems;
import eu.europa.ec.re3gistry2.javaapi.handler.RegActionHandler;
Expand Down Expand Up @@ -98,6 +99,7 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
RegItemhistoryManager regItemhistoryManager = new RegItemhistoryManager(entityManager);
RegItemManager regItemManager = new RegItemManager(entityManager);
RegLanguagecodeManager regLanguagecodeManager = new RegLanguagecodeManager(entityManager);
RegLocalizationManager regLocalizationManager = new RegLocalizationManager(entityManager);

// Getting form parameter
String formRegActionUuid = request.getParameter(BaseConstants.KEY_FORM_FIELD_NAME_ACTIONUUID);
Expand Down Expand Up @@ -168,6 +170,7 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
List<RegItemproposed> regItemproposeds = null;
List<RegItemhistory> regItemhistorys = null;
List<RegItem> regItems = null;
List<RegLanguagecode> regLanguageCodes = null;

if (regActionUuid != null && regActionUuid.length() > 0) {

Expand All @@ -187,12 +190,13 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
case BaseConstants.KEY_STATUS_LOCALID_PUBLISHED:
// Getting the list of RegItem contained in the action
regItems = regItemManager.getAll(regAction);
regLanguageCodes = regLocalizationManager.getLanguageCodeByRegAction(regAction);
if (formSubmitAction != null && !regItems.isEmpty()) {
//update RSS
UpdateRSS.updateRSS(regAction, regItems);

//update CACHE
RecacheItems recacheItems = new RecacheItems(entityManager, request, regItems);
RecacheItems recacheItems = new RecacheItems(entityManager, request, regItems, regLanguageCodes);
recacheItems.start();

ResourceBundle systemLocalization = Configuration.getInstance().getLocalization();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import eu.europa.ec.re3gistry2.crudimplementation.RegItemhistoryManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegItemproposedManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegLanguagecodeManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegLocalizationManager;
import eu.europa.ec.re3gistry2.crudimplementation.RegRoleManager;
import eu.europa.ec.re3gistry2.javaapi.cache.RecacheItems;
import eu.europa.ec.re3gistry2.javaapi.handler.RegActionHandler;
Expand All @@ -47,6 +48,7 @@
import eu.europa.ec.re3gistry2.model.RegItemhistory;
import eu.europa.ec.re3gistry2.model.RegItemproposed;
import eu.europa.ec.re3gistry2.model.RegLanguagecode;
import eu.europa.ec.re3gistry2.model.RegLocalization;
import eu.europa.ec.re3gistry2.model.RegRole;
import eu.europa.ec.re3gistry2.model.RegUser;
import eu.europa.ec.re3gistry2.web.utility.SendEmailFromAction;
Expand Down Expand Up @@ -92,14 +94,15 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
RegItemhistoryManager regItemhistoryManager = new RegItemhistoryManager(entityManager);
RegItemManager regItemManager = new RegItemManager(entityManager);
RegLanguagecodeManager regLanguagecodeManager = new RegLanguagecodeManager(entityManager);
RegLocalizationManager regLocalizationManager = new RegLocalizationManager(entityManager);

// Getting form parameter
String formRegActionUuid = request.getParameter(BaseConstants.KEY_FORM_FIELD_NAME_ACTIONUUID);
String formSubmitAction = request.getParameter(BaseConstants.KEY_FORM_FIELD_NAME_SUBMITACTION);

String regActionUuid = request.getParameter(BaseConstants.KEY_REQUEST_ACTION_UUID);
String languageUUID = request.getParameter(BaseConstants.KEY_REQUEST_LANGUAGEUUID);

String formDirectPublish = request.getParameter(BaseConstants.KEY_FORM_FIELD_NAME_DIRECTPUBLISH);
String changelog = request.getParameter(BaseConstants.KEY_FORM_FIELD_NAME_CHANGELOG);
String issueReference = request.getParameter(BaseConstants.KEY_FORM_FIELD_NAME_ISSUEREFERENCE);
Expand Down Expand Up @@ -133,7 +136,7 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
String[] actionManageSystem = {BaseConstants.KEY_USER_ACTION_MANAGESYSTEM};
boolean permissionManageSystem = UserHelper.checkGenericAction(actionManageSystem, currentUserGroupsMap, regItemRegGroupRegRoleMappingManager);

if (permissionManageSystem || StringUtils.equals(formDirectPublish,BaseConstants.KEY_BOOLEAN_STRING_TRUE)) {
if (permissionManageSystem || StringUtils.equals(formDirectPublish, BaseConstants.KEY_BOOLEAN_STRING_TRUE)) {

if (formRegActionUuid != null && formRegActionUuid.length() > 0 && formSubmitAction != null && formSubmitAction.length() > 0) {
// This is a save request
Expand All @@ -144,22 +147,19 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
} else {
regActionHandler.registryManagerAction(formRegActionUuid, regUser);
}


regActionUuid = formRegActionUuid;

}
// This is a view request

// Getting the submitting organization RegRole

RegRole regRoleRegistryManager = regRoleManager.getByLocalId(BaseConstants.KEY_ROLE_REGISTRYMANAGER);
RegGroup regGroupRegistry = regGroupManager.getByLocalid(BaseConstants.KEY_ROLE_REGISTRYMANAGER);
// Getting the list of mapping that contains the specified role
List<RegItemRegGroupRegRoleMapping> tmps = regItemRegGroupRegRoleMappingManager.getAll(regRoleRegistryManager);

tmps = regItemRegGroupRegRoleMappingManager.getAll(regGroupRegistry);


// Getting the action for which the current user is RYM
Set<RegAction> regActions = new HashSet<>();
Expand All @@ -177,6 +177,7 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
List<RegItemproposed> regItemproposeds = null;
List<RegItemhistory> regItemhistorys = null;
List<RegItem> regItems = null;
List<RegLanguagecode> regLanguageCodes = null;

if (regActionUuid != null && regActionUuid.length() > 0) {

Expand All @@ -196,13 +197,13 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
case BaseConstants.KEY_STATUS_LOCALID_PUBLISHED:
// Getting the list of RegItem contained in the action
regItems = regItemManager.getAll(regAction);

regLanguageCodes = regLocalizationManager.getLanguageCodeByRegAction(regAction);
if (formSubmitAction != null && !regItems.isEmpty()) {
//update RSS
UpdateRSS.updateRSS(regAction, regItems);

//update CACHE
RecacheItems recacheItems = new RecacheItems(entityManager, request, regItems);
RecacheItems recacheItems = new RecacheItems(entityManager, request, regItems, regLanguageCodes);
recacheItems.start();

ResourceBundle systemLocalization = Configuration.getInstance().getLocalization();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public interface IRegLocalizationManager{
public List<RegLocalization> getAllWithRelationReference(RegItem regItem) throws Exception ;
public List<RegLocalization> getAll(RegField regField, RegItem regItem, RegAction regAction) throws Exception;
public List<RegLocalization> getAllByRelation(RegRelation relation) throws Exception;
public List<RegLanguagecode> getLanguageCodeByRegAction(RegAction regAction) throws Exception;

public List<RegLocalization> getAllFieldsByValue(String value) throws Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,5 +361,12 @@ public List<RegLocalization> getAllByRelation(RegRelation relation) throws Excep
q.setParameter(SQLConstants.SQL_PARAMETERS_RELATION, relation);
return (List<RegLocalization>) q.getResultList();
}

@Override
public List<RegLanguagecode> getLanguageCodeByRegAction(RegAction regAction) throws Exception{
Query q = this.em.createQuery(SQLConstants.SQL_GET_DISTINCT_LANGUAGE_CODES_BY_REGACTION);
q.setParameter(SQLConstants.SQL_PARAMETERS_ACTION, regAction);
return (List<RegLanguagecode>) q.getResultList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ private SQLConstants() {
public static final String SQL_GET_LOCALIZATION_FIELDS_BY_ITEM_AND_LANGUAGE = "SELECT r FROM RegLocalization r WHERE r.regItem = :regitem AND r.regLanguagecode = :regLanguagecode AND r.regField IS NOT NULL";
public static final String SQL_GET_LOCALIZATION_FIELDS_BY_LANGUAGE_AND_ITEMS = "SELECT r FROM RegLocalization r WHERE r.regLanguagecode = :regLanguagecode AND r.regField IS NOT NULL AND r.regItem IN :regitemList";
public static final String SQL_GET_LOCALIZATION_FIELDS_BY_ITEMCLASS = "SELECT r FROM RegLocalization r WHERE r.regItemclass = :regItemclass";

public static final String SQL_GET_DISTINCT_LANGUAGE_CODES_BY_REGACTION = "SELECT DISTINCT r.regLanguagecode FROM RegLocalization r WHERE r.regAction = :regaction";

public static final String SQL_GET_LOCALIZATION_FIELDS_BY_REGFIELD_ITEMCLASS_HREFNORNULL = "SELECT * FROM RegLocalization r INNER JOIN RegItem ri ON ri.uuid = r.regItem AND ri.regItemclass = :regItemclass WHERE r.regField = :regfield AND r.href IS NOT NULL";
public static final String SQL_GET_LOCALIZATION_BY_RELATION = "SELECT r FROM RegLocalization r WHERE r.regRelationReference = :relation";
public static final String SQL_GET_LOCALIZATION_BY_FIELD = "SELECT r FROM RegLocalization r WHERE r.regField = :regfield AND r.regItem IS NULL";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@ public class RecacheItems extends Thread {
private ItemCache cache;
private final Logger logger;
private final List<RegItem> regItems;
private final List<RegLanguagecode> regLanguageCode;
private final HttpServletRequest request;

private static final String TYPE_REGISTRY = BaseConstants.KEY_ITEMCLASS_TYPE_REGISTRY;
private static final String TYPE_REGISTER = BaseConstants.KEY_ITEMCLASS_TYPE_REGISTER;
private static final String TYPE_ITEM = BaseConstants.KEY_ITEMCLASS_TYPE_ITEM;

public RecacheItems(EntityManager em, HttpServletRequest request, List<RegItem> regItems) {
public RecacheItems(EntityManager em, HttpServletRequest request, List<RegItem> regItems, List<RegLanguagecode> regLanguageCode ) {
this.em = em;
this.request = request;
this.regItems = regItems;
this.regLanguageCode = regLanguageCode;

// Init logger
this.logger = Configuration.getInstance().getLogger();
Expand All @@ -56,7 +58,7 @@ public RecacheItems(EntityManager em, HttpServletRequest request, List<RegItem>
public void run() {
try {
if (!CacheHelper.checkCacheCompleteRunning()) {
recacheItems(regItems);
recacheItems(regItems, regLanguageCode);
}
} catch (Exception e) {
CacheHelper.deleteCacheCompleteRunningFile();
Expand All @@ -68,7 +70,7 @@ public void run() {
}
}

private void recacheItems(List<RegItem> regItems) throws Exception {
private void recacheItems(List<RegItem> regItems, List<RegLanguagecode> regLanguageCode) throws Exception {
CacheHelper.createCacheCompleteRunningFile();
RegLanguagecodeManager languageManager = new RegLanguagecodeManager(em);

Expand All @@ -84,13 +86,13 @@ private void recacheItems(List<RegItem> regItems) throws Exception {

// Iterating on the RegItems
regItems.forEach((regItem) -> {
availableLanguages.forEach((RegLanguagecode languageCode) -> {
regLanguageCode.forEach((RegLanguagecode languageCode) -> {
try {
this.logger.info("RECACHE - regItems: " + regItem.getLocalid() + ", language: " + languageCode.getIso6391code() + " - @ " + new Date());
System.out.println("RECACHE - regItems: " + regItem.getLocalid() + ", language: " + languageCode.getIso6391code() + " - @ " + new Date());

ItemSupplier itemSupplier = new ItemSupplier(em, masterLanguage, languageCode);
updateCacheForItemByUuidCache(regItem, languageCode.getIso6391code(), itemSupplier);
updateCacheForItemByUuid(regItem, languageCode.getIso6391code(), itemSupplier);

switch (regItem.getRegItemclass().getRegItemclasstype().getLocalid()) {
case TYPE_REGISTRY:
Expand All @@ -103,6 +105,10 @@ private void recacheItems(List<RegItem> regItems) throws Exception {
}
break;
case TYPE_ITEM:
registry = regRelationManager.getAllByRegItemSubjectAndPredicate(regItem, relationpredicateManager.get(BaseConstants.KEY_PREDICATE_REGISTER)).get(0).getRegItemObject();
if (!itemsToRecache.contains(registry)) {
itemsToRecache.add(registry);
}
List<RegRelation> collectionList = regRelationManager.getAllByRegItemSubjectAndPredicate(regItem, relationpredicateManager.get(BaseConstants.KEY_PREDICATE_COLLECTION));
if (collectionList != null && !collectionList.isEmpty()) {
RegItem collection = collectionList.get(0).getRegItemObject();
Expand Down Expand Up @@ -148,12 +154,13 @@ private void recacheItems(List<RegItem> regItems) throws Exception {
//if the item has a collection, parent, successor, predecessor update it
itemsToRecache.forEach((regItem) -> {
try {
for (RegLanguagecode languageCode : availableLanguages) {
for (RegLanguagecode languageCode : regLanguageCode) {
this.logger.info("RECACHE - regItems: " + regItem.getLocalid() + ", language: " + languageCode.getIso6391code() + " - @ " + new Date());
System.out.println("RECACHE - regItems: " + regItem.getLocalid() + ", language: " + languageCode.getIso6391code() + " - @ " + new Date());

ItemSupplier itemSupplier = new ItemSupplier(em, masterLanguage, languageCode);
updateCacheForItemByUuidCache(regItem, languageCode.getIso6391code(), itemSupplier);
updateCacheForItemByUuid(regItem, languageCode.getIso6391code(), itemSupplier);

}
} catch (javax.persistence.PersistenceException | org.eclipse.persistence.exceptions.DatabaseException | org.postgresql.util.PSQLException e) {
if (!em.isOpen()) {
Expand Down Expand Up @@ -193,32 +200,5 @@ private Optional<Item> updateCacheForItemByUuid(RegItem regItem, String language
return Optional.of(item);
}

private void updateCacheForItemByUuidCache(RegItem regItem, String language, ItemSupplier itemSupplier) throws Exception {
RegItemhistoryManager regItemHistoryManager = new RegItemhistoryManager(em);
RegItemhistory regItemHistoryFound = null;
Item cached = cache.getByUuid(language, regItem.getUuid());
//Validate if the item is already in cache
if (cached != null) {
//If the item is on cache, check if this item has been modified
regItemHistoryFound = regItemHistoryManager.getMaxVersionByLocalidAndRegItemClass(regItem.getLocalid(), regItem.getRegItemclass());
//Check if the modification is already stored in cached
if (cached.getVersionHistory() != null && regItemHistoryFound != null) {
if (cached.getVersionHistory().size() == regItemHistoryFound.getVersionnumber()) {
//If it is stores, establish regItemHistoryFound as null
regItemHistoryFound = null;
}
}
}
//If an item has been modified or it isn't in cached, find it
if (regItemHistoryFound != null || cached == null) {
Item item = itemSupplier.getItem(regItem);
//If the item was a modification, delete the old one.
if (cached != null) {
cache.remove(language, regItem.getUuid());
}
//Add the item to cache
cache.add(language, item, null);
}
}

}

0 comments on commit 187e799

Please sign in to comment.