Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 28251 replacing api calls #28544

Merged
merged 54 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
0c88bd7
#27979
fabrizzio-dotCMS May 3, 2024
e5998e7
Merge branch 'master' into issue-27979-lang-vars-backend-take-2
fabrizzio-dotCMS May 3, 2024
832075c
#27979
fabrizzio-dotCMS May 7, 2024
d264292
Merge branch 'master' into issue-27979-lang-vars-backend-take-2
fabrizzio-dotCMS May 7, 2024
4eec0b2
#27979 cache invalidation
fabrizzio-dotCMS May 7, 2024
34a42b8
Merge branch 'master' into issue-27979-lang-vars-backend-take-2
fabrizzio-dotCMS May 7, 2024
9b13cc3
#27979 api calls replaced
fabrizzio-dotCMS May 7, 2024
c490b99
Merge branch 'master' into issue-27979-lang-vars-backend-take-2
fabrizzio-dotCMS May 8, 2024
e1d2497
#27979 reverting changes to keep smaller pr
fabrizzio-dotCMS May 8, 2024
bf33c2d
Merge branch 'master' into issue-27979-lang-vars-backend-take-2
fabrizzio-dotCMS May 8, 2024
9395a56
#27979 adding postman for the new endpoint
fabrizzio-dotCMS May 8, 2024
38bca4e
Merge branch 'issue-27979-lang-vars-backend-take-2' of https://github…
fabrizzio-dotCMS May 8, 2024
52a36ba
#27979 adding more test
fabrizzio-dotCMS May 8, 2024
96daa8c
Merge branch 'master' into issue-27979-lang-vars-backend-take-2
fabrizzio-dotCMS May 8, 2024
db1e5d9
#27979 optimize imports
fabrizzio-dotCMS May 8, 2024
08629c3
Merge branch 'issue-27979-lang-vars-backend-take-2' of https://github…
fabrizzio-dotCMS May 8, 2024
547d2b1
#27979
fabrizzio-dotCMS May 9, 2024
a336d8d
#27979 fix postman
fabrizzio-dotCMS May 9, 2024
be1c347
#28251
fabrizzio-dotCMS May 9, 2024
6394ea4
Merge branch 'master' into issue-27979-lang-vars-backend-take-2
fabrizzio-dotCMS May 9, 2024
4be1309
#27979
fabrizzio-dotCMS May 9, 2024
c88af63
Merge branch 'issue-27979-lang-vars-backend-take-2' into issue-28251-…
fabrizzio-dotCMS May 9, 2024
58675f4
#27979 doc
fabrizzio-dotCMS May 9, 2024
b6f4d9f
Merge branch 'issue-27979-lang-vars-backend-take-2' into issue-28251-…
fabrizzio-dotCMS May 9, 2024
69aa0e4
#27979
fabrizzio-dotCMS May 9, 2024
845c4cc
Merge branch 'issue-27979-lang-vars-backend-take-2' into issue-28251-…
fabrizzio-dotCMS May 9, 2024
7546f83
#28251
fabrizzio-dotCMS May 10, 2024
645a711
#27979
fabrizzio-dotCMS May 10, 2024
afa8450
Merge branch 'issue-27979-lang-vars-backend-take-2' into issue-28251-…
fabrizzio-dotCMS May 10, 2024
f81cc3c
#27979 feedback
fabrizzio-dotCMS May 10, 2024
1d16bf5
Merge branch 'issue-27979-lang-vars-backend-take-2' into issue-28251-…
fabrizzio-dotCMS May 11, 2024
25f468e
Merge branch 'master' into issue-27979-lang-vars-backend-take-2
fabrizzio-dotCMS May 13, 2024
bac0aad
#27979
fabrizzio-dotCMS May 13, 2024
cc034de
Merge branch 'issue-27979-lang-vars-backend-take-2' of https://github…
fabrizzio-dotCMS May 13, 2024
effa4a2
#27979 sonar feedback
fabrizzio-dotCMS May 13, 2024
5fae363
Merge branch 'master' into issue-27979-lang-vars-backend-take-2
fabrizzio-dotCMS May 13, 2024
a2ab8ba
Merge branch 'issue-27979-lang-vars-backend-take-2' into issue-28251-…
fabrizzio-dotCMS May 13, 2024
72d60dc
#27979 restore file
fabrizzio-dotCMS May 13, 2024
e6684ca
Merge branch 'issue-27979-lang-vars-backend-take-2' into issue-28251-…
fabrizzio-dotCMS May 13, 2024
542a193
#28251 merge from master
fabrizzio-dotCMS May 13, 2024
dfa9c1a
#28251
fabrizzio-dotCMS May 13, 2024
4d350f4
#28251
fabrizzio-dotCMS May 14, 2024
d7eabb7
#28251 improve cache to support per key lookups
fabrizzio-dotCMS May 16, 2024
184c328
#28251 fixing queries and adding feature flag conditionals + sonar fe…
fabrizzio-dotCMS May 17, 2024
53384c9
Merge branch 'master' into issue-28251-api-calls-take-2
fabrizzio-dotCMS May 17, 2024
ef628e3
#28251 dupe key was breaking endpoint. merge function added
fabrizzio-dotCMS May 17, 2024
74f1e6f
Merge branch 'master' into issue-28251-api-calls-take-2
fabrizzio-dotCMS May 17, 2024
7371d5a
#28251
fabrizzio-dotCMS May 17, 2024
d9d9be2
Merge branch 'issue-28251-api-calls-take-2' of https://github.com/dot…
fabrizzio-dotCMS May 17, 2024
837794a
#28251 adjust in the deprecated annotation
fabrizzio-dotCMS May 17, 2024
be5ffad
#28251 adding doc
fabrizzio-dotCMS May 17, 2024
87bd3d2
Merge branch 'master' into issue-28251-api-calls-take-2
fabrizzio-dotCMS May 17, 2024
b128b14
#28251 adding doc
fabrizzio-dotCMS May 17, 2024
6a35c9b
Merge branch 'master' into issue-28251-api-calls-take-2
fabrizzio-dotCMS May 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.liferay.portal.model.User;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
* Provides access to Language Variable objects in the system, which allow you to associate a key to
Expand Down Expand Up @@ -146,6 +147,15 @@ public List<KeyValue> getAllLanguageVariablesKeyStartsWith(final String key, fin
*/
List<LanguageVariable> findVariables(final long langId) throws DotDataException;

/**
* Returns an Optional of {@link LanguageVariable} matching the specified language ID and key.
* @param languageId - The ID of the language that the variable was created for.
* @param key - The key to the Language Variable that starts with.
* @return Optional of Language Variables.
* @throws DotDataException - If an error occurs while retrieving the Language Variables.
*/
Optional<LanguageVariable> findVariable(final long languageId, final String key) throws DotDataException;

/**
* Returns a list of {@link LanguageVariable} that the key starts with the specified key and
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.vavr.Lazy;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -171,6 +172,25 @@ public List<LanguageVariable> findVariables(final long langId) throws DotDataExc
});
}

/**
* {@inheritDoc}
*/
@CloseDBIfOpened
@Override
public Optional<LanguageVariable> findVariable(final long languageId, final String key) throws DotDataException {
final LanguageVariableFactory factory = FactoryLocator.getLanguageVariableFactory();
final ContentType contentType = langVarContentType.get();
final LanguageCache languageCache = CacheLocator.getLanguageCache();
return languageCache.ifPresentGetOrElseFetch(languageId, key, ()->{
try {
return factory.findVariables(contentType, languageId, 0, 0, null);
} catch (DotDataException e) {
Logger.error(this, "Error finding language variables", e);
return List.of();
}
});
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,22 @@ public List<LanguageVariable> findVariables(final ContentType contentType,
@Override
public List<LanguageVariableExt> findVariablesForPagination(ContentType contentType, int offset,
int limit, String orderBy) throws DotDataException {
// START-NOSCAN
String select = "select contentlet.contentlet_as_json->'fields'->'key'->'value' as key, \n"
+ " contentlet.contentlet_as_json->'fields'->'value'->'value' as value, \n"
+ " contentlet.identifier,\n"
+ " contentlet.language_id\n"
+ " from contentlet \n"
+ " inner join ( \n"
+ " select distinct c.identifier \n"
+ " from contentlet c, contentlet_version_info vi, inode i \n"
+ " where \n"
+ " inner join ( \n"
+ " select distinct c.identifier, vi.lang \n"
+ " from contentlet c, contentlet_version_info vi \n"
+ " where \n"
+ " c.structure_inode = '"+contentType.inode()+"' \n"
+ " and vi.identifier = c.identifier \n"
+ " and ( vi.working_inode = c.inode or vi.live_inode = c.inode ) \n"
+ " and c.inode = i.inode \n"
+ " and vi.deleted = 'false' \n"
+ " order by c.identifier \n"
+ " offset "+offset+" limit "+limit+" \n"
+ " ) con_ident on contentlet.identifier = con_ident.identifier \n"
+ " inner join ( \n"
+ " select cvi.* from contentlet_version_info cvi \n"
+ " ) cc on ( cc.working_inode = contentlet.inode or cc.live_inode = contentlet.inode ) \n"
+ " join (\n"
+ " select i.* from inode i \n"
+ " ) inode on inode.inode = contentlet.inode \n";
// END-NOSCAN
+ " ) con_ident on contentlet.identifier = con_ident.identifier and contentlet.language_id = con_ident.lang \n";
final String sanitizedSort = SQLUtil.sanitizeSortBy(orderBy);
if (!sanitizedSort.isEmpty()) {
select += ORDER_BY + sanitizedSort;
Expand Down Expand Up @@ -130,27 +121,18 @@ public List<LanguageVariableExt> findVariablesForPagination(ContentType contentT
@Override
public int countVariablesByKey(ContentType contentType) throws DotDataException {
final DotConnect dotConnect = new DotConnect();
// START-NOSCAN
final String select = "select count( distinct( contentlet.contentlet_as_json->'fields'->'key'->'value' )) as x \n"
+ " from contentlet \n"
+ " inner join ( \n"
+ " select distinct c.identifier \n"
+ " from contentlet c, contentlet_version_info vi, inode i \n"
+ " select distinct c.identifier, vi.lang \n"
+ " from contentlet c, contentlet_version_info vi \n"
+ " where \n"
+ " c.structure_inode = '"+contentType.inode()+"' \n"
+ " and vi.identifier = c.identifier \n"
+ " and ( vi.working_inode = c.inode or vi.live_inode = c.inode ) \n"
+ " and c.inode = i.inode \n"
+ " and vi.deleted = 'false' \n"
+ " order by c.identifier \n"
+ " ) con_ident on contentlet.identifier = con_ident.identifier \n"
+ " inner join ( \n"
+ " select cvi.* from contentlet_version_info cvi \n"
+ " ) cc on ( cc.working_inode = contentlet.inode or cc.live_inode = contentlet.inode ) \n"
+ " join (\n"
+ " select i.* from inode i \n"
+ " ) inode on inode.inode = contentlet.inode";
// END-NOSCAN
+ " ) con_ident on contentlet.identifier = con_ident.identifier and contentlet.language_id = con_ident.lang \n";
dotConnect.setSQL(select);
return dotConnect.getInt("x");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void buildVariablesTable(final String key, final List<LanguageVariableExt> varia
//for easy access of LanguageVariable by languageId
final Map<Long, LanguageVariable> byLangIdMap = variables.stream()
.collect(
Collectors.toMap(LanguageVariableExt::languageId, Function.identity()));
Collectors.toMap(LanguageVariableExt::languageId, Function.identity(), (v1, v2) -> v1));
//if no value for the key, we create a new map
if (v == null) {
v = new LinkedHashMap<>(languages.size());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.dotcms.rest.api.v2.languages;

import static com.dotcms.rest.ResponseEntityView.OK;
import static com.dotmarketing.portlets.languagesmanager.business.LanguageAPI.isLocalizationEnhancementsEnabled;
import static com.dotmarketing.util.WebKeys.CONTENT_SELECTED_LANGUAGE;
import static com.dotmarketing.util.WebKeys.HTMLPAGE_LANGUAGE;
import static com.dotmarketing.util.WebKeys.LANGUAGE_SEARCHED;

import com.dotcms.keyvalue.model.KeyValue;
import com.dotcms.languagevariable.business.LanguageVariable;
import com.dotcms.languagevariable.business.LanguageVariableAPI;
import com.dotcms.rendering.velocity.viewtools.util.ConversionUtils;
import com.dotcms.repackage.com.google.common.annotations.VisibleForTesting;
import com.dotcms.rest.AnonymousAccess;
Expand Down Expand Up @@ -347,10 +350,6 @@ private Locale resolveAdminLocale(@NotNull String language) {
}






/**
* Gets all the Messages from the language passed.
* If default is passed it will get the messages for the default language.
Expand All @@ -368,7 +367,7 @@ private Locale resolveAdminLocale(@NotNull String language) {
public Response getAllMessages (
@Context final HttpServletRequest request,
@Context final HttpServletResponse response,
@PathParam("language") final String language){
@PathParam("language") final String language) throws DotDataException {

final InitDataObject initData = new WebResource.InitBuilder(request, response)
.requiredAnonAccess(AnonymousAccess.READ)
Expand All @@ -377,36 +376,52 @@ public Response getAllMessages (
final User user = initData.getUser();

final Locale currentLocale=resolveAdminLocale(language);

//Messages in the properties file
final Map mapPropertiesFile = LanguageUtil.getAllMessagesByLocale(currentLocale);
//These are the resources that are in the properties file added by developers to dotCMS so will always need this
final Map<?,?> mapPropertiesFile = LanguageUtil.getAllMessagesByLocale(currentLocale);

final Map result = new TreeMap(mapPropertiesFile);
final Map<Object,Object> result = new TreeMap<>(mapPropertiesFile);

final Language language1 = APILocator.getLanguageAPI().getLanguage(currentLocale.getLanguage(),currentLocale.getCountry());
final Language language1 = languageAPI.getLanguage(currentLocale.getLanguage(),currentLocale.getCountry());
if(UtilMethods.isSet(language1)) {
//Language Keys
final Map mapLanguageKeys = APILocator.getLanguageAPI()
.getLanguageKeys(currentLocale.getLanguage()).stream().collect(
Collectors.toMap(LanguageKey::getKey, LanguageKey::getValue));

result.putAll(mapLanguageKeys);

//Language Variable
long langId = language1.getId();
final Map mapLanguageVariables = Try.of(()->APILocator.getLanguageVariableAPI().getAllLanguageVariablesKeyStartsWith("", langId,
user, -1)).getOrElse(ArrayList::new).stream().collect(Collectors.toMap(
KeyValue::getKey,KeyValue::getValue, (value1,value2) ->{
Logger.warn(this.getClass(),"Duplicate language variable found using latest value: " + value1);
return value1;
}));

result.putAll(mapLanguageVariables);
final LanguageVariableAPI languageVariableAPI = APILocator.getLanguageVariableAPI();
if(isLocalizationEnhancementsEnabled()) {
final Language matchingLang = languageAPI.getLanguage(currentLocale.getLanguage(),currentLocale.getCountry());
// Enhanced Language Vars
final List<LanguageVariable> variables = languageVariableAPI.findVariables(matchingLang.getId());
final Map<?,?> map = variables.stream().collect(
Collectors.toMap(LanguageVariable::key, LanguageVariable::value, (value1,value2) ->{
//Merge function is always a good idea to have.
//There can be cases on which the "unique" constraint of the key is lifted allowing for duplicates
Logger.warn(this.getClass(),"Duplicate language variable found using latest value: " + value1);
return value1;
}));
result.putAll(map);
} else {
//Language Keys
final Map <?,?> mapLanguageKeys = languageAPI
.getLanguageKeys(currentLocale.getLanguage()).stream().collect(
Collectors.toMap(LanguageKey::getKey, LanguageKey::getValue));

result.putAll(mapLanguageKeys);

//Legacy Language Variable
long langId = language1.getId();
final Map mapLanguageVariables = Try.of(()->APILocator.getLanguageVariableAPI().getAllLanguageVariablesKeyStartsWith("", langId,
user, -1)).getOrElse(ArrayList::new).stream().collect(Collectors.toMap(
KeyValue::getKey,KeyValue::getValue, (value1,value2) ->{
Logger.warn(this.getClass(),"Duplicate language variable found using latest value: " + value1);
return value1;
}));
result.putAll(mapLanguageVariables);
}

}


return Response.ok(new ResponseEntityView(result)).build();
return Response.ok(new ResponseEntityView<>(result)).build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.dotcms.contenttype.model.type.PageContentType;
import com.dotcms.enterprise.FormAJAXProxy;
import com.dotcms.keyvalue.model.KeyValue;
import com.dotcms.languagevariable.business.LanguageVariable;
import com.dotcms.languagevariable.business.LanguageVariableAPI;
import com.dotcms.repackage.com.google.common.base.Preconditions;
import com.dotcms.repackage.org.directwebremoting.WebContextFactory;
import com.dotcms.util.LogTime;
Expand Down Expand Up @@ -114,6 +116,7 @@
import static com.dotmarketing.business.PermissionAPI.PERMISSION_PUBLISH;
import static com.dotmarketing.business.PermissionAPI.PERMISSION_READ;
import static com.dotmarketing.business.PermissionAPI.PERMISSION_WRITE;
import static com.dotmarketing.portlets.languagesmanager.business.LanguageAPI.isLocalizationEnhancementsEnabled;

/**
* This class handles the communication between the view and the back-end
Expand Down Expand Up @@ -142,6 +145,8 @@ public class ContentletAjax {
private ContentletWebAPI contentletWebAPI = WebAPILocator.getContentletWebAPI();
private LanguageAPI langAPI = APILocator.getLanguageAPI();

private LanguageVariableAPI languageVariableAPI = APILocator.getLanguageVariableAPI();

//Number of children related IDs to be added to a lucene query to get children related to a selected parent
private static final int RELATIONSHIPS_FILTER_CRITERIA_SIZE = Config
.getIntProperty("RELATIONSHIPS_FILTER_CRITERIA_SIZE", 500);
Expand Down Expand Up @@ -1649,36 +1654,30 @@ private JSONArray getAvailableWorkflowActionsListingJson(final User currentUser,
}

@CloseDB
public ArrayList<String[]> doSearchGlossaryTerm(String valueToComplete, String language) throws Exception {
final int limit = Config.getIntProperty("glossary.term.max.limit",15);
ArrayList<String[]> list = new ArrayList<>(limit);
public List<String[]> doSearchGlossaryTerm(String valueToComplete, String language)
throws DotDataException, DotSecurityException {
final int limit = Config.getIntProperty("glossary.term.max.limit", 15);
List<String[]> list = new ArrayList<>(limit);
final User systemUser = APILocator.systemUser();
final long languageId = Long.parseLong(language);
List<String> listAddedKeys = new ArrayList<>();
String[] term;

List<LanguageKey> props = retrieveProperties(languageId);
valueToComplete = valueToComplete.toLowerCase();
for (LanguageKey prop : props) {
if (prop.getKey().toLowerCase().startsWith(valueToComplete)) {
term = new String[]{prop.getKey(),
(70 < prop.getValue().length() ? prop.getValue().substring(0, 69)
: prop.getValue())};
list.add(term);
listAddedKeys.add(prop.getKey());
}
}

if(list.size() < limit){
List<KeyValue> languageVariables = APILocator.getLanguageVariableAPI().getAllLanguageVariablesKeyStartsWith(valueToComplete,languageId,systemUser,limit);
final List<String> listAddedKeys =
isLocalizationEnhancementsEnabled() ? collectLanguageVariables(valueToComplete,
list, languageId) : collectLanguageKeys(valueToComplete, list, languageId);

if (list.size() < limit) {
List<KeyValue> languageVariables = languageVariableAPI.getAllLanguageVariablesKeyStartsWith(
valueToComplete, languageId, systemUser, limit);
for (KeyValue languageVariable : languageVariables) {
if(!listAddedKeys.contains(languageVariable.getKey())) {
term = new String[]{languageVariable.getKey(),
if (!listAddedKeys.contains(languageVariable.getKey())) {
final String[] term = new String[]{languageVariable.getKey(),
(70 < languageVariable.getValue().length() ? languageVariable.getValue()
.substring(0, 69) : languageVariable.getValue())};
list.add(term);
}
if(list.size() == limit){
if (list.size() == limit) {
break;
}
}
Expand All @@ -1687,11 +1686,54 @@ public ArrayList<String[]> doSearchGlossaryTerm(String valueToComplete, String l
return list;
}

private List<LanguageKey> retrieveProperties(long langId) throws Exception {
Language lang = langAPI.getLanguage(langId);
return langAPI.getLanguageKeys(lang);
/**
* Collects the language keys that start with the valueToComplete from the properties file
* @param valueToComplete the value to complete
* @param list the list to add the terms
* @param languageId the language id
* @return the list of added keys
*/
private List<String> collectLanguageKeys(final String valueToComplete, final List<String[]> list, final long languageId) {
final Language lang = langAPI.getLanguage(languageId);
final List<String> listAddedKeys = new ArrayList<>();
List<LanguageKey> props = langAPI.getLanguageKeys(lang);
for (LanguageKey prop : props) {
if (prop.getKey().toLowerCase().startsWith(valueToComplete)) {
final String[] term = new String[]{prop.getKey(),
(70 < prop.getValue().length() ? prop.getValue().substring(0, 69)
: prop.getValue())};
list.add(term);
listAddedKeys.add(prop.getKey());
}
}
return listAddedKeys;
}

/**
* Collects the language variables that start with the valueToComplete from the properties file
* @param valueToComplete the value to complete
* @param list the list to add the terms
* @param languageId the language id
* @return the list of added keys
* @throws DotDataException if an error occurs
*/
private List<String> collectLanguageVariables(final String valueToComplete, final List<String[]> list, final long languageId)
throws DotDataException {
final List<String> listAddedKeys = new ArrayList<>();
final List<LanguageVariable> variables = languageVariableAPI.findVariables(languageId);
for (LanguageVariable variable : variables) {
if (variable.key().toLowerCase().startsWith(valueToComplete)) {
final String[] term = new String[]{variable.key(),
(70 < variable.value().length() ? variable.value().substring(0, 69)
: variable.value())};
list.add(term);
listAddedKeys.add(variable.key());
}
}
return listAddedKeys;
}


/**
* Publishes or unpublishes contentlets from a given list of identifiers. You can have to publish within
* a specific language or all languages. Set the languageId = 0 for all languages.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import javax.servlet.http.HttpServletRequest;

@Deprecated(since = "24.05", forRemoval = true)
public class LanguageAjax {

public LanguageAPI langAPI = APILocator.getLanguageAPI();
Expand Down
Loading
Loading