Showing posts with label Web. Show all posts
Showing posts with label Web. Show all posts

Tuesday, August 21, 2012

Weekend Challenge #1: CSV to a Web Page

If you know programming and want to see samples of my work, you can read this blog. If you are not a programmer and want to evaluate my skills, there is nothing out there that I can show you. I haven’t worked on a public web site for over 3 years. My current projects are internal Windows Forms applications. You could load Improv Suggester onto your Windows Phone, if you have one.

So last weekend I challenged myself to write a public sample. I wanted to challenge myself to work in technologies that I haven’t worked in for awhile if ever.

Problem

Suppose you have a system that uses Excel spreadsheets to store data. Now, suppose that you want to transition to a web based system but you want to be able to fill out an existing spreadsheet and “Import” the data into the new system.

The Tools

Web Matrix 2

Editor – Half way between Visual Studio and notepad. The editor felt bulky without the features I would expect in exchange for the bulk. There were a couple of times when the editor was a dozen or so keystrokes behind me. Publishing to the web is easy.

Link to Web Matrix 2

Razor Rendering Engine

Language & Rendering Engine – It is interesting how HTML, JavaScript and C# are mixed together. It looks cleaner than ASP.NET, PHP or T4 markup, though I wish the JavaScript looked more different than C#, but that may be an editor issue.

Link to Razor

Hanson Table

Grid Control – This was the first JQuery control that I found that behaved the way I needed it to. There were other grids that allowed you to edit the cells but forced you to click on the cell you wanted to edit.

Link to Hanson Table

Lumenworks (CsvReader)

Parse CSV – This is somewhat of a cheat. I have used this library on a couple of other projects. It has this cool feature that allows you to reference columns by name, assuming that these names are on the first line. Since I wanted to allow the user to put global data at the top of the sheet, I looked for the headers and that was the first line that I gave to CsvReader.

Link to Lumenworks on Code Project

The Code

According to the rules that I have imposed on myself, I give you the code as it existed as of the end of August 19. You can see the sample in action on my main site.

