Skip to content

Commit

Permalink
#27448 Updating code to support in the language file descriptor only …
Browse files Browse the repository at this point in the history
…the language name and the ISO code.

This update modifies the `ContentComparator` interface to include a `localFile` parameter in its findMatchingServerContent and localContains methods. Also, modifications have been made to all applicable classes that implement this interface, including support for file renaming if content changes. A NameUtil class has been added to generate standardized file names for Site, Language, and ContentType. Other enhancements include updating the push and pull handlers for Site, Language, and ContentType and improving their respective functionalities.
  • Loading branch information
jgambarios committed Apr 9, 2024
1 parent c4932c7 commit f6a617b
Show file tree
Hide file tree
Showing 19 changed files with 249 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,12 @@ protected void saveLanguage(final Language lang) {
}
dbUpsert(lang);
CacheLocator.getLanguageCache().clearLanguages();
// Cleaning up the createDefaultLanguageLock if we are updating the default language,
// otherwise changes won't be reflected.
if (createDefaultLanguageLock.get() != null
&& createDefaultLanguageLock.get().getId() == lang.getId()) {
createDefaultLanguageLock.set(null);
}
} catch (DotDataException e) {

throw new DotRuntimeException("saveLanguage failed to save the language:" + lang, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.dotcms.model.ResponseEntityView;
import com.dotcms.model.language.Language;
import com.dotcms.model.views.CommonViews;
import com.dotcms.model.views.CommonViews.LanguageFileView;
import com.fasterxml.jackson.annotation.JsonView;
import java.util.List;
import javax.ws.rs.Consumes;
Expand Down Expand Up @@ -41,6 +42,7 @@ public interface LanguageAPI {
@Operation(
summary = " Returns the Language that matches the specified id"
)
@JsonView(LanguageFileView.class)
ResponseEntityView<Language> findById(@PathParam("languageid") String languageId);


Expand All @@ -51,6 +53,14 @@ public interface LanguageAPI {
)
ResponseEntityView<Language> getFromLanguageIsoCode(@PathParam("languageTag") String languageIsoCode);

@GET
@Operation(
summary = " Returns all the languages in the system but using a different view, a "
+ "reduced one, for pull operations and the generation of the language files."
)
@JsonView(LanguageFileView.class)
ResponseEntityView<List<Language>> listForPull();

@GET
@Operation(
summary = " Returns all the languages in the system"
Expand All @@ -61,23 +71,28 @@ public interface LanguageAPI {
@Operation(
summary = " Creates a new language in the system"
)
@JsonView(CommonViews.LanguageFileView.class)
ResponseEntityView<Language> create(
@JsonView(CommonViews.ExternalView.class) Language language);
@JsonView(CommonViews.LanguageExternalView.class) Language language);

@POST
@Path("/{languageTag}")
@Operation(
summary = " Creates a new language in the system given its ISO code"
)
ResponseEntityView<Language> create(@PathParam("languageTag") String languageIsoCode);
@JsonView(CommonViews.LanguageFileView.class)
ResponseEntityView<Language> create(
@JsonView(CommonViews.LanguageExternalView.class)
@PathParam("languageTag") String languageIsoCode);

@PUT
@Path("/{languageId}")
@Operation(
summary = " Updates an existing language in the system"
)
@JsonView(CommonViews.LanguageFileView.class)
ResponseEntityView<Language> update(@PathParam("languageId") String languageId,
@JsonView(CommonViews.ExternalView.class) Language language);
@JsonView(CommonViews.LanguageExternalView.class) Language language);

@DELETE
@Path("/{languageId}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.dotcms.model.language;

import com.dotcms.model.annotation.ValueType;
import com.dotcms.model.views.CommonViews;
import com.dotcms.model.views.CommonViews.LanguageExternalView;
import com.dotcms.model.views.CommonViews.LanguageFileView;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
Expand All @@ -19,25 +20,31 @@ public interface AbstractLanguage {

String TYPE = "Language";

@JsonView(CommonViews.InternalView.class)
@JsonView(LanguageFileView.class)
@Value.Derived
default String dotCMSObjectType() {
return TYPE;
}

@JsonView(LanguageExternalView.class)
Optional<Long> id();

@JsonView(LanguageExternalView.class)
Optional<String> languageCode();

@JsonView(LanguageExternalView.class)
Optional<String> countryCode();

@JsonView({LanguageFileView.class, LanguageExternalView.class})
Optional<String> language();

@JsonView(LanguageExternalView.class)
Optional<String> country();

@JsonView(CommonViews.InternalView.class)
@JsonView(LanguageFileView.class)
Optional<Boolean> defaultLanguage();

@JsonView({LanguageFileView.class, LanguageExternalView.class})
String isoCode();

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,19 @@ public interface InternalView extends ExternalView {

}

/**
* The LanguageFileView interface defines the view used for the language file descriptor
*/
public interface LanguageFileView {

}

/**
* The LanguageExternalView interface defines the view used for some server operations as those
* server operations require more data than the one displayed in the language file descriptor
*/
public interface LanguageExternalView {

}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dotcms.api.client.pull.contenttype;

import com.dotcms.api.client.pull.GeneralPullHandler;
import com.dotcms.api.client.util.NameUtil;
import com.dotcms.contenttype.model.type.ContentType;
import com.dotcms.model.pull.PullOptions;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -32,7 +33,7 @@ public String displayName(final ContentType contentType) {

@Override
public String fileName(final ContentType contentType) {
return contentType.variable();
return NameUtil.contentTypeFileName(contentType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class LanguageFetcher implements ContentFetcher<Language>, Serializable {
public List<Language> fetch(final boolean failFast, final Map<String, Object> customOptions) {

final var languageAPI = clientFactory.getClient(LanguageAPI.class);
return languageAPI.list().entity();
return languageAPI.listForPull().entity();
}

@ActivateRequestContext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dotcms.api.client.pull.language;

import com.dotcms.api.client.pull.GeneralPullHandler;
import com.dotcms.api.client.util.NameUtil;
import com.dotcms.model.language.Language;
import com.dotcms.model.pull.PullOptions;
import java.util.List;
Expand Down Expand Up @@ -30,19 +31,18 @@ public String displayName(final Language language) {

@Override
public String fileName(final Language language) {
return language.isoCode();
return NameUtil.languageFileName(language);
}

@Override
public String shortFormat(final Language language, final PullOptions pullOptions) {

return String.format(
"language: [@|bold,underline,blue %s|@] id: [@|bold,underline,cyan %s|@] code: [@|bold,underline,green %s|@] country:[@|bold,yellow %s|@] countryCode: [@|bold,yellow %s|@] isoCode: [@|bold,yellow %s|@]",
"language: [@|bold,underline,blue %s|@] "
+ "defaultLanguage: [@|bold,underline,cyan %b|@] "
+ "isoCode: [@|bold,yellow %s|@]",
language.language().orElse(""),
language.id().isPresent() ? language.id().get() : "",
language.languageCode().orElse(""),
language.country().orElse(""),
language.countryCode().orElse(""),
language.defaultLanguage().orElse(false),
language.isoCode()
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dotcms.api.client.pull.site;

import com.dotcms.api.client.pull.GeneralPullHandler;
import com.dotcms.api.client.util.NameUtil;
import com.dotcms.model.pull.PullOptions;
import com.dotcms.model.site.SiteView;
import java.util.List;
Expand Down Expand Up @@ -31,7 +32,7 @@ public String displayName(final SiteView site) {

@Override
public String fileName(final SiteView site) {
return site.hostName();
return NameUtil.siteFileName(site);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dotcms.api.client.push;

import java.io.File;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
Expand All @@ -22,22 +23,24 @@ public interface ContentComparator<T> {
/**
* Finds matching server content based on local content and a list of server contents.
*
* @param localFile the local file representing the local content
* @param localContent the local content to compare against server contents
* @param serverContents the list of server contents to search for matches
* @return an Optional containing the matching server content if found, otherwise an empty
* Optional.
*/
Optional<T> findMatchingServerContent(T localContent, List<T> serverContents);
Optional<T> findMatchingServerContent(File localFile, T localContent, List<T> serverContents);

/**
* Checks if the given server content is contained within the list of local contents.
*
* @param serverContent the server content to check for containment
* @param localFiles the list of local files representing the local contents
* @param localContents the list of local contents to search for containment
* @return an Optional containing the matching local content if found, or an empty Optional if
* not found.
*/
Optional<T> localContains(T serverContent, List<T> localContents);
Optional<T> localContains(T serverContent, List<File> localFiles, List<T> localContents);

/**
* Checks if the given local content and server content are equal.
Expand All @@ -48,6 +51,17 @@ public interface ContentComparator<T> {
*/
boolean contentEquals(T localContent, T serverContent);

/**
* Retrieves the file name without the extension from a given File object.
*
* @param file the File object representing the file
* @return the file name without the extension
*/
default String getFileName(File file) {
String fileName = file.getName();
return fileName.substring(0, fileName.lastIndexOf('.'));
}

/**
* Retrieves a comparator that can be used to sort a list of contents in the order they should
* be processed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ private <T> List<PushAnalysisResult<T>> checkLocal(List<File> localFiles,
var localContent = map(localFile, comparator.type());

var matchingServerContent = comparator.findMatchingServerContent(
localFile,
localContent,
serverContents
);
Expand Down Expand Up @@ -161,7 +162,7 @@ private <T> List<PushAnalysisResult<T>> checkServerForRemovals(List<File> localF

for (T serverContent : serverContents) {

var local = comparator.localContains(serverContent, localContents);
var local = comparator.localContains(serverContent, localFiles, localContents);
if (local.isEmpty()) {
removals.add(
PushAnalysisResult.<T>builder().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public interface PushHandler<T> {
*/
String title();

/**
* Returns the file name for a given T elements used to save the content to a file.
*
* @param content the content to be saved to a file.
*/
String fileName(T content);

/**
* Generates a simple String representation of a content to use on the console.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.dotcms.api.client.model.RestClientFactory;
import com.dotcms.api.client.push.ContentComparator;
import com.dotcms.contenttype.model.type.ContentType;
import java.io.File;
import java.util.List;
import java.util.Optional;
import javax.enterprise.context.Dependent;
Expand All @@ -23,8 +24,8 @@ public Class<ContentType> type() {

@ActivateRequestContext
@Override
public Optional<ContentType> findMatchingServerContent(ContentType localContentType,
List<ContentType> serverContents) {
public Optional<ContentType> findMatchingServerContent(File localFile,
ContentType localContentType, List<ContentType> serverContents) {

// Compare by identifier first.
var result = findById(localContentType.id(), serverContents);
Expand All @@ -40,7 +41,7 @@ public Optional<ContentType> findMatchingServerContent(ContentType localContentT

@ActivateRequestContext
@Override
public Optional<ContentType> localContains(ContentType serverContent,
public Optional<ContentType> localContains(ContentType serverContent, List<File> localFiles,
List<ContentType> localSites) {

// Compare by identifier first.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.dotcms.api.ContentTypeAPI;
import com.dotcms.api.client.model.RestClientFactory;
import com.dotcms.api.client.push.PushHandler;
import com.dotcms.api.client.util.NameUtil;
import com.dotcms.contenttype.model.type.ContentType;
import com.dotcms.model.contenttype.AbstractSaveContentTypeRequest.Builder;
import com.dotcms.model.contenttype.SaveContentTypeRequest;
Expand Down Expand Up @@ -32,6 +33,11 @@ public String title() {
return "ContentTypes";
}

@Override
public String fileName(final ContentType contentType) {
return NameUtil.contentTypeFileName(contentType);
}

@Override
public String contentSimpleDisplay(ContentType contentType) {

Expand Down
Loading

0 comments on commit f6a617b

Please sign in to comment.