Rhino-ETL Conditional Operation - rhino-etl

I want to check the value of a row field value and depending on the value run a specific operation. Can this be done. Part of the code is below
public override IEnumerable<Row> Execute(IEnumerable<Row> rows)
{
foreach (Row row in rows)
{
if (row["ProductAction"] == "Update")
{
_updateProductOperation.Execute(rows);
}
else
{
_addProductOperation.Execute(rows);
}
yield return row;
}
}

Someone answered me on another forum in case any one is interested. They said you'd do this with a branching operation were the one branch would check for "Update" and the other would check for != "Update".

Related

Filter courses according to there category using angular material checkbox

I have two array called 1.Courses and 2.Categories each courses have different category i want to filter the courses by category using mat-checkbox.
example: javascript is a course name and scripting is category .
Here is the stackblitz link
and below is the screen shot of the approach:
It should work on multiple checkbox filtering Thank you in advance.
So, the simplest way to do this with your current approach, IMO, would be to create a new course array filteredCourses and iterate that in your template.
OnInit, set filteredCourses to courses so it renders them all on init.
ngOnInit() {
this.filteredCourses = this.courses;
}
Next, you need some way of maintaining a list of the selected categories. This would be much easier if you used Angulars built in forms, but, in the absence of that, may I suggest the following:
onSelect (click), add the clicked category to a list of selected categories (on click, if it's not there, add it, else, remove it)
onSelect(selectedCategory: any) {
this.selectCategory(selectedCategory);
}
selectCategory(selectedCategory: any) {
const index: number = this.selectedCategories.findIndex((cat: any) => {
return cat.id === selectedCategory.id
});
if ( index === -1) {
this.selectedCategories.push(selectedCategory);
} else {
this.selectedCategories.splice(index, 1);
}
}
The next step would then to be to filter your courses array to only those where the the categoryId is included in the list of selectedCategories and set your filteredCourses array with the result, allowing the template to update. So, your onSelect function becomes:
onSelect(selectedCategory: any) {
this.selectCategory(selectedCategory);
this.filteredCourses = this.courses.filter((course: any) => {
return this.selectedCategories.findIndex((cat: any) => {
return course.categoryId === cat.id;
}) !== -1;
});
}
Updated blitz with suggestion: https://stackblitz.com/edit/mat-checkbox-kq6xgd

SaveChanges is not working on modify the data

when i try to save the the new Student object. it works fine but when i am trying to modify the data, it doesn't update the data. Instead none error is thrown on SaveChanges.
i am using code first approach (using mysql provider) and here it is the complete source.
https://www.dropbox.com/s/e34frntq8u5gsmh/SchoolManagementSystem.rar?dl=0
my tired code is this :
public ActionResult Create(Student student, HttpPostedFileBase Image)
{
try
{
if (ModelState.IsValid)
{
student = db.Students.Find(student.ID);
if (student.ID > 0)
{
db.Entry(student).State = EntityState.Modified;
db.SaveChanges();
}
else
{
if (student.Basic == null) student.Basic = new BasicInformation();
if (Image != null && Image.ContentLength > 0)
{
student.Basic.PictureUrl = Image.FileName;
string path = Server.MapPath(("~/Images/"));
Image.SaveAs(path + Image.FileName);
}
db.Students.Add(student);
db.SaveChanges();
}
return RedirectToAction("StudentList");
}
}
catch (RetryLimitExceededException /* dex */)
{
//Log the error (uncomment dex variable name and add a line here to write a log.
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
}
return View(student);
}
This seems to be the modify scenario :
if (student.ID > 0)
{
db.Entry(student).State = EntityState.Modified;
db.SaveChanges();
}
Eliminate this line of code :
db.Entry(student).State = EntityState.Modified;
and instead add code that updates the object with the new modified values.
Hope this helps.
By this line
student = db.Students.Find(student.ID);
you overwrite the student that enters the method and you lose all changes. You can do two things in stead:
Only remove the line. Your code will now attach the modified student as EntityState.Modified.
Fetch the original student from the database and copy the modified values to it:
var studentOrg = db.Students.Find(student.ID);
db.Entry(studentOrg).CurrentValues.SetValues(student);
(both SaveChanges calls can be moved to one just before return RedirectToAction...)
Option 1 will generate an update statement containing all Student's fields, but not have a roundtrip to get the original record.
Option 2 has this roundtrip, but only updates modified fields. This (option 2) can be beneficial when changes are audited or when concurrency should be minimized.

Two checks IValidatableObject in one entity

Is the essence of Project, the creation of which is necessary to check whether there is already an entity with the same name. When editing needs such as checking, but keep in mind that the old and the new name of the entity can be matched.
You also need to display an error message. For this I use interface IValidatableObject, but do not know how to tell the Validate method the object is currently being edited or created
DbContext.ValidateEntity takes the IDictionary<Object, Object> items as the second parameter. You can pass any data there and the data you pass will be passed to IValidatableObject.Validate in the ValidationContext.Items
Assuming you refer to check EF cant do for you.
This is actually difficult to check. You are checking an entity after it has been added to the context. It should not check itself and needs to consider other items in context that are not yet saved. As well as the DB. There are several 3 combinations plus an self recognition. Record a an entity record in LOCAL when ID is blank/new ie multiple new inserts needs careful coding. (Consider using temp IDs)
the not yet saved entries should be in context
Context.Set<TPoco>().Local
and get data from DB and keep in a temp list. BUT dont put in context.
Or use a SECOND context.
var matchingSet = Context.Set<TPoco>().AsNoTracking() // not into context...
.Where(t=>t.field == somevalue).ToList();
So what about logical and actual duplicates on the DB. Logical duplicates are duplicates on a field with no unique index that from a business perspective should be unique.
If you want to check those...
You need to read the DB.... BUT if these records are currently being changed, you CAN NOT just put them into the Context. You would overwrite them.
But what if the values the logical key values have changed?
Something caused a logical dup on a record on the DB may no longer be a dup once saved or vice verse. Is that still a dup or not ?
So you need to decide how you match LOCAL versus loaded records.
Ie check LOCAL and matching DB records and decidr what to do if a record is in both, only local or only db.
LOCAL ONLY and DB Only is easy.
But in both... That is your business process decision.
Problem is solved using method ModelState.AddModelError (string, string) in actions Edit and Create.
[HttpPost]
[HandleError(View="AjaxError")]
public ActionResult Edit(ProjectsViewData data)
{
if (ModelState.IsValid)
{
if (!ContainsProject(data.CurrentObject.Name))
{
db.Projects.Attach(data.CurrentObject);
db.ObjectStateManager.ChangeObjectState(data.CurrentObject, EntityState.Modified);
db.SaveChanges();
return Projects(data);
}
else
{
int projectId = (from p in db.Projects
where p.Name == data.CurrentObject.Name
select p.ProjectID).FirstOrDefault();
if (projectId == data.CurrentObject.ProjectID)
{
db.Projects.Attach(data.CurrentObject);
db.ObjectStateManager.ChangeObjectState(data.CurrentObject, EntityState.Modified);
db.SaveChanges();
return Projects(data);
}
else
{
ModelState.AddModelError("Name", Localizer.ProjectAlreadyExists);
}
}
}
data.ObjectToEdit = data.CurrentObject;
return Projects(data);
}
[HttpPost]
[HandleError(View = "AjaxError")]
public ActionResult Create(ProjectsViewData data)
{
if (ModelState.IsValid)
{
if (!ContainsProject(data.CurrentObject.Name))
{
db.Projects.AddObject(data.CurrentObject);
db.SaveChanges();
return Projects(data);
}
else
{
ModelState.AddModelError("Name", Localizer.ProjectAlreadyExists);
}
}
data.ObjectToAdd = data.CurrentObject;
return Projects(data);
}
Helper method:
private bool ContainsProject(string projectName)
{
if (projectName != null)
{
projectName = Regex.Replace(projectName.Trim(), "\\s+", " ");
List<string> projects = new List<string>();
var projectNames = (from p in db.Projects
select p.Name.Trim()).ToList();
foreach (string p in projectNames)
{
projects.Add(Regex.Replace(p, "\\s+", " "));
}
if (projects.Contains(projectName))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}

Is there a way to flush the contents of a view at some point without using if-else statements?

Is there a way to flush the contents of a view at some point without using if-else statements?
For example, If I'm listing a bunch of records using a table, but I don't need the headers if no rows are found: maybe a message saying, "No data found". I'd like to:
if (records.count == 0) {
<div>No Data</div>
return; //flush/render view
}
//no else
<table><tr><th>......</tr>
foreach (var record in records) {
<tr><td>....</tr>
}
</table>
Makes sense? Or I'm just being lazy: I hate huge chunks of nested code, especially inside a view where indentation is not so sleak.
Try like this, it should work:
#if (records.count == 0)
{
<div>No Data</div>
return;
}
<table>
<tr><th>......</tr>
foreach (var record in records)
{
<tr><td>....</tr>
}
</table>
If the condition (records.count == 0) is satisfied only No Data will be printed on the Razor view and the subsequent code will never be executed and the table will never be shown.

EntityFramework ObjectContext Refresh issue

I have DataContext.Refresh Method:
public void RefreshDataSource()
{
_entities.Refresh(RefreshMode.ClientWins,Departments);
}
And observable collection:
public ObservableCollection<Department> Departments
{
get
{
if (_departments == null && _entities != null)
{
_entities.Departments.Include("Drivers").ToArray();
_departments = new EntityObservableCollection<Department>(_entities.Departments);
}
return _departments;
}
}
If i update records outside context i see only changed records but can't see inserted and removed. Why?
Because Refresh doesn't look for new records. It takes records you already have and updates them with current values. It also probably doesn't handle deleted records especially if you use ClientWins strategy which takes your state as more important.

Resources