<commit_before>/*
 * Copyright 1999-2010 <NAME> (l.garulli--at--orientechnologies.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.orientechnologies.orient.core.metadata.schema;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.orientechnologies.common.util.OArrays;
import com.orientechnologies.orient.core.annotation.OBeforeSerialization;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.record.ODatabaseRecord;
import com.orientechnologies.orient.core.db.record.ORecordElement;
import com.orientechnologies.orient.core.exception.OSchemaException;
import com.orientechnologies.orient.core.metadata.security.ODatabaseSecurityResources;
import com.orientechnologies.orient.core.metadata.security.ORole;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.type.ODocumentWrapperNoClass;

@SuppressWarnings("unchecked")
public class OClassImpl extends ODocumentWrapperNoClass implements OClass {

	protected int											id;
	protected OSchemaShared						owner;
	protected String									name;
	protected Class<?>								javaClass;
	protected int											fixedSize		= 0;
	protected Map<String, OProperty>	properties	= new HashMap<String, OProperty>();
	protected int[]										clusterIds;
	protected int											defaultClusterId;
	protected OClassImpl							superClass;
	protected int[]										polymorphicClusterIds;
	protected List<OClass>						baseClasses;
	protected float										overSize		= 0;
	protected String									shortName;

	/**
	 * Constructor used in unmarshalling.
	 */
	public OClassImpl() {
	}

	/**
	 * Constructor used in unmarshalling.
	 */
	protected OClassImpl(final OSchemaShared iOwner) {
		document = new ODocument(getDatabase());
		owner = iOwner;
	}

	/**
	 * Constructor used in unmarshalling.
	 */
	protected OClassImpl(final OSchemaShared iOwner, final ODocument iDocument) {
		document = iDocument;
		owner = iOwner;
	}

	protected OClassImpl(final OSchemaShared iOwner, final int iId, final String iName, final String iJavaClassName,
			final int[] iClusterIds) throws ClassNotFoundException {
		this(iOwner, iId, iName, iClusterIds);
		javaClass = Class.forName(iJavaClassName);
	}

	protected OClassImpl(final OSchemaShared iOwner, final int iId, final String iName, final int[] iClusterIds) {
		this(iOwner);
		id = iId;
		name = iName;
		clusterIds = iClusterIds;
		polymorphicClusterIds = iClusterIds;
		clusterIds = iClusterIds;
		defaultClusterId = iClusterIds[0];
	}

	public <T> T newInstance() throws InstantiationException, IllegalAccessException {
		if (javaClass == null)
			throw new IllegalArgumentException("Can't create an instance of class '" + name + "' since no Java class was specified");

		return (T) javaClass.newInstance();
	}

	public OClass getSuperClass() {
		return superClass;
	}

	/**
	 * Set the super class.
	 * 
	 * @param iSuperClass
	 *          Super class as OClass instance
	 * @return the object itself.
	 */
	public OClass setSuperClass(final OClass iSuperClass) {
		document.getDatabase().checkSecurity(ODatabaseSecurityResources.SCHEMA, ORole.PERMISSION_UPDATE);
		final String cmd = String.format("alter class %s superclass %s", name, iSuperClass.getName());
		document.getDatabase().command(new OCommandSQL(cmd)).execute();
		setSuperClassInternal(iSuperClass);
		return this;
	}

	public void setSuperClassInternal(final OClass iSuperClass) {
		this.superClass = (OClassImpl) iSuperClass;
		superClass.addBaseClasses(this);
	}

	public String getName() {
		return name;
	}

	public OClass setName(final String iName) {
		document.getDatabase().checkSecurity(ODatabaseSecurityResources.SCHEMA, ORole.PERMISSION_UPDATE);
		final String cmd = String.format("alter class %s name %s", name, iName);
		document.getDatabase().command(new OCommandSQL(cmd)).execute();
		name = iName;
		return this;
	}

	public void setNameInternal(final String iName) {
		document.getDatabase().checkSecurity(ODatabaseSecurityResources.SCHEMA, ORole.PERMISSION_UPDATE);
		owner.changeClassName(name, iName);
		name = iName;
	}

	public String getShortName() {
		return shortName;
	}

	public OClass setShortName(final String iShortName) {
		document.getDatabase().checkSecurity(ODatabaseSecurityResources.SCHEMA, ORole.PERMISSION_UPDATE);
		final String cmd = String.format("alter class %s shortname %s", name, iShortName);
		document.getDatabase().command(new OCommandSQL(cmd)).execute();
		setShortNameInternal(iShortName);<commit_msg>Patch by <NAME> to fix a bug on alter class (issue 403?)<commit_after>/*
 * Copyright 1999-2010 <NAME> (l.garulli--at--orientechnologies.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.orientechnologies.orient.core.metadata.schema;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.orientechnologies.common.util.OArrays;
import com.orientechnologies.orient.core.annotation.OBeforeSerialization;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.record.ODatabaseRecord;
import com.orientechnologies.orient.core.db.record.ORecordElement;
import com.orientechnologies.orient.core.exception.OSchemaException;
import com.orientechnologies.orient.core.metadata.security.ODatabaseSecurityResources;
import com.orientechnologies.orient.core.metadata.security.ORole;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.type.ODocumentWrapperNoClass;

@SuppressWarnings("unchecked")
public class OClassImpl extends ODocumentWrapperNoClass implements OClass {

	protected int											id;
	protected OSchemaShared						owner;
	protected String									name;
	protected Class<?>								javaClass;
	protected int											fixedSize		= 0;
	protected Map<String, OProperty>	properties	= new HashMap<String, OProperty>();
	protected int[]										clusterIds;
	protected int											defaultClusterId;
	protected OClassImpl							superClass;
	protected int[]										polymorphicClusterIds;
	protected List<OClass>						baseClasses;
	protected float										overSize		= 0;
	protected String									shortName;

	/**
	 * Constructor used in unmarshalling.
	 */
	public OClassImpl() {
	}

	/**
	 * Constructor used in unmarshalling.
	 */
	protected OClassImpl(final OSchemaShared iOwner) {
		document = new ODocument(getDatabase());
		owner = iOwner;
	}

	/**
	 * Constructor used in unmarshalling.
	 */
	protected OClassImpl(final OSchemaShared iOwner, final ODocument iDocument) {
		document = iDocument;
		owner = iOwner;
	}

	protected OClassImpl(final OSchemaShared iOwner, final int iId, final String iName, final String iJavaClassName,
			final int[] iClusterIds) throws ClassNotFoundException {
		this(iOwner, iId, iName, iClusterIds);
		javaClass = Class.forName(iJavaClassName);
	}

	protected OClassImpl(final OSchemaShared iOwner, final int iId, final String iName, final int[] iClusterIds) {
		this(iOwner);
		id = iId;
		name = iName;
		clusterIds = iClusterIds;
		polymorphicClusterIds = iClusterIds;
		clusterIds = iClusterIds;
		defaultClusterId = iClusterIds[0];
	}

	public <T> T newInstance() throws InstantiationException, IllegalAccessException {
		if (javaClass == null)
			throw new IllegalArgumentException("Can't create an instance of class '" + name + "' since no Java class was specified");

		return (T) javaClass.newInstance();
	}

	public OClass getSuperClass() {
		return superClass;
	}

	/**
	 * Set the super class.
	 * 
	 * @param iSuperClass
	 *          Super class as OClass instance
	 * @return the object itself.
	 */
	public OClass setSuperClass(final OClass iSuperClass) {
		document.getDatabase().checkSecurity(ODatabaseSecurityResources.SCHEMA, ORole.PERMISSION_UPDATE);
		final String cmd = String.format("alter class %s superclass %s", name, iSuperClass.getName());
		document.getDatabase().command(new OCommandSQL(cmd)).execute();
		setSuperClassInternal(iSuperClass);
		return this;
	}

	public void setSuperClassInternal(final OClass iSuperClass) {
		this.superClass = (OClassImpl) iSuperClass;
		superClass.addBaseClasses(this);
	}

	public String getName() {
		return name;
	}

	public OClass setName(final String iName) {
		document.getDatabase().checkSecurity(ODatabaseSecurityResources.SCHEMA, ORole.PERMISSION_UPDATE);
		final String cmd = String.format("alter class %s name %s", name, iName);
		document.getDatabase().command(new OCommandSQL(cmd)).execute();
		name = iName;
		return this;
	}

	public void setNameInternal(final String iName) {
		document.getDatabase().checkSecurity(ODatabaseSecurityResources.SCHEMA, ORole.PERMISSION_UPDATE);
		owner.changeClassName(name, iName);
		name = iName;
		owner.saveInternal();
	}

	public String getShortName() {
		return shortName;
	}

	public OClass setShortName(final String iShortName) {
		document.getDatabase().checkSecurity(ODatabaseSecurityResources.SCHEMA, ORole.PERMISSION_UPDATE);
		final String cmd = String.format("alter class %s shortname %s", name, iShortName);
		document.getDatabase().command(new OCommandSQL(cmd)).execute();
		setShortNameInternal(iShortName);
{'fraction_non_alphanumeric': 0.09555327677822317, 'fraction_numerical': 0.0030763493987135265, 'mean_word_length': 4.694267515923567, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '683441', 'n_tokens_mistral': 3471, 'n_tokens_neox': 3162, 'n_words': 825}