src-db/database/model/functions/AD_TABLE_IMPORT.xml
author Antonio Moreno <antonio.moreno@openbravo.com>
Mon, 01 Aug 2011 17:04:08 +0200
changeset 13367 0951c9cfecef
parent 11391 af3c2ae60fce
child 13436 7f7af04a2913
permissions -rw-r--r--
Fixed issue 18116. Names of common columns will now be generated correctly
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION AD_TABLE_IMPORT">
adrian@94
     3
    <function name="AD_TABLE_IMPORT" type="NULL">
juanpablo@1605
     4
      <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
antonio@735
     5
        <default/>
antonio@735
     6
      </parameter>
juanpablo@1605
     7
      <parameter name="p_ad_table_id" type="VARCHAR" mode="in">
antonio@735
     8
        <default/>
antonio@735
     9
      </parameter>
gorkaion@239
    10
      <body><![CDATA[/*************************************************************************
juanpablo@771
    11
  * The contents of this file are subject to the Compiere Public
juanpablo@771
    12
  * License 1.1 ("License"); You may not use this file except in
juanpablo@771
    13
  * compliance with the License. You may obtain a copy of the License in
juanpablo@771
    14
  * the legal folder of your Openbravo installation.
carlos@0
    15
  * Software distributed under the License is distributed on an
carlos@0
    16
  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
carlos@0
    17
  * implied. See the License for the specific language governing rights
carlos@0
    18
  * and limitations under the License.
juanpablo@778
    19
  * The Original Code is  Compiere  ERP &  Business Solution
juanpablo@771
    20
  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
carlos@0
    21
  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
carlos@0
    22
  * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
carlos@0
    23
  * All Rights Reserved.
ggi@6701
    24
  * Contributor(s): Openbravo SLU
asier@11391
    25
  * Contributions are Copyright (C) 2001-2011 Openbravo, S.L.U.
juanpablo@771
    26
  *
juanpablo@771
    27
  * Specifically, this derivative work is based upon the following Compiere
juanpablo@771
    28
  * file and version.
carlos@0
    29
  *************************************************************************
carlos@0
    30
  * $Id: AD_Table_Import.sql,v 1.8 2003/01/18 05:34:25 jjanke Exp $
carlos@0
    31
  ***
carlos@0
    32
  * Title: Import Table Column Definition
carlos@0
    33
  * Description:
carlos@0
    34
  *   Create Columns of Table not existing as a Dictionary Column
carlos@0
    35
  ************************************************************************/
carlos@0
    36
  -- Logistice
carlos@0
    37
  v_ResultStr VARCHAR2(2000):='';
carlos@0
    38
  v_Message VARCHAR2(2000):='';
carlos@0
    39
  v_Result NUMBER:=1; -- 0=failure
juanpablo@1605
    40
  v_Record_ID VARCHAR2(32);
juanpablo@1605
    41
  v_AD_User_ID VARCHAR2(32):='0';
asier@1929
    42
  v_module_table_id varchar2(32);
asier@1929
    43
  v_module_id varchar2(32);
asier@1929
    44
  db_prefix varchar2(30);
carlos@0
    45
  -- Parameter
carlos@0
    46
  TYPE RECORD IS REF CURSOR;
carlos@0
    47
    Cur_Parameter RECORD;
carlos@0
    48
    -- Parameter Variables
carlos@0
    49
    --
carlos@0
    50
    Cur_Column RECORD;
asier@5167
    51
    Cur_CommonCols RECORD;
asier@11391
    52
    -- 
juanpablo@1605
    53
    v_NextNo VARCHAR2(32) ;
carlos@0
    54
    v_count NUMBER(10):=0;
carlos@0
    55
    -- Added by Ismael Ciordia
juanpablo@1605
    56
    v_AD_Reference_ID VARCHAR2(32);
juanpablo@1605
    57
    v_AD_Reference_Value_ID VARCHAR2(32);
carlos@0
    58
    v_IsParent CHAR(1):='N';
carlos@0
    59
    v_IsKey CHAR(1):='N';
carlos@0
    60
    v_IsIdentifier CHAR(1):='N';
carlos@0
    61
    v_IsSessionAttr CHAR(1):='N';
carlos@0
    62
    v_IsUpdateable CHAR(1):='Y';
carlos@0
    63
    v_DefaultValue NVARCHAR2(2000):='';
carlos@0
    64
    v_SeqNo NUMBER(10) ;
antonio@13367
    65
    v_columnNameOrg VARCHAR2(40) ;
carlos@0
    66
    v_columnName VARCHAR2(40) ;
asier@5167
    67
    v_TableName  VARCHAR2(40) ;
carlos@0
    68
    v_LastColumnName VARCHAR2(40) ;
carlos@0
    69
    v_varchar2 VARCHAR2(32767) ;
carlos@0
    70
    v_FieldLength NUMBER(10) ;
juanpablo@1605
    71
    v_PInstance_Log_ID VARCHAR2(32);
juanpablo@1605
    72
    v_Client_ID VARCHAR2(32);
asier@1748
    73
    v_Aux NUMBER;
asier@5207
    74
    v_missingColumns boolean;
antonio@5670
    75
    v_CorrectType CHAR(1):='Y';
carlos@0
    76
  BEGIN
carlos@0
    77
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
    78
      --  Update AD_PInstance
carlos@0
    79
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
    80
      v_ResultStr:='PInstanceNotFound';
carlos@0
    81
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
    82
      -- Get Parameters
carlos@0
    83
      v_ResultStr:='ReadingParameters';
carlos@0
    84
      FOR Cur_Parameter IN
asier@799
    85
        (SELECT i.Record_ID, i.AD_User_ID, p.ParameterName, p.P_String, p.P_Number, p.P_Date, p.AD_CLIENT_ID
carlos@0
    86
        FROM AD_PInstance i
carlos@0
    87
        LEFT JOIN AD_PInstance_Para p
carlos@0
    88
          ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
    89
        WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
    90
        ORDER BY p.SeqNo
carlos@0
    91
        )
carlos@0
    92
      LOOP
carlos@0
    93
        v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
    94
        v_AD_User_ID:=Cur_Parameter.AD_User_ID;
asier@799
    95
        v_Client_ID:=Cur_Parameter.AD_CLIENT_ID;
carlos@0
    96
      END LOOP; -- Get Parameter
carlos@0
    97
    ELSE
carlos@0
    98
      v_Record_ID:=p_AD_Table_ID;
carlos@0
    99
    END IF;
carlos@0
   100
    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
carlos@0
   101
  BEGIN --BODY
carlos@0
   102
    IF(v_Record_ID IS NOT NULL) THEN
carlos@0
   103
      SELECT COALESCE(MAX(SeqNo), 0) + 10
carlos@0
   104
      INTO v_SeqNo
carlos@0
   105
      FROM AD_Column
carlos@0
   106
      WHERE AD_Table_ID=v_Record_ID;
carlos@0
   107
    ELSE
carlos@0
   108
      v_SeqNo:=0;
carlos@0
   109
    END IF;
asier@1929
   110
    
asier@1929
   111
          
asier@1929
   112
      select p.ad_module_id
asier@1929
   113
        into v_module_table_id
asier@1929
   114
        from ad_table t, ad_package p
asier@1929
   115
      where t.ad_table_id = v_Record_ID
asier@1929
   116
        and t.ad_package_id = p.ad_package_id;
asier@1929
   117
        
carlos@0
   118
    FOR Cur_Column IN
carlos@0
   119
      (SELECT Column_Name, Data_Type, Data_Length, Nullable, AD_Table_ID, -- added by Ismael Ciordia
asier@1748
   120
        uc.DATA_PRECISION, uc.DATA_SCALE, DATA_DEFAULT, Table_Name, uc.COLUMN_ID
carlos@0
   121
      FROM User_Tab_Columns uc, AD_Table t
carlos@0
   122
      WHERE uc.Table_Name=UPPER(t.TableName) AND NOT EXISTS
carlos@0
   123
        (SELECT *
carlos@0
   124
        FROM AD_Table t, AD_Column c
carlos@0
   125
        WHERE t.AD_Table_ID=c.AD_Table_ID  AND uc.Table_Name=UPPER(t.TableName) AND uc.Column_Name=UPPER(c.ColumnName)
carlos@0
   126
        )
carlos@0
   127
        AND(v_Record_ID=t.AD_Table_ID OR v_Record_ID IS NULL) -- added by Ismael Ciordia
carlos@0
   128
      ORDER BY uc.COLUMN_ID
carlos@0
   129
      )
carlos@0
   130
    LOOP
asier@1653
   131
      AD_Sequence_Next('AD_Column', '0', v_NextNo) ; -- get ID
carlos@0
   132
      -- Added by Ismael Ciordia
carlos@0
   133
      v_AD_Reference_ID:=NULL;
carlos@0
   134
      v_AD_Reference_Value_ID:=NULL;
carlos@0
   135
      v_IsParent:='N';
carlos@0
   136
      v_IsKey:='N';
carlos@0
   137
      v_IsIdentifier:='N';
carlos@0
   138
      v_IsSessionAttr:='N';
carlos@0
   139
      v_IsUpdateable:='Y';
carlos@0
   140
      v_varchar2:=Cur_Column.DATA_DEFAULT;
carlos@0
   141
      v_varchar2:=SUBSTR(v_varchar2, 1, 2000) ;
antonio@5670
   142
      v_CorrectType:='Y';
gorkaion@239
   143
      IF(INSTR(v_varchar2, '''')<>0) THEN
carlos@0
   144
        v_varchar2:=SUBSTR(SUBSTR(v_varchar2, 2, 1999), 1, INSTR(SUBSTR(v_varchar2, 2, 1999), '''') -1) ;
carlos@0
   145
      ELSE
carlos@285
   146
        v_varchar2:=TRIM(REPLACE(REPLACE(v_varchar2, REPLACE('now ()',' ',''), '@#Date@'), CHR(10), '')) ;
carlos@0
   147
      END IF;
carlos@0
   148
      v_DefaultValue:=v_varchar2;
carlos@0
   149
      IF(UPPER(Cur_Column.Column_Name)=UPPER(Cur_Column.Table_Name) ||'_ID') THEN --ID column
carlos@0
   150
        v_AD_Reference_ID:=13;
carlos@0
   151
        v_IsKey:='Y';
carlos@0
   152
        v_IsUpdateable:='N';
carlos@0
   153
      ELSIF(UPPER(Cur_Column.Column_Name) IN('AD_CLIENT_ID', 'AD_ORG_ID')) THEN
carlos@0
   154
        v_AD_Reference_ID:=19;
carlos@0
   155
        v_DefaultValue:='@'||Cur_Column.Column_Name||'@';
carlos@0
   156
        v_IsUpdateable:='N';
carlos@0
   157
        v_IsSessionAttr:='Y';
carlos@0
   158
      ELSIF(UPPER(Cur_Column.Column_Name) IN('UPDATED', 'CREATED')) THEN
asier@2163
   159
        v_AD_Reference_ID:='16';
carlos@0
   160
        v_IsUpdateable:='N';
carlos@0
   161
      ELSIF(UPPER(Cur_Column.Column_Name) IN('UPDATEDBY', 'CREATEDBY')) THEN
asier@2163
   162
        v_AD_Reference_ID:='30';
carlos@0
   163
        v_IsUpdateable:='N';
carlos@0
   164
      ELSIF(UPPER(Cur_Column.Column_Name) IN('NAME')) THEN
carlos@0
   165
        v_IsIdentifier:='Y';
asier@2163
   166
      ELSIF(UPPER(Cur_Column.Column_Name) IN('M_PRODUCT_ID')) THEN
asier@2163
   167
        v_AD_Reference_ID:='30';
asier@2163
   168
        v_AD_Reference_Value_ID:='800060';
asier@2163
   169
      ELSIF(UPPER(Cur_Column.Column_Name) IN ('C_BPARTNER_ID')) THEN 
asier@2163
   170
        v_AD_Reference_ID:='30';
asier@2163
   171
        v_AD_Reference_Value_ID:='800057';
carlos@0
   172
      ELSIF(UPPER(Cur_Column.Column_Name) IN('M_ATTRIBUTESETINSTANCE_ID')) THEN
asier@2163
   173
        v_AD_Reference_ID:='35';
carlos@0
   174
      ELSIF(UPPER(Cur_Column.Column_Name) LIKE '%_LOCATION_ID') THEN
asier@2163
   175
        v_AD_Reference_ID:='30';
asier@2163
   176
        v_AD_Reference_Value_ID:='21';
carlos@0
   177
      ELSIF(UPPER(Cur_Column.Column_Name) LIKE '%_LOCATOR%_ID') THEN
asier@2163
   178
        v_AD_Reference_ID:='30';
asier@2163
   179
        v_AD_Reference_Value_ID:='31';
carlos@0
   180
      ELSIF(UPPER(Cur_Column.Column_Name) LIKE '%_ACCT') THEN
asier@2163
   181
        v_AD_Reference_ID:='30';
asier@2163
   182
        v_AD_Reference_Value_ID:='25';
carlos@0
   183
      ELSIF(UPPER(Cur_Column.Column_Name) LIKE '%_ID') THEN
asier@2163
   184
        v_AD_Reference_ID:='19';
carlos@0
   185
      ELSIF(UPPER(Cur_Column.Column_Name) IN('LINE', 'SEQNO')) THEN
carlos@0
   186
        v_DefaultValue:='@SQL=SELECT COALESCE(MAX('||Cur_Column.Column_Name||'),0)+10 AS DefaultValue FROM '||Cur_Column.Table_Name||' WHERE xxParentColumn=@xxParentColumn@';
carlos@0
   187
      END IF;
carlos@0
   188
      IF(UPPER(v_LastColumnName)='UPDATEDBY' AND UPPER(Cur_Column.Column_Name) LIKE '%_ID') THEN
carlos@0
   189
        v_IsParent:='Y';
carlos@0
   190
        v_IsUpdateable:='N';
carlos@0
   191
      END IF;
carlos@0
   192
      --added by Pablo Sarobe
carlos@0
   193
      IF(Cur_Column.Data_Type IN('VARCHAR2', 'CHAR')) THEN
carlos@0
   194
        v_FieldLength:=Cur_Column.Data_Length;
carlos@0
   195
      ELSIF(Cur_Column.Data_Type IN('NVARCHAR2', 'NCHAR')) THEN
carlos@0
   196
        v_FieldLength:=Cur_Column.Data_Length/2;
asier@3760
   197
      ELSIF(Cur_Column.Data_Type IN('DATE', 'TIMESTAMP')) THEN
carlos@0
   198
        v_FieldLength:=19;
carlos@0
   199
      ELSIF(Cur_Column.Data_Type IN('NUMBER')) THEN
carlos@0
   200
        v_FieldLength:=COALESCE(Cur_Column.Data_Precision, 10) +2;
carlos@0
   201
      ELSE
carlos@0
   202
        v_FieldLength:=Cur_Column.Data_Length;
carlos@0
   203
      END IF;
carlos@0
   204
      IF(v_AD_Reference_ID IS NULL) THEN
carlos@285
   205
        IF(Cur_Column.Data_Type IN('CHAR','BPCHAR') AND Cur_Column.Data_Length=1) THEN
juanpablo@1605
   206
          v_AD_Reference_ID:='20';
antonio@5697
   207
        ELSIF(Cur_Column.Data_Type IN('VARCHAR', 'VARCHAR2', 'NVARCHAR2', 'CHAR', 'NCHAR') AND Cur_Column.Data_Length=4000) THEN
juanpablo@1605
   208
          v_AD_Reference_ID:='14';
antonio@5697
   209
        ELSIF(Cur_Column.Data_Type IN('VARCHAR', 'VARCHAR2', 'NVARCHAR2', 'CHAR', 'NCHAR')) THEN
juanpablo@1605
   210
          v_AD_Reference_ID:='10';
carlos@0
   211
        ELSIF(Cur_Column.Data_Type='NUMBER' AND Cur_Column.DATA_SCALE=0) THEN
juanpablo@1605
   212
          v_AD_Reference_ID:='11';
carlos@0
   213
        ELSIF(Cur_Column.Data_Type='NUMBER' AND UPPER(Cur_Column.Column_Name) LIKE '%AMT%') THEN
juanpablo@1605
   214
          v_AD_Reference_ID:='12';
carlos@0
   215
        ELSIF(Cur_Column.Data_Type='NUMBER' AND UPPER(Cur_Column.Column_Name) LIKE '%QTY%') THEN
juanpablo@1605
   216
          v_AD_Reference_ID:='29';
carlos@0
   217
        ELSIF(Cur_Column.Data_Type='NUMBER') THEN
juanpablo@1605
   218
          v_AD_Reference_ID:='22';
asier@5164
   219
        ELSIF(Cur_Column.Data_Type IN ('DATE', 'TIMESTAMP')) THEN
juanpablo@1605
   220
          v_AD_Reference_ID:='15';
carlos@0
   221
        ELSE
juanpablo@1605
   222
          v_AD_Reference_ID:='10'; -- if not found, use String
antonio@5670
   223
          v_CorrectType:='N';
carlos@0
   224
        END IF;
carlos@0
   225
      END IF;
carlos@0
   226
      v_columnName:=InitCap(Cur_Column.Column_Name) ;
gorkaion@239
   227
      IF(INSTR(v_columnName, '_')<>0 AND INSTR(v_columnName, '_')<5) THEN
carlos@0
   228
        v_columnName:=UPPER(SUBSTR(v_columnName, 1, INSTR(v_columnName, '_'))) ||SUBSTR(v_columnName, INSTR(v_columnName, '_') +1, 40) ;
carlos@0
   229
      END IF;
carlos@0
   230
      IF(v_columnName LIKE '%_Id') THEN
carlos@0
   231
        v_columnName:=SUBSTR(v_columnName, 1, LENGTH(v_columnName) -3) ||'_ID';
carlos@0
   232
      END IF;
antonio@13367
   233
      -- Some columns need to have specific names for DAL to work correctly:
antonio@13367
   234
      v_columnNameOrg:=v_columnName;
antonio@13367
   235
      IF(v_columnName = 'AD_Org_ID') THEN
antonio@13367
   236
        v_columnName:='Organization';
antonio@13367
   237
      END IF;
antonio@13367
   238
      IF(v_columnName = 'AD_Client_ID') THEN
antonio@13367
   239
        v_columnName:='Client';
antonio@13367
   240
      END IF;
antonio@13367
   241
      IF(v_columnName = 'Created') THEN
antonio@13367
   242
        v_columnName:='Creation Date';
antonio@13367
   243
      END IF;
antonio@13367
   244
      IF(v_columnName = 'Createdby') THEN
antonio@13367
   245
        v_columnName:='Created By';
antonio@13367
   246
      END IF;
antonio@13367
   247
      IF(v_columnName = 'Updated') THEN
antonio@13367
   248
        v_columnName:='Updated';
antonio@13367
   249
      END IF;
antonio@13367
   250
      IF(v_columnName = 'Updatedby') THEN
antonio@13367
   251
        v_columnName:='Updated By';
antonio@13367
   252
      END IF;
antonio@13367
   253
      IF(v_columnName = 'Isactive') THEN
antonio@13367
   254
        v_columnName:='Active';
antonio@13367
   255
      END IF;
asier@1929
   256
      IF substr(upper(v_columnName),1,3)='EM_' then
asier@3319
   257
        db_prefix := substr(v_columnName,4,instr(v_columnName,'_',1,2)-4);
asier@3254
   258
        DBMS_OUTPUT.PUT_LINE('Prefix:'||db_prefix);
asier@1929
   259
        select max(ad_module_id)
asier@1929
   260
          into v_module_id
asier@1929
   261
          from ad_module_dbprefix
asier@3254
   262
         where upper(name) = upper(db_prefix);
asier@3254
   263
          
asier@1929
   264
        if v_module_id is null then
asier@1929
   265
          v_module_id := v_module_table_id;
asier@1929
   266
        end if;
asier@1929
   267
      else
asier@1929
   268
        v_module_id := v_module_table_id;
asier@1929
   269
      end if;
asier@11391
   270
      
asier@11391
   271
       --Check if it is necessary to recalculate positions
asier@11391
   272
      SELECT count(*)
asier@11391
   273
        INTO v_Aux
asier@11391
   274
        FROM AD_COLUMN
asier@11391
   275
       WHERE POSITION = Cur_Column.COLUMN_ID;
asier@11391
   276
       
asier@11391
   277
      IF v_Aux!=0 THEN
asier@11391
   278
        UPDATE AD_COLUMN C
asier@11391
   279
           SET POSITION = (SELECT COLUMN_ID
asier@11391
   280
                             FROM USER_TAB_COLUMNS U,
asier@11391
   281
                                  AD_TABLE T
asier@11391
   282
                            WHERE C.AD_TABLE_ID = T.AD_TABLE_ID
asier@11391
   283
                              AND U.TABLE_NAME = UPPER(T.TABLENAME)
asier@11391
   284
                              AND U.COLUMN_NAME = UPPER(C.COLUMNNAME))
asier@11391
   285
         WHERE AD_TABLE_ID = Cur_Column.AD_Table_ID
asier@11391
   286
          AND exists (select 1 
asier@11391
   287
                        from ad_module m 
asier@11391
   288
                       where m.ad_module_id = c.ad_module_id
asier@11391
   289
                         and isindevelopment ='Y');
asier@11391
   290
      END IF;
asier@11391
   291
      
antonio@5670
   292
      IF (v_CorrectType='Y') THEN
antonio@5670
   293
        DBMS_OUTPUT.PUT_LINE('Inserting Column:'||v_columnName||' to module:'||v_module_id);
asier@1748
   294
      
antonio@5670
   295
        INSERT
antonio@5670
   296
        INTO AD_COLUMN
antonio@5670
   297
          (
antonio@5670
   298
            AD_COLUMN_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
antonio@5670
   299
            CREATED, CREATEDBY, UPDATED, UPDATEDBY,
antonio@5670
   300
            NAME, COLUMNNAME, AD_TABLE_ID,
antonio@5670
   301
            AD_REFERENCE_ID, FIELDLENGTH, ISKEY, ISPARENT,
antonio@5670
   302
            ISMANDATORY, ISIDENTIFIER, SEQNO, ISTRANSLATED,
antonio@5670
   303
            ISENCRYPTED, ISUPDATEABLE, AD_REFERENCE_VALUE_ID,
antonio@5670
   304
            AD_VAL_RULE_ID, DEFAULTVALUE, ISSESSIONATTR, 
antonio@5670
   305
            POSITION, aD_module_id
antonio@5670
   306
          )
antonio@5670
   307
          VALUES
antonio@5670
   308
          (v_NextNo, '0', '0', 'Y',
antonio@5670
   309
          now(), '0', now(), '0',
antonio@13367
   310
           v_columnName,v_columnNameOrg, Cur_Column.AD_Table_ID,
antonio@5670
   311
          v_AD_Reference_ID, v_FieldLength, v_IsKey, v_IsParent,
antonio@5670
   312
         (CASE Cur_Column.Nullable WHEN 'Y' THEN 'N' ELSE 'Y' END),
antonio@5670
   313
          v_IsIdentifier, v_SeqNo, 'N', 'N', v_IsUpdateable, v_AD_Reference_Value_ID, 
asier@9386
   314
          null, v_DefaultValue, v_IsSessionAttr,
antonio@5670
   315
          Cur_Column.COLUMN_ID, v_module_id);
antonio@5670
   316
          --
antonio@5670
   317
	  v_count:=v_count + 1;
antonio@5670
   318
      ELSE
antonio@5677
   319
        v_Result :=0;
antonio@5670
   320
	v_Message:=v_Message || '@WrongColumnType@: ' ||v_columnName||'. ';
antonio@5670
   321
      END IF;
carlos@0
   322
      -- Added by Ismael Ciordia
carlos@0
   323
      v_SeqNo:=v_SeqNo + 10;
carlos@0
   324
      v_LastColumnName:=Cur_Column.Column_Name;
carlos@0
   325
      -- Falta: insert de AD_Element
asier@1027
   326
       DBMS_OUTPUT.PUT_LINE('adding Table ' || InitCap(Cur_Column.Table_Name) || ' Column ' || InitCap(Cur_Column.Column_Name)) ;
asier@1027
   327
      
carlos@0
   328
    END LOOP; --  All new columns
carlos@0
   329
    -- Summary info
antonio@5670
   330
    v_Message:=v_Message || '@Created@ = ' || v_count;
asier@5167
   331
    
asier@5167
   332
    
asier@5167
   333
    --Check common columns
asier@5167
   334
    IF (v_Record_ID is not null) THEN
asier@5207
   335
      v_missingColumns := false;
asier@5167
   336
      FOR Cur_CommonCols IN (select columnname
asier@5167
   337
                              from ad_column c
asier@5167
   338
                             where c.ad_table_id = '100'
antonio@13367
   339
                               and lower(c.columnname) in ('client','organization','active','creation date','updated','created by','updated by')
asier@5167
   340
                               and not exists (select 1 
asier@5167
   341
                                                 from ad_column c1
asier@5167
   342
                                                where c1.ad_table_id = v_Record_ID
asier@5167
   343
                                                 and lower(c1.columnname) = lower(c.columnname))) LOOP
asier@5207
   344
        v_missingColumns := true;
asier@5167
   345
        v_Message := '@MissingCommonColumn@: '||Cur_CommonCols.columnname||'<br/>'||v_Message;
asier@5167
   346
      END LOOP;
asier@5167
   347
      
asier@5167
   348
      select count(*)
asier@5167
   349
        into v_count
asier@5167
   350
        from ad_column c, ad_table t
asier@5167
   351
       where lower(columnname) = lower(t.tablename)||'_id'
asier@5167
   352
         and t.ad_table_id = c.ad_table_id
asier@5167
   353
         and t.ad_table_id = v_Record_ID;
asier@5167
   354
         
asier@5167
   355
        IF v_Count = 0 THEN
asier@5167
   356
          select tablename
asier@5167
   357
            into v_TableName
asier@5167
   358
            from ad_table
asier@5167
   359
            where ad_table_id = v_record_ID;
asier@5167
   360
            
asier@5207
   361
          v_missingColumns := true;
asier@5167
   362
          v_Message := '@MissingPrimaryKeyColumn@: '||v_tablename||'_ID<br/>'||v_Message;
asier@5167
   363
        END IF;
asier@5207
   364
        
asier@5207
   365
        IF (v_MissingColumns) THEN
asier@5207
   366
          v_Result :=0;
asier@5207
   367
          v_Message := '@MissingRequiredColumns@<br/>'|| v_message;
asier@5207
   368
        END IF;
asier@5167
   369
    END IF;
asier@5167
   370
    
asier@5167
   371
    
gorkaion@239
   372
    --<<FINISH_PROCESS>>
carlos@0
   373
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   374
      --  Update AD_PInstance
carlos@0
   375
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
carlos@0
   376
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
carlos@0
   377
    ELSE
carlos@0
   378
      DBMS_OUTPUT.PUT_LINE('Finished ' || v_Message) ;
carlos@0
   379
    END IF;
carlos@0
   380
    -- Commented by cromero 19102006 COMMIT;
carlos@0
   381
    RETURN;
asier@5167
   382
    
asier@5167
   383
    
asier@5167
   384
    
carlos@0
   385
  END; --BODY
carlos@0
   386
EXCEPTION
carlos@0
   387
WHEN OTHERS THEN
carlos@0
   388
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   389
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   390
  ROLLBACK;
carlos@0
   391
  IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   392
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   393
  END IF;
carlos@0
   394
--  RETURN;
antonio@735
   395
END AD_TABLE_IMPORT
gorkaion@239
   396
]]></body>
adrian@94
   397
    </function>
adrian@94
   398
  </database>