I am developing some software that generates reports from event data collected from some devices. The data is stored in a database. I want the user to be able to template the output filename of the report generated by the software. So when the user decides to generate a report, the output filename template they have configured is applied to the data retrieved from the database.
For example, lets say that each report has the following data template elements associated with it:
<Name>
<Timestamp>
<Location>
<EventType>
So the user could select any of these template items in any order to determine the format of the output filename. If they chose something like this:
<Name> <EventType> <Location> <Timestamp>.pdf
And lets say this is the data for the current report in the database:
Name = "MyReport"
EventType = "Error"
Location = "Park Ave"
Timestamp = 1-30-2013 11.00.00 AM
The report filename would look like this when the actual report is generated:
"MyReport Error Park Ave 1-30-2013 11.00.00 AM.pdf"
I'm struggling to come up with an elegant way of providing custom format elements like <Name>, <Location> etc... and building a string from data in a database or possibly an entity object. I have looked at ICustomFormatter and IFormatProvider, but I'm not sure if these are completely appropriate for what I'm doing.
I am using WPF 4.5 and my data will be driven by the Entity Framework.
Any ideas would be appreciated!
I saw this in my questions and that no one ever answered. Just for posterity I wanted to record what I did. I ended up using Regex.Replace. I used an enumeration to list the different format fields like this:
String pattern = String.Format("<{0}>", Template.Timestamp);
String result = Regex.Replace(pattern, data.Timestamp.ToString());
Where "Template" is an enumeration and "Timestamp" is the element I wanted to replace. It seemed to work for my needs.
Related
I'm trying to read task details from a mpp file using net.sf.mpxj library. However, when trying to read custom fields, I get a byte array which I do not know what to do with! It is not the exact value of the custom field from that specific task. Can anyone tell me what to do?
ProjectReader reader = new MPPReader();
ProjectFile project = reader.read(#"C:\EPM\test2.mpp");
foreach (net.sf.mpxj.Task task in project.Tasks)
{
var Value = task.GetFieldByAlias("My Custom Field Name");
}
The "Value" will be a byte array and I do not know how to get the real value from it.
UPDATED ANSWER:
As of MPXJ 10.7.0 you can retrieve correctly typed values for enterprise custom fields. You'll also find a CustomFieldDataType attribute as part of the CustomField class which indicates what type you'll be retrieving.
(One interesting "gotcha" is that if your MPP file contains an enterprise custom field which is based on a lookup table, i.e. the user can only select from a fixed set of values, the user-visible text is NOT stored in the MPP file. You'll only get back a GUID representing the value the user has selected. Microsoft Project itself has this same issue... if you open the MPP file when you're not connected to Project Server, these values will appears as blanks...)
ORIGINAL ANSWER:
The main problem is unfortunately that MPXJ doesn't currently offer the same level of support for Enterprise Custom Fields as it does for other fields. While it is able to identify Enterprise Custom Fields and the aliases they've been given, at the moment it is only able to read the raw bytes representing the field data.
Enterprise Custom Fields are not as commonly used as other field types so there hasn't been as much time invested in locating the definitions of these fields in the MPP file. The field definition will contain the type information necessary to convert from the raw bytes to the expected data type.
Improved support for Enterprise Custom Fields is on the "to do" list for MPXJ.
In my project (I am using azure storage) I have some data that I want to translate. I have the resource system in place for translations. I have a table in cloud which has name property. I want to translate it somehow.
One option is to create all the entries in database for each language which I don't prefer as it would create a lot entries along with the name.
Is there a smart way to use the resx mechanism I have in place?
So the table has multiple properties and one is name. Name could be anything like Mud, rock etc. Now I want to translate Mud into different language. Something like Texts.Mud would return me the correct value.
But lets say I get data like this
var data = some query;
string translatedName = Texts.data[0].name; // this won't work
You should instead add more columns in the database, each for a different language and select the column based on the user language.
Other solution is to have a transaltion mechanism (a custom class for example), where you pass the original database result (say data[0].name) to a query and it returns the translated value for you.
I have a string that is written out like an XML file. An example would look like this:
string = <Employees><EmployeeId>1</EmployeeId>< ... ></Employees>
I am saving this in a table because I wanted to audit changes, but I didn't want to have multiple tables for different audits. This is because it would record changes to things other than employees. So using an XML style string in the database seemed like a good suggestion.
Now to the real business. I want to check to make sure that there were actually changes to the employee because one could go into the edit page, change nothing, and click save. As of right now, the data would write to the DB and just clutter it up with non-changed data.
I'd like to be able to check if the XML styled string that is going to be saved is on the database, so if <employees><employeeid>###</employeeid> == "changes" and then see if the whole string equals the other. Basically, check the employeeId first because that won't change, and then check the string as a whole to see if there is any difference. I would have just checked the first n numbers, but the id number could have a length of 1 to 3.
Also, because it is styled as XML, is there an easy way to convert it to read it like an XML file and check that way?
Storing arbitrary data in a column is a form of denormalization. You can't really do much with it at a database level. However, SQL Server does have an XML column type. Entity Framework doesn't support mapping to/from an XML column, so it will simply treat your XML as a standard string. With this column type, though, you can write actual SQL queries against your XML using XPath expressions.
Your best bet, then, is to type your column as XML, and then write a stored procedure that performs the query you need. You can then utilize this stored procedure with Entity Framework.
For more information on the XML column type see: https://msdn.microsoft.com/en-us/library/ms190798(SQL.90).aspx
I have metadata in a DB table that i want to use in code.
The metadata is different sorts of Time types for reporting spent time.
The data can be:
NormalTime
OverTime
Vacation
Illness
etc
The data have a ID and a description and some other stuff.
ID = 1
Name = "Regular time"
Description = "Normal work time"
What is a good way to relate to this data in my code?
If for example i want create a method that sums all the NormalTime reported (i have another table that stores used time where the NormalTime ID and amount and some other stuff) how do i do that?
I dont want to hardcode the ID:
Select * from xyz where TimeType = 1
What i wanna do is:
Select * from xyz where TimeType = NormalTime.
Otherwise the code becomes very hard to read.
In my current solution i have hardcoded string consts that correlates to the ID.
The problem with this is if someone changes the description of the TimeType from NormalTime to something eles the hardcoded string const sais one thing and the db data sais something else.
And yes, this has happend as i dont have control over the DB content :(
So, how do I solve this in the best maintainable and readable way where changes can occur in the DB table and the code dont get very hard to read.
Where someone can add TimeTypes to the DB and later I can add methods that uses them in code.
One way to do this would be to use Visual Studio's T4 text generation templates.
(Entity Framework uses these for its code generation)
You can create a template file which contains code to pull the tables with metadata
from the database, and generates classes with static constants in.
They do need to be run whenever the database changes, though. But I think you might be able
to set them up so they do re-generate every time your code is built.
A question about T4 templates
You could have an enum type on the C# side that maps to a table in the database.
http://www.codeproject.com/Articles/41746/Mapping-NET-Enumerations-to-the-Database
I have a Task where I have to read an csv file and write the content into a c# List.
Since the csv file can change its layout (caption/order) in the future I want to use a configuration file for mapping the attributes.
Now I am wondering if there is an example out there so I don't have to reinvent the weel :D
My datasource looks like this (tab stop seperated)
Customer No. Customer Name Created Discount
10215 John Doe 2010-08-25 5050.23
And my class like this:
Class Customer
{
string CustomerNo {get;set;}
string CustomerName {get;set;}
DateTime CreatedOn {get;set;}
decimal Discount {get;set;}
}
Now I want to have an external xml file with the definition so I can modify it at runtime without recompiling the code.
<customermapping mapstoclass="my.namespace.Customer">
<attribute csvcaption="Customer No." mapstoproperty="CustomerNo"
typeof="System.String" required="true">
<attribute csvcaption="Customer Name" mapstoproperty="CustomerName"
typeof="System.String" required="true">
<attribute csvcaption="Created" mapstoproperty="CreatedOn"
typeof="System.DateTime" required="false">
<attribute csvcaption="Discount" mapstoproperty="Discount"
typeof="System.Decimal" required="false">
</customermapping>
At the end of the Day I want to do the following:
(I already can read all the values from the csv file (the first line is the caption and is in a seperate array)
List<Customer> customers =
CreateCustomerList(string[] csvCaptions, string[] csvLines,
"c:\customermapping.xml");
Shouldn't be to complicated but as I said, if someone already did something similar, any examples are welcome.
It looks like you want to reinvent the xsd schema. If you can change the format use the xml and define that rules in the xml.
If you can't change the format or your data is too large to fit nicely in to xml I guess you are on your own. CSV isn't rather an adhoc format I don't think any body cared enough to create a schema validation for it.
You might want to look into using LINQ for this. There is an articles on LINQ to TEXT here:
Tutorial Reading A Text File Using LINQ
LINQ to TEXT and LINQ to CSV
Update: I've re-read your question and I'm no longer sure that using LINQ will really help solve your problem, however I'm leaving the answer here just in case it helps.
If you can at all help it I would put the logic about what columns map onto what properties in code rather than in xml files.