/*
 * Copyright 1999-2010 Luca Garulli (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.sql;

import java.util.Map;

import com.orientechnologies.orient.core.command.OCommandRequestText;
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
import com.orientechnologies.orient.core.metadata.schema.OSchemaProxy;
import com.orientechnologies.orient.core.metadata.security.ODatabaseSecurityResources;
import com.orientechnologies.orient.core.metadata.security.ORole;

/**
 * SQL CREATE PROPERTY command: Creates a new property in the target class.
 * 
 * @author Luca Garulli
 * 
 */
@SuppressWarnings("unchecked")
public class OCommandExecutorSQLDropClass extends OCommandExecutorSQLPermissionAbstract {
	public static final String	KEYWORD_DROP	= "DROP";
	public static final String	KEYWORD_CLASS	= "CLASS";

	private String							className;

	public OCommandExecutorSQLDropClass parse(final OCommandRequestText iRequest) {
		iRequest.getDatabase().checkSecurity(ODatabaseSecurityResources.COMMAND, ORole.PERMISSION_DELETE);

		init(iRequest.getDatabase(), iRequest.getText());

		final StringBuilder word = new StringBuilder();

		int oldPos = 0;
		int pos = OSQLHelper.nextWord(text, textUpperCase, oldPos, word, true);
		if (pos == -1 || !word.toString().equals(KEYWORD_DROP))
			throw new OCommandSQLParsingException("Keyword " + KEYWORD_DROP + " not found", text, oldPos);

		pos = OSQLHelper.nextWord(text, textUpperCase, pos, word, true);
		if (pos == -1 || !word.toString().equals(KEYWORD_CLASS))
			throw new OCommandSQLParsingException("Keyword " + KEYWORD_CLASS + " not found", text, oldPos);

		pos = OSQLHelper.nextWord(text, textUpperCase, pos, word, false);
		if (pos == -1)
			throw new OCommandSQLParsingException("Expected <class>", text, pos);

		className = word.toString();
		if (className == null)
			throw new OCommandSQLParsingException("Class is null", text, pos);

		return this;
	}

	/**
	 * Execute the DROP CLASS.
	 */
	public Object execute(final Map<Object, Object> iArgs) {
		if (className == null)
			throw new OCommandExecutionException("Can't execute the command because it hasn't been parsed yet");

		((OSchemaProxy) database.getMetadata().getSchema()).dropClassInternal(className);
		((OSchemaProxy) database.getMetadata().getSchema()).saveInternal();

		return null;
	}
}

@@ -874,16 +874,82 @@
 ption;%0D%0A
+import com.orientechnologies.orient.core.metadata.schema.OClass;%0D%0A
 import c
@@ -1166,16 +1166,76 @@
 .ORole;%0D
+%0Aimport com.orientechnologies.orient.core.storage.OCluster;%0D
 %0A%0D%0A/**%0D%0A
@@ -2945,16 +2945,115 @@
 t%22);%0D%0A%0D%0A
+%09%09int clusterId = database.getMetadata().getSchema().getClass(className).getDefaultClusterId();%0D%0A%0D%0A
 %09%09((OSch
@@ -3208,19 +3208,600 @@
 %0D%0A%09%09
-return null
+deleteDefaultCluster(clusterId);%0D%0A%0D%0A%09%09return null;%0D%0A%09%7D%0D%0A%0D%0A%09protected void deleteDefaultCluster(int clusterId) %7B%0D%0A%09%09OCluster cluster = database.getStorage().getClusterById(clusterId);%0D%0A%09%09if (cluster.getName().equalsIgnoreCase(className)) %7B%0D%0A%09%09%09if (isClusterDeletable(clusterId)) %7B%0D%0A%09%09%09%09database.getStorage().dropCluster(clusterId);%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%0D%0A%09protected boolean isClusterDeletable(int clusterId) %7B%0D%0A%09%09for (OClass iClass : database.getMetadata().getSchema().getClasses()) %7B%0D%0A%09%09%09for (int i : iClass.getClusterIds()) %7B%0D%0A%09%09%09%09if (i == clusterId)%0D%0A%09%09%09%09%09return false;%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09return true
 ;%0D%0A%09

Fix by Luca Molino about the extension of drop class command also to the cluster only if has the same name and it's not used by others