head	1.2;
access;
symbols;
locks; strict;
comment	@# @;


1.2
date	2001.07.30.18.44.24;	author leonardr;	state dead;
branches;
next	1.1;

1.1
date	2001.07.05.22.21.39;	author leonardr;	state Exp;
branches;
next	;


desc
@@


1.2
log
@Moved stuff from 1.1.0 migration directory to 1.2.0 directory to reflect actual future versioning.
@
text
@#!/usr/bin/perl
use DBI;
use strict;

my $dbh = dbConnection($ENV{DATABASE_NAME}, $ENV{DATABASE_ADMIN_USER},
		       $ENV{DATABASE_ADMIN_PASSWORD});

my $path="/java/org/tigris/helm/om/Licenses.java";
my $oldPath="/java/org/tigris/helm/tool/Licenses.java";
my $instance=$ENV{'SANDBOX'} . '/' . $ENV{'INSTANCE'};
my $sandbox=$ENV{'SANDBOX'} . '/helm';

my @@paths = ("$instance$path", "$instance$oldPath", "$sandbox$path",
	      "$sandbox$oldPath");

my @@defaultLicenses = ("Apache License", "BSD License", "GNU Public License",
		       "Proprietary");
my @@licenses = ();

print "Looking for license specification.\n";
foreach my $testPath (@@paths)
  {
    print " $testPath?\n";
    if (-e $testPath)
      {
	print "$testPath found.\n";
	@@licenses = getLicenses($testPath);
	print "This site has the following licenses:\n";
	foreach (@@licenses)
	  {
	    print " $_\n";
	  }
	last;
      }
  }

unless (@@licenses)
  {
    print "Could not find Licenses.java, assuming Apache/BSD/GPL/Proprietary.\n";
    @@licenses = @@defaultLicenses;
  }

my $domainProjectID= getDomainProjectID($dbh);
foreach my $license (@@licenses)
  {
    putLicenseIntoDatabase($domainProjectID, $license);
  }

#Now, change the projects from old-style licenses to new licenses.

my $oldLicenseQuery = ("select PROJECT_ID, LICENSE from HELM_PROJECT");
my $sth = $dbh->prepare($oldLicenseQuery);
my $rv = $sth->execute();

runStatement($dbh, "alter table HELM_PROJECT change column LICENSE LICENSE_ID INTEGER DEFAULT NULL");

while (my $row=$sth->fetchrow_hashref())
  {
    my $projectID = $row->{PROJECT_ID};
    my $license = $row->{LICENSE};
    my $licenseID = "NULL";
    if ($license ne "None")
      {
	if ($license ne "" && $license ne "None")
	  {
	    $licenseID = "'" . getLicenseID($dbh, $license) . "'";
	  }
      }
    my $query = "update HELM_PROJECT SET LICENSE_ID=$licenseID where PROJECT_ID='$projectID'";
    runStatement($dbh, $query);
  }

##################################################################

sub getLicenses
  {
    my $fp = shift;
    my @@licenses = ();
    
    if(open(F, "< $fp"))
      {
	my @@lines = <F>;
	close F;
	my $contents = join('', @@lines);
	
	$contents =~ m/^\s*private\s+static\s+final\s+String\[\]\s+LICENSES\s*=\s*\{([^}]+)\}/ms;
    
        @@licenses = split(',', $1);
        foreach(@@licenses)
	  {
	    s/^\s*['"]//;
            s/['"]\s*//;
          }
      }
      else 
      {
          print "Couldn't open $fp for read: $!\n";
      }
      return @@licenses;
  }

sub putLicenseIntoDatabase
  {
    my ($domainProjectID, $license) = @@_;
    my $query = "insert ignore into HELM_LICENSE (PROJECT_ID, NAME) VALUES ($domainProjectID, '$license')";
    runStatement($dbh, $query);
  }


####################################################################

# Creates and returns a handle to the database (i.e. connection reference).
#
# ex. my $conn = dbConnection('tigris', 'user', 'passwd');
sub dbConnection($$$)
{
    my ($dbName, $dbUser, $dbPasswd) = @@_;
    return DBI->connect
        ("DBI:mysql:$dbName:$ENV{DATABASE_HOST}:$ENV{DATABASE_PORT}",
         $dbUser, $dbPasswd,
        { RaiseError => 1, AutoCommit => 1 });
}

# Returns the ID of the domain project
sub getDomainProjectID($)
{
  my ($dbh) = @@_;
  my $projectQuery = "select PROJECT_ID from HELM_PROJECT where TYPE='domain'";
  my $resultSet = runSelect($dbh, $projectQuery);
  return $resultSet->{PROJECT_ID};
}

# Returns the ID of the named license
sub getLicenseID($$)
{
  my ($dbh, $name) = @@_;
  my $query = "select LICENSE_ID from HELM_LICENSE where NAME='$name'";
  my $resultSet = runSelect($dbh, $query);
  my $licenseID = $resultSet->{LICENSE_ID};
  if ($licenseID eq "")
    {
      putLicenseIntoDatabase(getDomainProjectID($dbh), $name);
      return getLicenseID($dbh, $name);
    }
  else
    {
      return $licenseID;
    }
}

# Runs a SQL statement.
#
# ex. my $rv = runStatement($dbh, 'delete from bar where baz');
sub runStatement($$)
{
    my ($dbh, $sql) = @@_;
    #print "sql=$sql\n";  # debug
    $sql =~ s/(.*)\;\s*$/$1/s;
    my $sth = $dbh->prepare($sql);
    my $rv = $sth->execute();
    $sth->finish();
    return $rv;
}

# Runs a SQL SELECT statement.
#
# ex. my $rv = runSelect($dbh, 'select foo from bar where baz');
sub runSelect($$)
{
  my ($dbh, $sql) = @@_;
  #print "sql=$sql\n";  # debug
  $sql =~ s/(.*)\;\s*$/$1/s;
  my $sth = $dbh->prepare($sql);

  my $rv = $sth->execute();
  my $hashref=$sth->fetchrow_hashref();
  $sth->finish();
  return $hashref;
}
@


1.1
log
@Licenses are now contained in a database-managed table rather than in a custom Java class (yay!)
@
text
@@

