Skip to content

Commit

Permalink
chore: Including hotfix-Zendesk-99460 along with GIT-14965 (#26147)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcastro-dotcms committed Sep 17, 2023
1 parent d73a272 commit d0653d2
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.dotcms.content.elasticsearch.business;

import com.dotcms.business.WrapInTransaction;
import com.dotcms.concurrent.DotConcurrentFactory;
import com.dotcms.concurrent.DotSubmitter;
import com.dotcms.content.business.DotMappingException;
import com.dotcms.content.elasticsearch.business.IndiciesAPI.IndiciesInfo;
import com.dotcms.content.elasticsearch.constants.ESMappingConstants;
Expand Down Expand Up @@ -911,7 +913,12 @@ protected Contentlet findContentletByIdentifierAnyLanguage(String identifier) th
List<Language> langs = APILocator.getLanguageAPI().getLanguages();
for(Language l : langs) {
ContentletVersionInfo cvi = APILocator.getVersionableAPI().getContentletVersionInfo(identifier, l.getId());
if(cvi!=null && !cvi.isDeleted()) {
Logger.info(this,"Looking contentlet for identifier: " + identifier
+ " and language: " + l.getId() + ", is set: "
+ (cvi != null && UtilMethods.isSet(cvi.getIdentifier())));
if(cvi != null && UtilMethods.isSet(cvi.getIdentifier()) && !cvi.isDeleted()) {
Logger.info(this,"Found contentlet for identifier: " + identifier
+ " and language: " + l.getId() + ", exists and is not deleted");
return find(cvi.getWorkingInode());
}
}
Expand Down Expand Up @@ -2342,40 +2349,40 @@ protected void clearField(String structureInode, Field field) throws DotDataExce
return;
}
Queries queries = getQueries(field);
List<String> inodesToFlush = new ArrayList<>();
final List<String> inodesToFlush = new ArrayList<>();

Connection conn = DbConnectionFactory.getConnection();

try(PreparedStatement ps = conn.prepareStatement(queries.getSelect())) {
ps.setObject(1, structureInode);
final int BATCH_SIZE = 200;

try(ResultSet rs = ps.executeQuery();)
{
PreparedStatement ps2 = conn.prepareStatement(queries.getUpdate());
for (int i = 1; rs.next(); i++) {
String contentInode = rs.getString("inode");
try(ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
final String contentInode = rs.getString("inode");
inodesToFlush.add(contentInode);
ps2.setString(1, contentInode);
ps2.addBatch();

if (i % BATCH_SIZE == 0) {
ps2.executeBatch();
}
}
}

ps2.executeBatch(); // insert remaining records
try (PreparedStatement ps2 = conn.prepareStatement(queries.getUpdate())) {
ps2.setObject(1, structureInode);
int numUpdatedRows = ps2.executeUpdate();
Logger.info(this, "Cleared " + numUpdatedRows + " contentlets "
+ " for field ID: " + field.getInode() + " with var name: " + field.getVelocityVarName()
+ " from content type ID: " + structureInode);
}


} catch (SQLException e) {
throw new DotDataException(String.format("Error clearing field '%s' for Content Type with ID: %s",
field.getVelocityVarName(), structureInode), e);

}

for (String inodeToFlush : inodesToFlush) {
contentletCache.remove(inodeToFlush);
}
DotConcurrentFactory.getInstance().getSubmitter().submit(() -> {
for (String inodeToFlush : inodesToFlush) {
contentletCache.remove(inodeToFlush);
}
});

}

/**
Expand Down Expand Up @@ -2456,7 +2463,7 @@ public Queries getQueries(Field field) {
}

select.append(" WHERE structure_inode = ?").append(" AND (").append(whereField).append(")");
update.append(" WHERE inode = ?");
update.append(" WHERE structure_inode = ?").append(" AND (").append(whereField).append(")");

return new Queries().setSelect(select.toString()).setUpdate(update.toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.dotmarketing.util.json.JSONArray;
import com.dotmarketing.util.json.JSONObject;
import com.liferay.portal.model.User;
import org.apache.commons.lang3.time.StopWatch;
import org.elasticsearch.action.search.SearchResponse;

import java.util.*;
Expand Down Expand Up @@ -475,6 +476,11 @@ private ContentType transactionalSave(final List<Field> newFields,
// not logging, expected when inserting new from separate environment
}

Logger.info(this, "Saving Content Type: " + contentTypeToSave.name()
+ " with ID: " + contentTypeToSave.id());
StopWatch stopWatch = new StopWatch();
stopWatch.start();

contentTypeToSave = this.contentTypeFactory.save(contentTypeToSave);

if (oldType != null) {
Expand All @@ -491,10 +497,18 @@ private ContentType transactionalSave(final List<Field> newFields,
"User " + user.getUserId() + "/" + user.getFullName() + " added ContentType " + contentTypeToSave.name()
+ " to host id:" + contentTypeToSave.host());
AdminLogger.log(getClass(), "ContentType", "ContentType saved : " + contentTypeToSave.name(), user);
stopWatch.stop();
Logger.info(this, "Saved Content Type: " + contentTypeToSave.name()
+ " with ID: " + contentTypeToSave.id() + ", elapsed time: " + stopWatch);

// update the existing content type fields
if (newFields != null) {

Logger.info(this, "Removing no longer needed fields for Content Type: " + contentTypeToSave.name()
+ " with ID: " + contentTypeToSave.id());
stopWatch.reset();
stopWatch.start();

Map<String, Field> varNamesCantDelete = new HashMap();

for (Field oldField : oldFields) {
Expand All @@ -512,11 +526,20 @@ private ContentType transactionalSave(final List<Field> newFields,
}
}

stopWatch.stop();
Logger.info(this, "Removed no longer needed fields for Content Type: " + contentTypeToSave.name()
+ " with ID: " + contentTypeToSave.id() + ", elapsed time: " + stopWatch);

// for each field in the content type lets create it if doesn't exists and update its
// properties if it does
for (Field field : newFields) {

field = this.checkContentTypeFields(contentTypeToSave, field);
Logger.info(this, "Saving field " + field.id() + " with var " + field.variable()
+ " for Content Type: " + contentTypeToSave.name()
+ " with ID: " + contentTypeToSave.id());
stopWatch.reset();
stopWatch.start();
if (!varNamesCantDelete.containsKey(field.variable())) {
fieldAPI.save(field, APILocator.systemUser());
} else {
Expand Down Expand Up @@ -544,6 +567,11 @@ private ContentType transactionalSave(final List<Field> newFields,
}
}
}
stopWatch.stop();
Logger.info(this, "Saving field " + field.id() + " with var " + field.variable()
+ " for Content Type: " + contentTypeToSave.name()
+ " with ID: " + contentTypeToSave.id() + ", elapsed time: " + stopWatch);

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@
import com.liferay.portal.model.User;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.StopWatch;


public class FieldAPIImpl implements FieldAPI {
Expand Down Expand Up @@ -179,6 +182,8 @@ public Field save(final Field field, final User user) throws DotDataException, D

//if RelationshipField, Relationship record must be added/updated
if (field instanceof RelationshipField) {
Logger.info(this, "Field " + field.name() + ", var name: " + field.variable()
+ " in content type: " + type.id() + " is a relationship field, looking for existing relationship");
Optional<Relationship> relationship = getRelationshipForField(result, contentTypeAPI,
type, user);

Expand Down Expand Up @@ -280,10 +285,16 @@ Optional<Relationship> getRelationshipForField(final Field field, final ContentT
final int cardinality = Integer.parseInt(field.values());

final String[] relationType = field.relationType().split("\\.");

Logger.info(this, "Relation types for field " + field.name() + ", var name: " + field.variable()
+ ", related content type: " + relationType[0] + ", field content type: "
+ (relationType.length > 1 ? relationType[1] : "()") + ", cardinality: " + cardinality);
//we need to find the id of the related structure using the velocityVarName set in the relationType
try {
relatedContentType = contentTypeAPI.find(relationType[0]);
if (relatedContentType != null) {
Logger.info(this, "Related content type id: " + relatedContentType.id()
+ ", name: " + relatedContentType.name() + ", var name: " + relatedContentType.variable());
}
} catch (NotFoundInDbException e) {
final String errorMessage = "Unable to save relationships for field " + field.name()
+ " because the related content type " + relationType[0]
Expand All @@ -300,11 +311,24 @@ Optional<Relationship> getRelationshipForField(final Field field, final ContentT

//verify if the relationship already exists
if (UtilMethods.isSet(relationship) && UtilMethods.isSet(relationship.getInode())) {

Logger.info(this, "Relationship exists, updating for field: " + field.name()
+ ", var name: " + field.variable()
+ ", related content type: " + (relatedContentType == null ? "" : relatedContentType.id())
+ ", var name: " + (relatedContentType == null ? "" : relatedContentType.variable())
+ ", relationship id: " + relationship.getInode()
+ ", relationship parent name: " + relationship.getParentRelationName()
+ ", relationship child name: " + relationship.getChildRelationName()
+ ", relationship parent inode: " + relationship.getParentStructureInode()
+ ", relationship child inode: " + relationship.getChildStructureInode()
+ ", relationship type: " + relationship.getRelationTypeValue());
updateRelationshipObject(field, type, relatedContentType, relationship, cardinality, user);

} else {
//otherwise, a new relationship will be created
Logger.info(this, "Adding new relationship for field: " + field.name()
+ ", var name: " + field.variable()
+ ", related content type: " + (relatedContentType == null ? "" : relatedContentType.id())
+ ", var name: " + (relatedContentType == null ? "" : relatedContentType.variable()));
relationship = new Relationship(type, relatedContentType, field);
}

Expand Down Expand Up @@ -351,6 +375,13 @@ private void updateRelationshipObject(final Field field, final ContentType type,

//check which side of the relationship is being updated (parent or child)
if (isChildField) {

Logger.info(this, "Relatioship field is child field: "
+ field.id() + ", var name: " + field.variable() + ", related content type: "
+ relatedContentType.id() + ", with name: " + relatedContentType.name()
+ ", and var name: " + relatedContentType.variable()
+ ", relationship id: " + relationship.getInode());

//parent is updated
relationship.setParentRelationName(relationName);
relationship.setParentRequired(field.required());
Expand All @@ -370,7 +401,11 @@ private void updateRelationshipObject(final Field field, final ContentType type,

if (relationship.getChildRelationName() != null) {
//verify if the cardinality was changed to update it on the other side of the relationship
final Field otherSideField = byContentTypeAndVar(relatedContentType,
Logger.info(this, "Looking for other side field for child field: "
+ relationship.getChildRelationName() + ", in related content type id: "
+ relatedContentType.id() + ", with name: " + relatedContentType.name()
+ ", and var name: " + relatedContentType.variable());
final Field otherSideField = fieldFactory.byContentTypeIdFieldVar(relatedContentType.id(),
relationship.getChildRelationName());

if (!otherSideField.values().equals(field.values())) {
Expand All @@ -380,6 +415,13 @@ private void updateRelationshipObject(final Field field, final ContentType type,
}
}
} else {

Logger.info(this, "Relatioship field is parent field: "
+ field.id() + ", var name: " + field.variable() + ", related content type: "
+ relatedContentType.id() + ", with name: " + relatedContentType.name()
+ ", and var name: " + relatedContentType.variable()
+ ", relationship id: " + relationship.getInode());

//child is updated
relationship.setChildRelationName(relationName);
relationship.setChildRequired(field.required());
Expand All @@ -397,7 +439,13 @@ private void updateRelationshipObject(final Field field, final ContentType type,

//verify if the cardinality was changed to update it on the other side of the relationship
if (relationship.getParentRelationName() != null) {
final Field otherSideField = byContentTypeAndVar(relatedContentType,

Logger.info(this, "Looking for other side field for parent field: "
+ relationship.getParentRelationName() + ", in related content type id: "
+ relatedContentType.id() + ", with name: " + relatedContentType.name()
+ ", and var name: " + relatedContentType.variable());

final Field otherSideField = fieldFactory.byContentTypeIdFieldVar(relatedContentType.id(),
relationship.getParentRelationName());

if (!otherSideField.values().equals(field.values())) {
Expand Down Expand Up @@ -479,6 +527,11 @@ public void delete(final Field field, final User user) throws DotDataException,
final Structure structure = new StructureTransformer(type).asStructure();
com.dotmarketing.portlets.structure.model.Field legacyField = new LegacyFieldTransformer(field).asOldField();

Logger.info(this, "Deleting Field: " + field.name() + " with ID: " + field.id()
+ " and var name:" + field.variable() + ", from Content Type: " + structure.getName()
+ " with ID: " + structure.id());
StopWatch stopWatch = new StopWatch();
stopWatch.start();

if (!(field instanceof CategoryField) &&
!(field instanceof ConstantField) &&
Expand All @@ -497,6 +550,11 @@ public void delete(final Field field, final User user) throws DotDataException,
fieldFactory.moveSortOrderBackward(type.id(), oldField.sortOrder());
fieldFactory.delete(field);

stopWatch.stop();
Logger.info(this, "Deleted Field: " + field.name() + " with ID: " + field.id()
+ " and var name:" + field.variable() + ", from Content Type: " + structure.getName()
+ " with ID: " + structure.id() + ", elapsed time: " + stopWatch);

ActivityLogger.logInfo(ActivityLogger.class, "Delete Field Action",
String.format("User %s/%s deleted field %s from %s Content Type.", user.getUserId(), user.getFirstName(),
field.name(), structure.getName()));
Expand All @@ -518,7 +576,16 @@ public void delete(final Field field, final User user) throws DotDataException,

//if RelationshipField, Relationship record must be updated/deleted
if (field instanceof RelationshipField) {
Logger.info(this, "Removing relationship link for: " + field.name()
+ " with ID: " + field.id() + " and var name:" + field.variable()
+ ", from Content Type: " + structure.getName() + " with ID: " + structure.id());
StopWatch relationshipLinkWatch = new StopWatch();
relationshipLinkWatch.start();
removeRelationshipLink(field, type, contentTypeAPI);
relationshipLinkWatch.stop();
Logger.info(this, "Removed relationship link: " + field.name() + " with ID: " + field.id()
+ " and var name:" + field.variable() + ", from Content Type: " + structure.getName()
+ " with ID: " + structure.id() + ", elapsed time: " + stopWatch);
}

// rebuild contentlets indexes
Expand Down

0 comments on commit d0653d2

Please sign in to comment.