// @(#)root/net:$Id$
// Author: Jan Fiete Grosse-Oetringhaus   28/9/2004
//         Jancurova.lucia@cern.ch Slovakia  29/9/2008

/*************************************************************************
 * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TGridJDL                                                             //
//                                                                      //
// Abstract base class to generate JDL files for job submission to the  //
// Grid.                                                                //
//                                                                      //
// Related classes are TGLiteJDL.                                       //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "TGridJDL.h"
#include "TObjString.h"
#include "Riostream.h"


ClassImp(TGridJDL)

//______________________________________________________________________________
TGridJDL::~TGridJDL()
{
   // Cleanup.

   Clear();
}

//______________________________________________________________________________
void TGridJDL::Clear(const Option_t*)
{
   // Clears the JDL information.

   fMap.DeleteAll();
}

//______________________________________________________________________________
void TGridJDL::SetValue(const char *key, const char *value)
{
   // Sets a value. If the entry already exists the old one is replaced.

   TObject *object = fMap.FindObject(key);
   TPair *pair = dynamic_cast<TPair*>(object);
   if (pair) {
      TObject *oldObject = pair->Key();
      if (oldObject) {
         TObject *oldValue = pair->Value();

         fMap.Remove(oldObject);
         delete oldObject;
         oldObject = 0;

         if (oldValue) {
            delete oldValue;
            oldValue = 0;
         }
      }
   }

   fMap.Add(new TObjString(key), new TObjString(value));
}

//______________________________________________________________________________
const char *TGridJDL::GetValue(const char *key)
{
   // Returns the value corresponding to the provided key. Return 0 in case
   // key is not found.

   if (!key)
      return 0;

   TObject *object = fMap.FindObject(key);
   if (!object)
      return 0;

   TPair *pair = dynamic_cast<TPair*>(object);
   if (!pair)
      return 0;

   TObject *value = pair->Value();
   if (!value)
      return 0;

   TObjString *string = dynamic_cast<TObjString*>(value);
   if (!string)
      return 0;

   return string->GetName();
}

//______________________________________________________________________________
void TGridJDL::SetDescription(const char *key, const char* description)
{
   // Sets a value. If the entry already exists the old one is replaced.

   TObject *object = fDescriptionMap.FindObject(key);
   TPair *pair = dynamic_cast<TPair*>(object);
   if (pair) {
      TObject *oldObject = pair->Key();
      if (oldObject) {
         TObject *oldValue = pair->Value();

         fDescriptionMap.Remove(oldObject);
         delete oldObject;
         oldObject = 0;

         if (oldValue) {
            delete oldValue;
            oldValue = 0;
         }
      }
   }

   fDescriptionMap.Add(new TObjString(key), new TObjString(description));
}

//______________________________________________________________________________
const char *TGridJDL::GetDescription(const char *key)
{
   // Returns the value corresponding to the provided key. Return 0 in case
   // key is not found.

   if (!key)
      return 0;

   TObject *object = fDescriptionMap.FindObject(key);
   if (!object)
      return 0;

   TPair *pair = dynamic_cast<TPair*>(object);
   if (!pair)
      return 0;

   TObject *value = pair->Value();
   if (!value)
      return 0;

   TObjString *string = dynamic_cast<TObjString*>(value);
   if (!string)
      return 0;

   return string->GetName();
}

//______________________________________________________________________________
TString TGridJDL::AddQuotes(const char *value)
{
   // Adds quotes to the provided string.
   //  E.g. Value --> "Value"

   TString temp = TString("\"");
   temp += value;
   temp += "\"";

   return temp;
}

//______________________________________________________________________________
void TGridJDL::AddToSet(const char *key, const char *value)
{
   // Adds a value to a key value which hosts a set of values.
   // E.g. InputSandbox: {"file1","file2"}

   const char *oldValue = GetValue(key);
   TString newString;
   if (oldValue)
      newString = oldValue;
   if (newString.IsNull()) {
      newString = "{";
   } else {
      newString.Remove(newString.Length()-1);
      newString += ",";
   }

   newString += AddQuotes(value);
   newString += "}";

   SetValue(key, newString);
}

//______________________________________________________________________________
void TGridJDL::AddToSetDescription(const char *key, const char *description)
{
   // Adds a value to a key value which hosts a set of values.
   // E.g. InputSandbox: {"file1","file2"}

   const char *oldValue = GetDescription(key);
   TString newString;
   if (oldValue)
      newString = oldValue;
   newString += description;

   SetDescription(key, newString);
}
//______________________________________________________________________________
TString TGridJDL::Generate()
{
   // Generates the JDL snippet.

   TString output("");

   TIter next(&fMap);
   TIter nextDescription(&fDescriptionMap);
   TObject *object = 0;
   TObject *objectD = 0;
   while ((object = next())) {
      TObjString *key = dynamic_cast<TObjString*>(object);
      if (key) {
         TObject *value = fMap.GetValue(object);
         TObjString *valueobj = dynamic_cast<TObjString*>(value);

         if (valueobj) {
            nextDescription.Reset();
            while ((objectD = nextDescription())) {
               TObjString *keyD = dynamic_cast<TObjString*>(objectD);
               if (keyD) {
                  TObject *valueD = fDescriptionMap.GetValue(objectD);
                  TObjString *valueobjD = dynamic_cast<TObjString*>(valueD);
                  if (valueobjD && !strcmp(key->GetName(), keyD->GetName())){
                     //Info("",Form("%s %s",key->GetString().Data(),keyD->GetString().Data()));
                     output += "# ";
                     output += valueobjD->GetName();
                     output += "\n";
                  }
               }
            }
            output += key->GetName();
            output += " = ";
            output += valueobj->GetName();
            output += ";\n\n";
         }
      }
   }

   return output;
}
 TGridJDL.cxx:1
 TGridJDL.cxx:2
 TGridJDL.cxx:3
 TGridJDL.cxx:4
 TGridJDL.cxx:5
 TGridJDL.cxx:6
 TGridJDL.cxx:7
 TGridJDL.cxx:8
 TGridJDL.cxx:9
 TGridJDL.cxx:10
 TGridJDL.cxx:11
 TGridJDL.cxx:12
 TGridJDL.cxx:13
 TGridJDL.cxx:14
 TGridJDL.cxx:15
 TGridJDL.cxx:16
 TGridJDL.cxx:17
 TGridJDL.cxx:18
 TGridJDL.cxx:19
 TGridJDL.cxx:20
 TGridJDL.cxx:21
 TGridJDL.cxx:22
 TGridJDL.cxx:23
 TGridJDL.cxx:24
 TGridJDL.cxx:25
 TGridJDL.cxx:26
 TGridJDL.cxx:27
 TGridJDL.cxx:28
 TGridJDL.cxx:29
 TGridJDL.cxx:30
 TGridJDL.cxx:31
 TGridJDL.cxx:32
 TGridJDL.cxx:33
 TGridJDL.cxx:34
 TGridJDL.cxx:35
 TGridJDL.cxx:36
 TGridJDL.cxx:37
 TGridJDL.cxx:38
 TGridJDL.cxx:39
 TGridJDL.cxx:40
 TGridJDL.cxx:41
 TGridJDL.cxx:42
 TGridJDL.cxx:43
 TGridJDL.cxx:44
 TGridJDL.cxx:45
 TGridJDL.cxx:46
 TGridJDL.cxx:47
 TGridJDL.cxx:48
 TGridJDL.cxx:49
 TGridJDL.cxx:50
 TGridJDL.cxx:51
 TGridJDL.cxx:52
 TGridJDL.cxx:53
 TGridJDL.cxx:54
 TGridJDL.cxx:55
 TGridJDL.cxx:56
 TGridJDL.cxx:57
 TGridJDL.cxx:58
 TGridJDL.cxx:59
 TGridJDL.cxx:60
 TGridJDL.cxx:61
 TGridJDL.cxx:62
 TGridJDL.cxx:63
 TGridJDL.cxx:64
 TGridJDL.cxx:65
 TGridJDL.cxx:66
 TGridJDL.cxx:67
 TGridJDL.cxx:68
 TGridJDL.cxx:69
 TGridJDL.cxx:70
 TGridJDL.cxx:71
 TGridJDL.cxx:72
 TGridJDL.cxx:73
 TGridJDL.cxx:74
 TGridJDL.cxx:75
 TGridJDL.cxx:76
 TGridJDL.cxx:77
 TGridJDL.cxx:78
 TGridJDL.cxx:79
 TGridJDL.cxx:80
 TGridJDL.cxx:81
 TGridJDL.cxx:82
 TGridJDL.cxx:83
 TGridJDL.cxx:84
 TGridJDL.cxx:85
 TGridJDL.cxx:86
 TGridJDL.cxx:87
 TGridJDL.cxx:88
 TGridJDL.cxx:89
 TGridJDL.cxx:90
 TGridJDL.cxx:91
 TGridJDL.cxx:92
 TGridJDL.cxx:93
 TGridJDL.cxx:94
 TGridJDL.cxx:95
 TGridJDL.cxx:96
 TGridJDL.cxx:97
 TGridJDL.cxx:98
 TGridJDL.cxx:99
 TGridJDL.cxx:100
 TGridJDL.cxx:101
 TGridJDL.cxx:102
 TGridJDL.cxx:103
 TGridJDL.cxx:104
 TGridJDL.cxx:105
 TGridJDL.cxx:106
 TGridJDL.cxx:107
 TGridJDL.cxx:108
 TGridJDL.cxx:109
 TGridJDL.cxx:110
 TGridJDL.cxx:111
 TGridJDL.cxx:112
 TGridJDL.cxx:113
 TGridJDL.cxx:114
 TGridJDL.cxx:115
 TGridJDL.cxx:116
 TGridJDL.cxx:117
 TGridJDL.cxx:118
 TGridJDL.cxx:119
 TGridJDL.cxx:120
 TGridJDL.cxx:121
 TGridJDL.cxx:122
 TGridJDL.cxx:123
 TGridJDL.cxx:124
 TGridJDL.cxx:125
 TGridJDL.cxx:126
 TGridJDL.cxx:127
 TGridJDL.cxx:128
 TGridJDL.cxx:129
 TGridJDL.cxx:130
 TGridJDL.cxx:131
 TGridJDL.cxx:132
 TGridJDL.cxx:133
 TGridJDL.cxx:134
 TGridJDL.cxx:135
 TGridJDL.cxx:136
 TGridJDL.cxx:137
 TGridJDL.cxx:138
 TGridJDL.cxx:139
 TGridJDL.cxx:140
 TGridJDL.cxx:141
 TGridJDL.cxx:142
 TGridJDL.cxx:143
 TGridJDL.cxx:144
 TGridJDL.cxx:145
 TGridJDL.cxx:146
 TGridJDL.cxx:147
 TGridJDL.cxx:148
 TGridJDL.cxx:149
 TGridJDL.cxx:150
 TGridJDL.cxx:151
 TGridJDL.cxx:152
 TGridJDL.cxx:153
 TGridJDL.cxx:154
 TGridJDL.cxx:155
 TGridJDL.cxx:156
 TGridJDL.cxx:157
 TGridJDL.cxx:158
 TGridJDL.cxx:159
 TGridJDL.cxx:160
 TGridJDL.cxx:161
 TGridJDL.cxx:162
 TGridJDL.cxx:163
 TGridJDL.cxx:164
 TGridJDL.cxx:165
 TGridJDL.cxx:166
 TGridJDL.cxx:167
 TGridJDL.cxx:168
 TGridJDL.cxx:169
 TGridJDL.cxx:170
 TGridJDL.cxx:171
 TGridJDL.cxx:172
 TGridJDL.cxx:173
 TGridJDL.cxx:174
 TGridJDL.cxx:175
 TGridJDL.cxx:176
 TGridJDL.cxx:177
 TGridJDL.cxx:178
 TGridJDL.cxx:179
 TGridJDL.cxx:180
 TGridJDL.cxx:181
 TGridJDL.cxx:182
 TGridJDL.cxx:183
 TGridJDL.cxx:184
 TGridJDL.cxx:185
 TGridJDL.cxx:186
 TGridJDL.cxx:187
 TGridJDL.cxx:188
 TGridJDL.cxx:189
 TGridJDL.cxx:190
 TGridJDL.cxx:191
 TGridJDL.cxx:192
 TGridJDL.cxx:193
 TGridJDL.cxx:194
 TGridJDL.cxx:195
 TGridJDL.cxx:196
 TGridJDL.cxx:197
 TGridJDL.cxx:198
 TGridJDL.cxx:199
 TGridJDL.cxx:200
 TGridJDL.cxx:201
 TGridJDL.cxx:202
 TGridJDL.cxx:203
 TGridJDL.cxx:204
 TGridJDL.cxx:205
 TGridJDL.cxx:206
 TGridJDL.cxx:207
 TGridJDL.cxx:208
 TGridJDL.cxx:209
 TGridJDL.cxx:210
 TGridJDL.cxx:211
 TGridJDL.cxx:212
 TGridJDL.cxx:213
 TGridJDL.cxx:214
 TGridJDL.cxx:215
 TGridJDL.cxx:216
 TGridJDL.cxx:217
 TGridJDL.cxx:218
 TGridJDL.cxx:219
 TGridJDL.cxx:220
 TGridJDL.cxx:221
 TGridJDL.cxx:222
 TGridJDL.cxx:223
 TGridJDL.cxx:224
 TGridJDL.cxx:225
 TGridJDL.cxx:226
 TGridJDL.cxx:227
 TGridJDL.cxx:228
 TGridJDL.cxx:229
 TGridJDL.cxx:230
 TGridJDL.cxx:231
 TGridJDL.cxx:232
 TGridJDL.cxx:233
 TGridJDL.cxx:234
 TGridJDL.cxx:235
 TGridJDL.cxx:236
 TGridJDL.cxx:237
 TGridJDL.cxx:238
 TGridJDL.cxx:239
 TGridJDL.cxx:240
 TGridJDL.cxx:241
 TGridJDL.cxx:242
 TGridJDL.cxx:243
 TGridJDL.cxx:244
 TGridJDL.cxx:245
 TGridJDL.cxx:246