@using Microsoft.Web.Helpers;
@using System.Web;
@using System.IO;
@using LumenWorks.Framework.IO.Csv;
@using System.Text;
@{
    Layout = "~/_SiteLayout.cshtml";
Page.Title = "Upload Grid Demo";

var make = "Make";
var date = string.Empty;
var submittedData = "";
var fileName = "";
var content = "";
var dataArray = "";
if (IsPost) 
{
var _txtMake = Request.Form["_txtMake"];

if(_txtMake != null)
{
submittedData = "Make: " + Request.Form["_txtMake"] + Environment.NewLine +
"Date: " + Request.Form["_txtDate"] + Environment.NewLine +
"Grid Data: " + Environment.NewLine +
Request.Form["_hidGridJson"];
}
else
{        
var fileSavePath = "";
var uploadedFile = Request.Files[0];
fileName = Path.GetFileName(uploadedFile.FileName);

StreamReader sr1 = new StreamReader(uploadedFile.InputStream);
string st = sr1.ReadToEnd();

Dictionary<string,string> topParams;
string lineItems;

getParamsAndGridDetail(st, out topParams, out lineItems);

Stream st2 = GenerateStreamFromString(lineItems);
TextReader tr = new StreamReader(st2);

dataArray = getGridDataForTextReader(tr);

if (!topParams.TryGetValue("Make:", out make))
{
make = string.Empty;
}
if (!topParams.TryGetValue("Date:", out date))
{
date = string.Empty;
}
}
}
}
@functions{
    public void getParamsAndGridDetail(string allLines, out Dictionary<string,string>
topParams, out string lineItems)
{
topParams = new Dictionary<string,string>();
StringBuilder sb = new StringBuilder();
string[] linesArray=allLines.Replace("\r", string.Empty).Split(new char[]{'\n'});
int stepNumber = 1;
foreach(string line in linesArray)
{
if (stepNumber == 1)
{
if (isHeaderLine(line))
{
sb.AppendLine(line);
stepNumber = 2;
}
else
{
lookForKeys(line, ref topParams);
}
}
else if (stepNumber == 2)
{
if(line.Replace(",", string.Empty).Trim().Length == 0)
{
stepNumber = 3;
}
else
{
sb.AppendLine(line);
}
}
}
lineItems = sb.ToString();
}
public void lookForKeys(string line, ref Dictionary<string,string> topParams)
{
string[] sumItems = line.Split(new char[] {','});
//foreach(string cell in sumItems)
for (int i = 0; i < sumItems.Length - 1; ++i)
{
if(sumItems[i].EndsWith(":"))
{
topParams.Add(sumItems[i], sumItems[i + 1]);
}
}
}
public bool isHeaderLine(string line)
{
bool foundModel = false;
bool foundYear = false;
bool foundColor = false;
string[] sumItems = line.Split(new char[] {','});
foreach(string cell in sumItems)
{
string normalizedCell = cell.Trim().ToLower();
if(normalizedCell == "model")
{
foundModel = true;
}
if(normalizedCell == "year")
{
foundYear = true;
}
if(normalizedCell == "color")
{
foundColor = true;
}
}
return foundModel && foundYear && foundColor;
}
public Stream GenerateStreamFromString(string s)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
public string getGridDataForTextReader(TextReader tr)
{
var sb = new StringBuilder();
sb.AppendLine("[");
using (var csv = new CsvReader(tr, true))
{
bool isFirstLine = true;
int fieldCount = csv.FieldCount;
while (csv.ReadNextRecord())
{
if(!isFirstLine)
{
sb.AppendLine(",");
}
isFirstLine = false;
sb.Append("        [");
sb.AppendFormat("\"{0}\",{1},\"{2}\"", csv["Model"], csv["Year"], csv["Color"]);
sb.Append("]");
}
}
sb.AppendLine("");
sb.Append("    ];");
return sb.ToString();
}
}
@section Scripts
{
<script src="@Href("~/lib/jquery.js")"></script>
<script src="@Href("~/jquery.handsontable.js")"></script>
<script src="@Href("~/lib/bootstrap-typeahead.js")"></script>
<script src="@Href("~/lib/jquery.autoresize.js")"></script>
<script src="@Href("~/lib/jQuery-contextMenu/jquery.contextMenu.js")"></script>
<script src="@Href("~/lib/jQuery-contextMenu/jquery.ui.position.js")"></script>
<link rel="stylesheet" media="screen" 
href="/lib/jQuery-contextMenu/jquery.contextMenu.css">
<link rel="stylesheet" media="screen" href="/jquery.handsontable.css">
}
    @FileUpload.GetHtml(
initialNumberOfFiles:1,
allowMoreFilesToBeAdded:false,
includeFormTag:true,
uploadText:"Parse CSV File")
@if (IsPost) {
<br/>
<span>File uploaded!</span>
<h3>@fileName</h3>
}
@if (string.IsNullOrEmpty(submittedData)){
<form action="" method="post" name="_formWriteBack" onclick="doOnSubmit();"  >
<input type="hidden" name="_hidGridJson" id="_hidGridJson" />
Make: <input name="_txtMake" id="_txtMake" value="@make" /><br />
Date: <input name="_txtDate" id="_txtDate" value="@date" /><br />
<div id="example1grid" class="dataTable"></div>
<script>
var $container = $("#example1grid");
var $parent = $container.parent();
$container.handsontable({
rows: 2,
cols: 3,
minSpareCols: 0, //always keep at least 0 spare row at the right
minSpareRows: 1, //always keep at least 1 spare row at the bottom
contextMenu: true,
colHeaders: ["Model", "Year", "Color"],
autoComplete: [
{
match: function (row, col, data) {
if (col == 2) {
return true;
}
return false;
},
highlighter: function (item) {
var query = this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&');
var label = item.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) {
return '<strong>' + match + '</strong>';
});
return '<span style="margin-right: 10px; background-color: ' +
item + '">&nbsp;&nbsp;&nbsp;</span>' + label;
},
source: function () {
return ["Yellow", "Red", "Orange", "Green", "Blue", "Gray", "Black", "White"]
},
strict: false //allows other values that defined in array above
}
]

});

var data = @Html.Raw(dataArray)
$container.handsontable("loadData", data);

