/*
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * Modified by
 * @author  Data Virtuality GmbH
 * @date    $Date$
 *  
 */
/*
 * EXTENSION
 * ISSUE: DVCORE-2521
 * FIXED BY: Roman Pedchenko
 * DATE: 2014-12-05
 * ADOPTED FROM: TEIID-2153, 2171, 2359, 2149, 2358, 2410, 2271, 2410, 2467, 2429, 2363, 2160, 2461, 2797, 2326, 1557
 * END EXTENSION
 */
// https://issues.jboss.org/browse/TEIID-2471
// https://issues.jboss.org/browse/TEIID-2181
package org.teiid.metadata;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.NavigableMap;
import java.util.concurrent.CopyOnWriteArrayList;

import org.teiid.adminapi.impl.DataPolicyMetadata.PermissionMetaData;
import org.teiid.connector.DataPlugin;
import org.teiid.core.util.NamesUtil;
import org.teiid.core.util.StringUtil;

/**
 * Simple holder for metadata.
 */
public class MetadataStore implements Serializable {

	protected static class Grant implements Serializable {
 	    private static final long serialVersionUID = -3768577122034702953L;
 	    protected String role;
 	    protected PermissionMetaData perm;

		public Grant(String role, PermissionMetaData perm) {
			this.role = role;
			this.perm = perm;
		}

		public String getRole() {
			return role;
		}

		public PermissionMetaData getPermission() {
			return perm;
		}
 	}

	private static final long serialVersionUID = -3130247626435324312L;
	protected NavigableMap<String, Schema> schemas = DVMetadataRecordHolder.initMap(StringUtil.CASE_INSENSITIVE_ORDER);
	protected List<Schema> schemaList = new CopyOnWriteArrayList<Schema>(); //used for a stable ordering
	protected NavigableMap<String, Datatype> datatypes = DVMetadataRecordHolder.initMap(null);
	protected List<Grant> grants;
	
	public NavigableMap<String, Schema> getSchemas() {
		return schemas;
	}
	
	public Schema getSchema(String name) {
		// BUG: errors with umlauts in a schema name
		// ISSUE: DVCORE-362
		// SOLUTION: converting string to upper case
		// FIXED BY: Roman Pedchenko
		// DATE: 2013.04.10
//		return this.schemas.get(name);
		return this.schemas.get(NamesUtil.getCanonicalName(name));
		// END BUG
	}

	public void addSchema(Schema schema) {
		if (this.schemas.put(schema.getName(), schema) != null) {
			// EXTENSION
			// ISSUE: DVCORE-2441
			// FIXED BY: Roman Pedchenko
			// DATE: 2014.10.23
			// ADOPTED FROM: https://issues.jboss.org/browse/TEIID-247, 2166, 2151, 2234, 2581, 2571, 2369, 1386, 2226, 2233, 2224, 2225, 2257
			// throw new DuplicateRecordException(DataPlugin.Util.gs(DataPlugin.Event.TEIID60012, schema.getName()));
			throw new DuplicateRecordException(DataPlugin.Event.TEIID60012, DataPlugin.Util.gs(DataPlugin.Event.TEIID60012, schema.getName()));
			// END EXTENSION
		}		
		this.schemaList.add(schema);
	}

	public List<Schema> getSchemaList() {
		return schemaList;
	}

	public void removeSchema(Schema schema) {
		Schema foundSchema = this.schemas.get(schema.getName());
		if (foundSchema != null) {
			this.schemas.remove(schema.getName());
			this.schemaList.remove(foundSchema);
		}		
	}

	public void addDataTypes(Collection<Datatype> types) {
		if (types != null){
			for (Datatype type:types) {
				addDatatype(type);
			}
		}
	}
	
	public void addDatatype(Datatype datatype) {
		this.datatypes.put(datatype.getName(), datatype);
	}
		
	public NavigableMap<String, Datatype> getDatatypes() {
		return datatypes;
	}
	
	public void merge(MetadataStore store) {
		if (store != null) {
			for (Schema s:store.getSchemaList()) {
				addSchema(s);
			}
			addDataTypes(store.getDatatypes().values());
			addGrants(store.grants);
		}
	}

	void addGrants(List<Grant> g) {
		if (g == null) {
			return;
		}
		if (this.grants == null) {
			this.grants = new ArrayList<Grant>();
		}
		this.grants.addAll(g);
	}
}
