Archive for the 'Custom Methods' Category

30
May
08

#08 Folder Creation

The following code creates folders off of a Work Flow call. I have removed some of the unusual code we use to check the current folders and align our names. There are a number of unimplemented but known improvements needed (better error processing – I drop some exceptions And I should send back the message properly to the user via WF - see the development kit)  That being noted this code will need an Active Directory ID with write access to your file stores.  Please contact me if you have any questions.  This is using the 1.1 asp.net framework.

Credit Due: http://thedotnet.com/blogs/rick_strahl/archive/2005/02/24/125731.aspx (for AD Impersonation)

C# Class & Method

using System;
using System.DirectoryServices;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Data.SqlClient;

namespace deltekCustom
{
public class CreateFolders
{

protected static string sServer01 = @”\server1dfsrootPRJ”;
protected static string sServer02 = @”\server2PRJ”;

const int LOGON32_PROVIDER_DEFAULT        = 0;
const int LOGON32_LOGON_INTERACTIVE       = 2;
const int LOGON32_LOGON_NETWORK           = 3;
const int LOGON32_LOGON_BATCH             = 4;
const int LOGON32_LOGON_SERVICE           = 5;
const int LOGON32_LOGON_UNLOCK            = 7;
const int LOGON32_LOGON_NETWORK_CLEARTEXT = 8;
const int LOGON32_LOGON_NEW_CREDENTIALS   = 9;
protected static string connDeltek = “***”;

[DllImport("advapi32.dll", SetLastError=true)]
public static extern int LogonUser(
string lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
out IntPtr phToken
);
[DllImport("advapi32.dll", SetLastError=true)]
public static extern int ImpersonateLoggedOnUser(
IntPtr hToken
);

[DllImport("advapi32.dll", SetLastError=true)]
static extern int RevertToSelf();

public CreateFolders()   {      }

public string SetupClientFolders(string ClientID, string WBS1, string Server1, string Server2, string AUX )
{
IntPtr lnToken;

int TResult = LogonUser(“***”,”***”, @”***”,LOGON32_LOGON_INTERACTIVE,LOGON32_PROVIDER_DEFAULT,out lnToken);

ImpersonateLoggedOnUser(lnToken);

if ( TResult > 0 )
{
bool bOK = true;
// VALIDATE KEY PARMS
if (AUX.Trim().Length > 0)
bOK = validAdditionalFolderText(AUX);

if (WBS1.Trim().Length == 0)
{
sReturn  = “Missing Job Number – Error on Folder Creation”;
bOK = false;
}

string Client = getClient(ClientID);
if (Client.Trim().Length == 0)
{
sReturn  = “Missing Client ID – Error on Folder Creation”;
bOK = false;
}

// PROCESS EACH SERVER SELECTED if VALID
if (bOK)
{
string sAUX = “”;
if (Server1 == “Y”)
CreeateJobFolders(sServer01 , Client, WBS1, AUX);
if (Server2 == “Y”)
CreeateJobFolders(sServer02 , Client, WBS1, AUX);
}

RevertToSelf();

}
else
sReturn  = “Security Error on Folder Creation”;

return sReturn;
}

/// <summary>
///  Get Client Code from CL table
/// </summary>
/// <param name=”WBS1?></param>
/// <returns>Client Code </returns>
private string getClient(string ClientID)
{
SqlDataReader dr;
string sClientCode = “”;

string strSQL = “Select Distinct CL.CLient as ClientCode FROM CL WHERE CL.ClientID =  ‘” + ClientID + “‘”;

SqlConnection conn = new SqlConnection(connDeltek);

SqlCommand cmdSelect  = new SqlCommand(strSQL, conn);

try
{
conn.Open();
dr = cmdSelect.ExecuteReader();

if (dr.Read())
{
sClientCode = dr["ClientCode"].ToString();
}
}

catch (Exception)
{
}

finally
{
if (conn != null) { conn.Close(); conn.Dispose(); }
if (cmdSelect != null) { cmdSelect.Dispose(); }
}

return sClientCode;

}

/// <summary>
/// Validate the Auxiliary text added to folder name
/// </summary>
/// <returns></returns>
private bool validAdditionalFolderText( string AuxText)
{
Regex objRegex;

objRegex = new Regex(“^[ a-zA-Z0-9._#s-]+$”);

return objRegex.IsMatch(AuxText);
}

private string getJobFolderEndingName( string sJob )
{
string sJobEnding = “.00″;
if (sJob.Length == 9 )
sJobEnding = sJob.Substring(7,2);

switch (sJobEnding)
{
case “00″ : return( “00-Overhead”);
case “10″ : return( “10-Proposal” );
case “20″ : return( “20-GISMapping” );
case “30″ : return( “30-ReportStudy” );
case “40″ : return( “40-Design” );
case “50″ : return( “50-DesignBuild” );
case “60″ : return( “60-Construction” );
case “70″ : return( “70-Operations” );
case “80″ : return( “80-PlanReview” );
case “90″ : return( “90-GeneralMunicipalServices” );
default: return( sJob );

}

}

/// <summary>
/// Create sub-folders on particular server for client/job
/// </summary>
/// <param name=”sServerPath”></param>
private void createJobFolders( string sServerPath , string Client, string WBS1 , string Aux )
{
string sWBS1Root =  “”;

if (WBS1.Length > 6)
sWBS1Root = WBS1.Substring(0,6);
else
sWBS1Root = WBS1;

// BASE CLIENT DIRECTORY
createDirectory(sServerPath +  @”" + Client );

// BASE JOB ROOT DIRECTORY
string sBaseJobPath = sServerPath +  @”" + Client +  @”" + sWBS1Root + Aux;
createDirectory(sBaseJobPath);

// DRAWING DIRECTORY UNDER JOB ROOT
createDirectory(sBaseJobPath + @”CADD-SURVEY”);
createDirectory(sBaseJobPath + @”CADD-SURVEYSURVEY”);
createDirectory(sBaseJobPath + @”CADD-SURVEYDRAWINGS”);
createDirectory(sBaseJobPath + @”CADD-SURVEYPLOTS”);
createDirectory(sBaseJobPath + @”CADD-SURVEYSCANS”);
// BASE JOB SUBNUMBER DIRECTORY
string JobSubnumberPath = sBaseJobPath +  @”" + getJobFolderEndingName(WBS1);
createDirectory(JobSubnumberPath);

// DEFAULT DIRECTORIES
createDirectory(JobSubnumberPath + @”AS SENT”);

createDirectory(sBaseJobPath  +    @”CADD-SURVEYDRAWINGSDGNS”);
createDirectory(sBaseJobPath  +    @”CADD-SURVEYDRAWINGSDWGS”);

}

private bool createDirectory ( string sDirName )
{
// Determine whether the directory exists.
if (Directory.Exists(sDirName))
{
return(false);
}

DirectoryInfo di = Directory.CreateDirectory( sDirName );

return(true);

}
}
}