function doOnSubmit() {
var newdata;
newdata = $container.handsontable("getData", "");
_hidGridJson.value = newdata;
}
</script>
<input type="submit" value="Submit Form" />
</form>
}
@if (!string.IsNullOrEmpty(submittedData)){
<p>This is the data you just submitted to the web server. </p>
<pre>
@submittedData
</pre>
}

Thursday, May 07, 2009

Photoviewer

Today I needed to write a pop up form that would show any image without any extra IE (or Firefox, Chrome, Safari, etc.) toolbars, etc. To get a web form to display an arbitrary image, I have a simple page that takes the URL of the image in the query string. The web form takes that string and uses it to set ImageUrl of an asp.image control.

photoviewer.aspx:
<%@ Page Language="C#" EnableViewState="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">    private void Page_Load()
    {        string file = Request.QueryString["file"];
        if (file != null)
            myImage.ImageUrl = Server.UrlDecode(file);
    }
</script>
<html>
<head>
    <title>My Image Viewer</title>
</head><body>
    <form id="Form1" method="post" runat="server">
    <table style="border:0;">
        <tr>
            <td>
                <asp:Image runat="server" ID="myImage" EnableViewState="false" />
            </td>    
        </tr>
        <tr>
            <td>
                <input type="button" onclick="javascript: window.close();" 
                    value="close" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

To get rid of extra browser features and to size the popup, I call window.open() I set the height and width to values that will ensure that I have enough room for the image (I can use to get the dimensions of an image and add some buffer around the image so the popup isn’t all picture). I suppress status, toolbar, menubar. Location and scrollbars by setting them all to “0”.

feature_win.js
// declared outside of the scope of the function so it will persist on the page:
var newWindow;
function featureWin(height, width, url)
{
    var lHeight = height;
    var lWidth = width;
    
    if (newWindow && !newWindow.closed) 
    {
      newWindow.close()
    }
    newWindow = window.open(url, null, 'height=' + height +  
       ',width=' + width + ',status=0,toolbar=0,menubar=0,location=0,scrollbars=0');
}

Here we put it all together.

default.html
<html>
<head>
    <title>Test My Image Viewer</title>
    <script type="text/javascript" src="feature_win.js"></script>
</head>
<body>
<a href="javascript: featureWin(825, 1050, 'photoviewer.aspx?file=Garden.jpg');">
    Garden</a>
</body>
</html>

Sunday, April 26, 2009

URL Builder Functions

I am working on an internal web application that does some state management through the query string. That being the case, I have to add, remove and change paramters in the query string. Here are the functions that I use to edit the URL to change these varables

// Takes a URL with parameters and replaces the current value of a given parameter 
// with the new value provided
public static string replaceParameterInUrl(string theUrl, string param, string newValue)
{
    StringBuilder sb = new StringBuilder();
    string[] parts = theUrl.Split(new char[] { '?', '&' });
    sb.Append(parts[0]).Append("?");
    for (int i = 1; i < parts.Length; ++i)
    {
         string thisParam = parts[i];
         string[] paramParts = thisParam.Split(new char[] { '=' });
         if (paramParts[0] != param)
             sb.Append(thisParam).Append("&");
    }
    sb.Append(param).Append("=").Append(newValue);
    return sb.ToString();
}
/// Takes a URL with parameters and replaces the current value of a given parameter 
/// with the new value provided
public static string removeParameterFromUrl(string theUrl, string param)
{
    StringBuilder sb = new StringBuilder();
    bool isFirstItem = true;
    string[] parts = theUrl.Split(new char[] { '?', '&' });
    sb.Append(parts[0]).Append("?");
    for (int i = 1; i < parts.Length; ++i)
    {
        string thisParam = parts[i];
        string[] paramParts = thisParam.Split(new char[] { '=' });
        if (paramParts[0] != param)
        {
            if (!isFirstItem)
                sb.Append("&");
            sb.Append(thisParam);
            isFirstItem = false;
        }
   }
   return sb.ToString();
}

If I was going to use these functions in a public facing site,I'd probably sort the parameters alphabetically, so there would be less flux in the URLs that Google would see