Convert POJO Java Classes To POCO C# Classes


public class Converter
 {
 public static void BatchConvert(string sourcePath, string destinationPath)
 {
 var files = Directory.GetFiles(sourcePath);
 var classNames = new Dictionary<string, List<string>>();

foreach (var file in files)
 {
 var className = file.Replace(sourcePath, string.Empty).Replace(".java", string.Empty);
 var types = Convert(sourcePath, destinationPath, className);
 classNames.Add(className, types);
 }

var ok = true;

while (ok)
 {
 ok = false;

foreach (var key in classNames.Keys)
 {
 string type = null;

foreach (var currentType in classNames[key])
 {
 if (classNames.ContainsKey(currentType))
 {
 type = currentType;
 break;
 }
 }

if (type != null)
 {
 classNames[key].Remove(type);
 ok = true;
 }
 }
 }

foreach (var key in classNames.Keys)
 {
 if (classNames[key].Count > 0)
 {
 Convert(sourcePath, destinationPath, key, classNames[key]);
 }
 }
 }

public static List<string> Convert(string sourcePath, string destinationPath, string className, List<string> excludedTypes = null)
 {
 var types = new List<string>();

#region Initializing the Java/C# types counterparts

var map = new Dictionary<string, string>();
 map.Add("boolean", "bool");
 map.Add("Boolean", "bool?");
 map.Add("byte", "byte");
 map.Add("Byte", "sbyte?");
 map.Add("short", "short?");
 map.Add("Short", "short?");
 map.Add("int", "int");
 map.Add("Integer", "int?");
 map.Add("long", "long");
 map.Add("Long", "long?");
 map.Add("char", "char");
 map.Add("Character", "char?");
 map.Add("float", "float");
 map.Add("Foat", "float?");
 map.Add("double", "double");
 map.Add("Double", "double?");
 map.Add("String", "string");
 map.Add("Date", "DateTime");
 map.Add("HashMap", "Hashtable");
 //map.Add("ArrayList", "ArrayList");

#endregion

using (var reader = new StreamReader(string.Concat(sourcePath, className, ".java")))
 {
 using (var writer = new StreamWriter(string.Concat(destinationPath, className, ".cs")))
 {
 writer.WriteLine("using System;");
 writer.WriteLine("using System.Collections;");
 writer.WriteLine("using System.Collections.Generic;");
 writer.WriteLine();
 writer.WriteLine("namespace Models");
 writer.WriteLine("{");
 writer.WriteLine(string.Concat("\tpublic class ", className));
 writer.WriteLine("\t{");
 string line;

while ((line = reader.ReadLine()) != null)
 {
 line = line.Trim();

if (line.StartsWith("private") &&
 !line.ToLower().Contains(" static ") &&
 !line.Contains("("))
 {
 var sb = new StringBuilder("\t\tpublic ");
 var tokens = line.Split(new char[] { ' ' });
 var propertyType = tokens[1].Trim().Replace("java.lang.", string.Empty).Replace("java.util.", string.Empty);
 var property = tokens[2].Trim().Replace(";", string.Empty);

if (property.ToLower() == className.ToLower())
 {
 property = string.Concat(property, " { get; set; }");
 }
 else
 {
 property = string.Concat(property, " { get; set; }");
 //property = string.Concat(CapitalizeFirstLetter(property), " { get; set; }"); We're not doing this to accomodate Java
 }

var found = false;

foreach (string javaType in map.Keys)
 {
 if (javaType == propertyType)
 {
 found = true;
 sb.Append(string.Format("{0} {1}", map[javaType], property));
 break;
 }
 }

if (!found)
 {
 if (propertyType.StartsWith("List<"))
 {
 propertyType = propertyType.Substring(5, propertyType.Length - 6);

if (map.ContainsKey(propertyType))
 {
 sb.AppendFormat("List<{0}> {1}", map[propertyType], property);
 }
 else
 {
 types.Add(propertyType);
 sb.AppendFormat("List<{0}> {1}", propertyType, property);
 }
 }
 else
 {
 types.Add(propertyType);
 sb.AppendFormat("{0} {1}", propertyType, property);
 }
 }

if ((sb.Length > 0) && (sb.ToString() != "\t\tpublic "))
 {
 var newLine = sb.ToString();

if (excludedTypes != null)
 {
 foreach (var excludedType in excludedTypes)
 {
 if (newLine.Contains(string.Format(" {0} ", excludedType)))
 {
 newLine = newLine.Replace("\t\t", "\t\t//");
 }
 }
 }

writer.WriteLine(newLine);
 }
 }
 }

writer.WriteLine("\t}");
 writer.WriteLine("}");
 }
 }

return types;
 }

public static string CapitalizeFirstLetter(string text)
 {
 return string.Concat(text.Substring(0, 1).ToUpper(), text.Substring(1));
 }
 }

Script To Search Text In Database


DECLARE @SearchStr varchar(50)
SET @SearchStr = 'Whatever'

SET NOCOUNT ON

DECLARE @Results TABLE ([Table] nvarchar(370), [Column] nvarchar(370), [Value] nvarchar(3630))
DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
SET @TableName = ''
SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')

WHILE @TableName IS NOT NULL
BEGIN
SET @ColumnName = ''
SET @TableName =
(
SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
AND OBJECTPROPERTY(
OBJECT_ID(
QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
), 'IsMSShipped'
) = 0
)

WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
BEGIN
SET @ColumnName =
(
SELECT MIN(QUOTENAME(COLUMN_NAME))
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = PARSENAME(@TableName, 2)
AND TABLE_NAME = PARSENAME(@TableName, 1)
AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')
AND QUOTENAME(COLUMN_NAME) > @ColumnName
)

IF @ColumnName IS NOT NULL
BEGIN
DECLARE @TableNameShort VARCHAR(100)
SET @TableNameShort = SUBSTRING(SUBSTRING(@TableName, 8, 100), 0, LEN(@TableName) - 7)
DECLARE @ColumnNameShort VARCHAR(100)
SET @ColumnNameShort = SUBSTRING(@ColumnName, 2, LEN(@ColumnName) - 2)
INSERT INTO @Results
EXEC
(
'SELECT ''' + @TableNameShort + ''', ''' + @ColumnNameShort + ''', LEFT(' + @ColumnName + ', 3630)
FROM ' + @TableName + ' (NOLOCK) ' +
' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
)
END
END
END

SELECT [Table], [Column], [Value] FROM @Results

Launch Cassini Using Command Line

taskkill /F /IM WebDev.WebServer.exe

START /D “C:\Program Files\Common Files\microsoft shared\DevServer\9.0\” /B WebDev.WebServer.EXE /port:5002 /path:”$PROJECT PATH” /vpath:”/PROJECT”

Example:
taskkill /F /IM WebDev.WebServer.exe

START /D “C:\Program Files\Common Files\microsoft shared\DevServer\9.0\” /B WebDev.WebServer.EXE /port:5002 /path:”d:\Projects\myproject\project.service” /vpath:”/Project.Service”

File MIME Type Using C#

string fileName = “C:\\somefile.vsd”;
string mimeType = “application/unknown”;
string ext = System.IO.Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue(“Content Type”) != null)
{
mimeType = regKey.GetValue(“Content Type”).ToString();
}