-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements.  See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You 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.

CREATE TABLE USER_TABLE(
	USERID VARCHAR(200) NOT NULL PRIMARY KEY,
	DESCRIPTION VARCHAR(500),
	ISADMIN  BOOLEAN 
);

CREATE TABLE  GROUP_TABLE(
	GROUPID VARCHAR(200) NOT NULL PRIMARY KEY,
	DESCRIPTION VARCHAR(500)
);

CREATE TABLE USER_GROUPS_TABLE (
	GROUPID VARCHAR(200) NOT NULL, 
	USERID VARCHAR(200) NOT NULL,
	FOREIGN KEY (GROUPID) REFERENCES GROUP_TABLE(GROUPID),
	FOREIGN KEY (USERID) REFERENCES USER_TABLE (USERID)
);

CREATE TABLE RESOURCE_TABLE(
	RESOURCEID VARCHAR(200) NOT NULL ,
	OWNER VARCHAR(500) NOT NULL,
	CREATED TIMESTAMP,
	PRIMARY KEY(RESOURCEID)
);

CREATE TABLE GROUP_GROUP_TABLE(
	GROUPID VARCHAR(200) NOT NULL,
	CONTAINED_GROUPID VARCHAR(200) NOT NULL,
	FOREIGN KEY (GROUPID) REFERENCES GROUP_TABLE(GROUPID),
	FOREIGN KEY (CONTAINED_GROUPID) REFERENCES GROUP_TABLE(GROUPID)
);

CREATE TABLE HOSTDESC_TABLE (
	RESOURCEID VARCHAR(200) NOT NULL,
	HOST_NAME VARCHAR(200) NOT NULL PRIMARY KEY,
	HOSTDESC_STR CLOB NOT NULL,
	FOREIGN KEY (RESOURCEID) REFERENCES RESOURCE_TABLE(RESOURCEID)
);

CREATE TABLE APPDESC_TABLE(
	RESOURCEID VARCHAR(200) NOT NULL,
	QNAME VARCHAR(200) NOT NULL,
	HOST_NAME VARCHAR(200) NOT NULL,
	APPDESC_STR CLOB NOT NULL,
	PRIMARY KEY (QNAME,HOST_NAME),
	FOREIGN KEY (RESOURCEID) REFERENCES RESOURCE_TABLE(RESOURCEID)
);

CREATE TABLE SERVICE_MAP_TABLE(
	RESOURCEID VARCHAR(200) NOT NULL,
	QNAME VARCHAR(200) NOT NULL PRIMARY KEY,
	SERVICEMAP_STR CLOB NOT NULL,
	AWSDL_STR CLOB NOT NULL,
	FOREIGN KEY (RESOURCEID) REFERENCES RESOURCE_TABLE(RESOURCEID)
);

CREATE TABLE CWSDL_TABLE(
	RESOURCEID VARCHAR(200) NOT NULL,
	QNAME VARCHAR(200) NOT NULL PRIMARY KEY,
	WSDL_STR CLOB NOT NULL,
	TIME_STAMP BIGINT,
	LIFE_TIME BIGINT,
	PORT_TYPE_NAME VARCHAR(200) NOT NULL,
	FOREIGN KEY (RESOURCEID) REFERENCES RESOURCE_TABLE(RESOURCEID)
);


CREATE TABLE CAPABILITY_TABLE(
        OWNER VARCHAR(200) NOT NULL,
        RESOURCEID VARCHAR(200) NOT NULL,
        ALLOWED_ACTOR VARCHAR(200) NOT NULL,
        ISUSER BOOLEAN,
        ACTION_TYPE VARCHAR(200) NOT NULL,
        ASSERTIONS CLOB,
        NOTBEFORE TIMESTAMP,
        NOTAFTER TIMESTAMP,
        PRIMARY KEY (RESOURCEID,ALLOWED_ACTOR),
        FOREIGN KEY (OWNER) REFERENCES USER_TABLE (USERID)
 );
 
CREATE TABLE DOC_TABLE(
	RESOURCEID VARCHAR(200) NOT NULL,
	DOC_STR CLOB NOT NULL,
	FOREIGN KEY (RESOURCEID) REFERENCES RESOURCE_TABLE(RESOURCEID)
);

CREATE TABLE OGCE_RESOURCE_TABLE(
    RESOURCEID VARCHAR(200) NOT NULL,
    RESOURCENAME VARCHAR(200),
    RESOURCETYPE VARCHAR(200) NOT NULL,
    RESOURCEDESC CLOB,
    RESOURCEDOCUMENT CLOB,
    PARENTTYPEDID VARCHAR(200),
    CREATED TIMESTAMP,
    FOREIGN KEY (RESOURCEID) REFERENCES RESOURCE_TABLE(RESOURCEID)
);
