/*
 * DBeaver - Universal Database Manager
 * Copyright (C) 2016-2016 Karl Griesser (fullref@gmail.com)
 * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
 *
 * 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 org.jkiss.dbeaver.ext.exasol.model;

import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.DBPRefreshableObject;
import org.jkiss.dbeaver.model.DBPSaveableObject;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCUtils;
import org.jkiss.dbeaver.model.meta.Property;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSObject;

import java.sql.Date;
import java.sql.ResultSet;

public class ExasolConnection
		implements DBPRefreshableObject, DBPSaveableObject {

	private ExasolDataSource dataSource;
	private String connectionName;
	private String connectionString;
	private String userName;
	private Date created;
	private String comment;
	private Boolean persisted;

	public ExasolConnection(ExasolDataSource dataSource, ResultSet dbResult)
	{
		this.dataSource = dataSource;
		if (dbResult != null) {
			this.persisted = true;
			this.connectionName = JDBCUtils.safeGetString(dbResult,"CONNECTION_NAME");
			this.connectionString = JDBCUtils.safeGetString(dbResult,"CONNECTION_STRING");
			this.created = JDBCUtils.safeGetDate(dbResult, "CREATED");
			this.comment = JDBCUtils.safeGetString(dbResult,"CONNECTION_COMMENT");
			this.userName = JDBCUtils.safeGetString(dbResult, "USER_NAME");
		} else {
			this.connectionName = "new connection";
			this.persisted = false;
		}

	}

	@Override
	@Property(viewable = true, order = 10)
	public String getName()
	{
		return this.connectionName;
	}

	@Property(viewable = true, order = 20)
	public String getConnectionString()
	{
		return this.connectionString;
	}

	@Property(viewable = true, order = 30)
	public Date getCreated()
	{
		return this.created;
	}

	@Property(viewable = true, order = 30)
	public String getUserName()
	{
		return this.userName;
	}

	@Override
	@Property(viewable = true, order = 50)
	public String getDescription()
	{
		return this.comment;
	}

	@Override
	public DBSObject getParentObject()
	{
		return this.dataSource.getContainer();
	}

	@Override
	public ExasolDataSource getDataSource()
	{
		return this.dataSource;
	}

	@Override
	public boolean isPersisted()
	{
		return this.persisted;
	}

	@Override
	public void setPersisted(boolean persisted)
	{
		this.persisted = persisted;

	}

	@Override
	public DBSObject refreshObject(@NotNull DBRProgressMonitor monitor)
			throws DBException
	{
		return this;
	}

}

@@ -830,16 +830,71 @@
 eption;%0A
+import org.jkiss.dbeaver.ext.exasol.tools.ExasolUtils;%0A
 import o
@@ -980,32 +980,80 @@
 SaveableObject;%0A
+import org.jkiss.dbeaver.model.DBPScriptObject;%0A
 import org.jkiss
@@ -1375,17 +1375,33 @@
 leObject
- 
+, DBPScriptObject
 %7B%0A%0A%09priv
@@ -3225,10 +3225,340 @@
 is;%0A%09%7D%0A%0A
+%09@Override%0A%09public String getObjectDefinitionText(DBRProgressMonitor monitor)%0A%09%09%09throws DBException%0A%09%7B%0A%09%09if (getDataSource().isAuthorizedForConnections()) %7B%0A%09%09%09return ExasolUtils.getConnectionDdl(this, monitor);%0A%09%09%7D else %7B%0A%09%09%09return %22User needs full access to dictionary or dba privilege to generate ddl for connections%22;%0A%09%09%7D%0A%09%7D%0A%0A
 %7D%0A

Generate DDL for Connection Objects