-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 30815 populate the unique fields table when the database valida…
…tion is enabled (#30862) ### Proposed Changes * Create a Initializer to check if the Database validation was enabled https://github.com/dotCMS/core/pull/30862/files#diff-b1a4fb7917e1a3e9e98ded9a8d7f31ffd2b90a7aa873c73ba2a8f35b82282a12R17 * Create a Method to create the table https://github.com/dotCMS/core/pull/30862/files#diff-b0aed2eddcc36be2a2e2f46624345a83c8698db238fe4dc228c0fb13a11e344fR419 * Create a method to drop the table https://github.com/dotCMS/core/pull/30862/files#diff-b0aed2eddcc36be2a2e2f46624345a83c8698db238fe4dc228c0fb13a11e344fR440 * Create a method to populate the table https://github.com/dotCMS/core/pull/30862/files#diff-b0aed2eddcc36be2a2e2f46624345a83c8698db238fe4dc228c0fb13a11e344fR464 * Add WrapInTransaction and CloseDBIfOpened annotation to each method in UniqueFieldDataBaseUtil https://github.com/dotCMS/core/pull/30862/files#diff-b0aed2eddcc36be2a2e2f46624345a83c8698db238fe4dc228c0fb13a11e344fR138-R353 --------- Co-authored-by: fabrizzio-dotCMS <[email protected]>
- Loading branch information
1 parent
e32a4d3
commit 632b9af
Showing
8 changed files
with
1,063 additions
and
634 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
.../java/com/dotcms/contenttype/business/uniquefields/UniqueFieldsValidationInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.dotcms.contenttype.business.uniquefields; | ||
|
||
import com.dotcms.config.DotInitializer; | ||
import com.dotcms.content.elasticsearch.business.ESContentletAPIImpl; | ||
import com.dotcms.contenttype.business.uniquefields.extratable.UniqueFieldDataBaseUtil; | ||
import com.dotmarketing.common.db.DotDatabaseMetaData; | ||
import com.dotmarketing.db.DbConnectionFactory; | ||
import com.dotmarketing.exception.DotDataException; | ||
import com.dotmarketing.util.Logger; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.enterprise.context.Dependent; | ||
import javax.inject.Inject; | ||
import java.sql.SQLException; | ||
|
||
/** | ||
* Initializer in charge of check when dotCMS start up if the Unique Fields Data Base validation was enabled | ||
* to create and populate the unique_fields table. | ||
* | ||
* It check if the table already exists and: | ||
* - If it exists and the Database validation is disabled then drop the table. | ||
* - If it does not exist and the Database validation is enabled then it created and populate it. | ||
* - If it exists and the Database validation is enabled do nothing. | ||
* - If it does not exist and the Database validation is disabled do nothing. | ||
*/ | ||
@Dependent | ||
public class UniqueFieldsValidationInitializer implements DotInitializer { | ||
|
||
private UniqueFieldDataBaseUtil uniqueFieldDataBaseUtil; | ||
private DotDatabaseMetaData dotDatabaseMetaData; | ||
|
||
@Inject | ||
public UniqueFieldsValidationInitializer(final UniqueFieldDataBaseUtil uniqueFieldDataBaseUtil){ | ||
this.uniqueFieldDataBaseUtil = uniqueFieldDataBaseUtil; | ||
this.dotDatabaseMetaData = new DotDatabaseMetaData(); | ||
} | ||
|
||
@Override | ||
public void init() { | ||
final boolean featureFlagDbUniqueFieldValidation = ESContentletAPIImpl.getFeatureFlagDbUniqueFieldValidation(); | ||
boolean uniqueFieldsTableExists = uniqueFieldsTableExists(); | ||
|
||
try { | ||
if (featureFlagDbUniqueFieldValidation && !uniqueFieldsTableExists) { | ||
this.uniqueFieldDataBaseUtil.createTableAnsPopulate(); | ||
} else if (!featureFlagDbUniqueFieldValidation && uniqueFieldsTableExists) { | ||
this.uniqueFieldDataBaseUtil.dropUniqueFieldsValidationTable(); | ||
} | ||
} catch (DotDataException e) { | ||
Logger.error(UniqueFieldsValidationInitializer.class, e); | ||
} | ||
} | ||
|
||
private boolean uniqueFieldsTableExists(){ | ||
try { | ||
return dotDatabaseMetaData.tableExists(DbConnectionFactory.getConnection(), "unique_fields"); | ||
} catch (SQLException e) { | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.