All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.object.petra.sql.dsl.DynamicObjectDefinitionLocalizationTable Maven / Gradle / Ivy

There is a newer version: 93.0.0
Show newest version
/**
 * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
 * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
 */

package com.liferay.object.petra.sql.dsl;

import com.liferay.object.model.ObjectDefinition;
import com.liferay.object.model.ObjectField;
import com.liferay.petra.sql.dsl.Column;
import com.liferay.petra.sql.dsl.base.BaseTable;
import com.liferay.petra.string.StringBundler;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.ListUtil;

import java.sql.Types;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Feliphe Marinho
 */
public class DynamicObjectDefinitionLocalizationTable
	extends BaseTable {

	public DynamicObjectDefinitionLocalizationTable(
		ObjectDefinition objectDefinition, List objectFields) {

		super(objectDefinition.getLocalizationDBTableName(), () -> null);

		_objectDefinition = objectDefinition;
		_objectFields = objectFields;

		createColumn(
			objectDefinition.getPKObjectFieldDBColumnName(), Long.class,
			Types.BIGINT, Column.FLAG_DEFAULT);
		createColumn(
			"languageId", String.class, Types.VARCHAR, Column.FLAG_DEFAULT);

		for (ObjectField objectField : objectFields) {
			createColumn(
				objectField.getDBColumnName(),
				DynamicObjectDefinitionTableUtil.getJavaClass(
					objectField.getDBType()),
				DynamicObjectDefinitionTableUtil.getSQLType(
					objectField.getDBType()),
				Column.FLAG_DEFAULT);
		}

		_objectFieldColumns = ListUtil.filter(
			new ArrayList<>(getColumns()),
			column -> {
				if (column.equals(getForeignKeyColumn()) ||
					column.equals(getLanguageIdColumn())) {

					return false;
				}

				return true;
			});
	}

	public String getCreateTableSQL() {
		StringBundler sb = new StringBundler();

		sb.append("create table ");
		sb.append(_objectDefinition.getLocalizationDBTableName());
		sb.append(" (");
		sb.append(_objectDefinition.getPKObjectFieldDBColumnName());
		sb.append(" LONG not null, languageId VARCHAR(10) not null");

		for (ObjectField objectField : _objectFields) {
			sb.append(", ");
			sb.append(objectField.getDBColumnName());
			sb.append(" ");
			sb.append(
				DynamicObjectDefinitionTableUtil.getDataType(
					objectField.getBusinessType(), objectField.getDBType()));
		}

		sb.append(", primary key (");
		sb.append(_objectDefinition.getPKObjectFieldDBColumnName());
		sb.append(", languageId));");

		String sql = sb.toString();

		if (_log.isDebugEnabled()) {
			_log.debug("SQL: " + sql);
		}

		return sql;
	}

	public Column
		getForeignKeyColumn() {

		return (Column)
			getColumn(_objectDefinition.getPKObjectFieldDBColumnName());
	}

	public String getForeignKeyColumnName() {
		Column
			foreignKeyColumn = getForeignKeyColumn();

		return foreignKeyColumn.getName();
	}

	public Column
		getLanguageIdColumn() {

		return (Column)
			getColumn("languageId");
	}

	public ObjectDefinition getObjectDefinition() {
		return _objectDefinition;
	}

	public List>
		getObjectFieldColumns() {

		return _objectFieldColumns;
	}

	public List getObjectFields() {
		return _objectFields;
	}

	private static final Log _log = LogFactoryUtil.getLog(
		DynamicObjectDefinitionLocalizationTable.class);

	private final ObjectDefinition _objectDefinition;
	private final List>
		_objectFieldColumns;
	private final List _objectFields;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy