/*
 * DBeaver - Universal Database Manager
 * Copyright (C) 2010-2016 Serge Rieder (serge@jkiss.org)
 * Copyright (C) 2011-2012 Eugene Fradkin (eugene.fradkin@gmail.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License (version 2)
 * as published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.jkiss.dbeaver.ext.postgresql.tools.maintenance;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.IWorkbenchWindow;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.ext.postgresql.model.PostgreObject;
import org.jkiss.dbeaver.ext.postgresql.model.PostgreTableBase;
import org.jkiss.dbeaver.model.DBPEvaluationContext;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.tools.IExternalTool;
import org.jkiss.utils.CommonUtils;

import java.util.Collection;
import java.util.List;

/**
 * Table truncate
 */
public class PostgreToolTruncate implements IExternalTool
{
    @Override
    public void execute(IWorkbenchWindow window, IWorkbenchPart activePart, Collection<DBSObject> objects) throws DBException
    {
        List<PostgreTableBase> tables = CommonUtils.filterCollection(objects, PostgreTableBase.class);
        if (!tables.isEmpty()) {
            SQLDialog dialog = new SQLDialog(activePart.getSite(), tables);
            dialog.open();
        }
    }

    static class SQLDialog extends TableToolDialog {

        public SQLDialog(IWorkbenchPartSite partSite, Collection<PostgreTableBase> selectedTables)
        {
            super(partSite, "Truncate table(s)", selectedTables);
        }

        @Override
        protected void generateObjectCommand(List<String> lines, PostgreObject object) {
            if (object instanceof PostgreTableBase) {
                lines.add("TRUNCATE TABLE " + ((PostgreTableBase)object).getFullyQualifiedName(DBPEvaluationContext.DDL));
            }
        }

        @Override
        protected void createControls(Composite parent) {
            createObjectsSelector(parent);
        }
    }

}

@@ -912,25 +912,142 @@
 swt.
-widgets.Composite
+layout.GridData;%0Aimport org.eclipse.swt.widgets.Button;%0Aimport org.eclipse.swt.widgets.Composite;%0Aimport org.eclipse.swt.widgets.Group
 ;%0Aim
@@ -1475,16 +1475,53 @@
 alTool;%0A
+import org.jkiss.dbeaver.ui.UIUtils;%0A
 import o
@@ -2144,16 +2144,133 @@
 alog %7B%0A%0A
+        private Button onlyCheck;%0A        private Button restartIdentityCheck;%0A        private Button cascadeCheck;%0A%0A
         
@@ -2628,18 +2628,21 @@
     
-lines.add(
+String sql = 
 %22TRU
@@ -2652,16 +2652,105 @@
 TE TABLE
+%22;%0A                if (onlyCheck.getSelection()) sql += %22 ONLY%22;%0A                sql += %22
  %22 + ((P
@@ -2765,16 +2765,17 @@
 bleBase)
+ 
 object).
@@ -2825,111 +2825,1436 @@
 DDL)
-);%0A            %7D%0A        %7D%0A%0A        @Override%0A        protected void createControls(Composite parent) %7B
+;%0A                if (restartIdentityCheck.getSelection())%0A                    sql += %22 RESTART IDENTITY%22;%0A                else%0A                    sql += %22 CONTINUE IDENTITY%22;%0A                if (cascadeCheck.getSelection())%0A                    sql += %22 CASCADE%22;%0A                else%0A                    sql += %22 RESTRICT%22;%0A                lines.add(sql);%0A            %7D%0A        %7D%0A%0A        @Override%0A        protected void createControls(Composite parent) %7B%0A            Group optionsGroup = UIUtils.createControlGroup(parent, %22Options%22, 1, 0, 0);%0A            optionsGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));%0A            onlyCheck = UIUtils.createCheckbox(optionsGroup, %22Only%22, %22If ONLY is not specified, the table and all its descendant tables (if any) are truncated.%22, false, 0);%0A            onlyCheck.addSelectionListener(SQL_CHANGE_LISTENER);%0A            restartIdentityCheck = UIUtils.createCheckbox(optionsGroup, %22Restart identity%22, %22Automatically restart sequences owned by columns of the truncated table(s).%22, false, 0);%0A            restartIdentityCheck.addSelectionListener(SQL_CHANGE_LISTENER);%0A            cascadeCheck = UIUtils.createCheckbox(optionsGroup, %22Cascade%22, %22Automatically truncate all tables that have foreign-key references to any of the named tables, or to any tables added to the group due to CASCADE.%22, false, 0);%0A            cascadeCheck.addSelectionListener(SQL_CHANGE_LISTENER);%0A
 %0A   

Truncate table