12.1.3. OAF. How to join 2 View Object which populating programmatically? - oracle

I have 2 View Object which populating programmatically, i.e. this objects don't have a SQL query in Query Statement region. There is HeaderVO and LinesVO. My task is display advanced table in advanced table. And this advanced tables based on HeaderVO and LinesVO. If I use View Link than the HeaderVO table displays data but LinesVO table displays only "No search conducted". It's logically and I understand why it is.
But how can I connected this 2 tables (View Objects)?

As the VO populates programatically, you can try by creating the View Link between these VOs also programatically. You can use the below method for the same :
Assume Master VO as deptVO and Detail VO as empVO.
// Build an attribute array, consisting of deptVO.DeptNum for Master VO
AttributeDef[] deptAttrs = new AttributeDef[1];
deptAttrs[0] = deptVO.findAttributeDef("DeptNum");
// Build an attribute array, consisting of empVO.DeptNum for Detail VO
AttributeDef[] empAttrs = new Attributedef[1];
empAttrs[0] = empVO.findAttributeDef("DeptNum");
ViewLink vl = myAM.createViewLinkBetweenViewObjects("yourVLName",
"VLAccessor", //accessor name
deptVO, //master VO
deptAttrs, //master VO attribute
empVO, //detail VO
empAttrs, //detail VO attribute
null); //association clause

In order to have a Master-Detail relationship in OAF advancedTable component, the detail VO child attribute must be correctly mapped. As you are programmatically defined Master VO and Child VO, please ensure that this step has been completed. Are you creating advancedTable declaratively or programmatically?
createViewLinkBetweenViewObjects API
ViewObject voDept = am.createViewObject("MyDeptVO", "package1.DeptView");
ViewObject voEmp = am.createViewObject("MyEmpVO", "package1.EmpView");
AttributeDef[] deptLinkAttrs = new AttributeDef[] { voDept.findAttributeDef("Deptno") };
AttributeDef[] empLinkAttrs = new AttributeDef[] { voEmp.findAttributeDef("Deptno") };
ViewLink vl = am.createViewLinkFromEntityAssocName("MyDeptEmpLink",
"Employees",
voDept, deptLinkAttrs,
voEmp, empLinkAttrs,
null);

Related

Entity Framework Core - Upsert entities from other database encounters tracking problems

I have a flatfile from a different database. I import it and map it to my application's entities. Because the flatfile does not contain ids I cannot be sure the entries I handle are not duplicates of what has already been added to my database earlier or to my context at this moment.
The error message I get is:
The instance of entity type 'Car' cannot be tracked because another
instance with the same key value for {'Make', 'Model'} is already
being tracked. When attaching existing entities, ensure that only one
entity instance with a given key value is attached. Consider using
'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the
conflicting key values.
An example:
Data rows from flatfile
Volvo V70 Steve
Volvo V70 John
Having mapped these rows and trying to put them in db
foreach(var row in flatFileRows){
Car existingCar = null;
if(dbContext.Cars.Any(c => c.Make == row.Make && c.Model == row.Model)){
existingCar = dbContext.Cars
.SingleOrDefault(c => c.Make == row.Make && c.Model == row.Model);
}
//I also do the same for existingDriver
var car = existingCar != null
? existingCar
: new Car()
{
Make = row.Make,
Model = row.Model,
Drivers = new List<Driver>();
};
var driver = new Driver()
{
CarId = existingCar != null ? exsitingCar.Id : 0,
Name = row.Name
};
car.Drivers.Add(driver);
dbContext.Cars.Update(car); //Second time we hit this the error is thrown
}
dbContext.SaveChanges();
Make and Model are set to keys in the schema because I don't want duplicate entries of the car models.
The above example is simplified.
What I want is to check if I already put a car in the db with these attributes and then build according to my schema from that entity. I don't care to track any entries, disconnected or otherwise, because I just need to populate the database.

Oracle ADF LOVs value binding to multiple text fileds

I have a use case where I have created a view object that contains 3 values namely LOC_CODE, LOC_DESC, CITY_DESC. Now in my ADF form I would like to display all 3 values in such a way so that user would have a provision to select LOC_CODE From Popup(LOV) and rest two fileds LOC_DESC & CITY_DESC should be changed accordingly. Currently the popup shows all 3 values but when I select the row and click on OK button it only fills the LOC_CODE in 1 textbox.
Below is the scenario of the same:
Got the solution. Just need to add a textbox or drag and drop near respective field and bind it with required binding object. For e.g. in this case LOC_DESC & CITY_DESC is available in my data control as DefLoc & DefCity that contains SQL to fetch respective description value. Now I need to drag and drop DefLoc & DefCity and binding is automatically done or just check binding in value.
you have to add valuechangelistener to location code. set autosubmit true.
now in backing bean use following code:
public void valuechangelistener(ValueChangeEvent valueChangeEvent) {
valueChangeEvent.getComponent().processUpdates(FacesContext.getCurrentInstance());
BindingContext bctx = BindingContext.getCurrent();
BindingContainer bindings = bctx.getCurrentBindingsEntry();
JUCtrlListBinding list = (JUCtrlListBinding)bindings.get("LOC_CODE");
String selectedValue = (String)list.getAttributeValue();
list.getListIterBinding().setCurrentRowWithKeyValue(selectedValue);
Row currRow = list.getListIterBinding().getCurrentRow();
if (currRow != null) {
bndloc_desc.setValue(currRow.getAttribute("LOC_DESC"));
bndcity_desc.setValue(currRow.getAttribute("CITY_DESC"));
}
}
now set partial trigger to both location desc and city desc with id of LOC_CODE.
After doing this you will get your desired result.
update after implementing it.
In my case JDeveloper 12.2.1.3.0
public void valueChangeListener(ValueChangeEvent valueChangeEvent) {
BindingContext bctx = BindingContext.getCurrent();
BindingContainer bindings = bctx.getCurrentBindingsEntry();
JUCtrlListBinding list = (JUCtrlListBinding) bindings.get("YourBindingforLOV");
String selectedValue = (String) valueChangeEvent.getNewValue();
list.getListIterBinding().setCurrentRowWithKeyValue(selectedValue);
Row currRow = list.getListIterBinding().getCurrentRow();
if (currRow != null) {
String s = (String) currRow.getAttribute("YourAttributeName");
}
}

LLBLGEN: Load a EntityCollection or List from a datatable

How do I load an EntityCollection or List(Of Entity) from a DataTable using LLBLGen?
A datatable holds its values in rows and columns whereas a LLBLGen Collection class holds a collection of Entity objects that represent a table in your persistent storage. You can fetch a DataTable of fields that you define with a ResultsetFields via the TypedListDAO. However, going from a DataTable to an EntityCollection is not possible unless your Entity objects are stored in your DataTable.
More likely, you have some keys in your DataTable. If this is the case, you'll need to iterate over the rows of the DataTable, pull out the keys and create new Entity objects from these. Then you can add these Entity objects to your EntityCollection.
// Define the fields that we want to get
ResultsetFields fields = new ResultsetFields(2);
fields.DefineField(EmployeeFields.EmployeeId, 0);
fields.DefineField(EmployeeFields.Name, 1);
// Fetch the data from the db and stuff into the datatable
DataTable dt = new DataTable();
TypedListDAO dao = new TypedListDAO();
dao.GetMultiAsDataTable(fields, dt, 0, null, null, null, false, null, null, 0, 0);
// We now have a datatable with "EmployeeId | Name"
// Create a new (empty) collection class to hold all of the EmployeeEntity objects we'll create
EmployeeCollection employees = new EmployeeCollection();
EmployeeEntity employee;
foreach(DataRow row in dt.Rows)
{
// Make sure the employeeId we are getting out of the DataTable row is at least a valid long
long employeeId;
if(long.TryParse(row["EmployeeId"].ToString(), out employeeId))
{
// ok-looking long value, create the Employee entity object
employee = new EmployeeEntity(employeeId);
// might want to check if this is .IsNew to check if it is a valid object
}
else
{
throw new Exception("Not a valid EmployeeId!");
}
// Add the object to the collection
employees.Add(employee);
}
// now you have a collection of employee objects...
employees.DoStuff();

Auditing in Entity Framework

After going through Entity Framework I have a couple of questions on implementing auditing in Entity Framework.
I want to store each column values that is created or updated to a different audit table.
Right now I am calling SaveChanges(false) to save the records in the DB(still the changes in context is not reset). Then get the added | modified records and loop through the GetObjectStateEntries. But don't know how to get the values of the columns where their values are filled by stored proc. ie, createdate, modifieddate etc.
Below is the sample code I am working on it.
// Get the changed entires( ie, records)
IEnumerable<ObjectStateEntry> changes = context.ObjectStateManager.GetObjectStateEntries(EntityState.Modified);
// Iterate each ObjectStateEntry( for each record in the update/modified collection)
foreach (ObjectStateEntry entry in changes)
{
// Iterate the columns in each record and get thier old and new value respectively
foreach (var columnName in entry.GetModifiedProperties())
{
string oldValue = entry.OriginalValues[columnName].ToString();
string newValue = entry.CurrentValues[columnName].ToString();
// Do Some Auditing by sending entityname, columnname, oldvalue, newvalue
}
}
changes = context.ObjectStateManager.GetObjectStateEntries(EntityState.Added);
foreach (ObjectStateEntry entry in changes)
{
if (entry.IsRelationship) continue;
var columnNames = (from p in entry.EntitySet.ElementType.Members
select p.Name).ToList();
foreach (var columnName in columnNames)
{
string newValue = entry.CurrentValues[columnName].ToString();
// Do Some Auditing by sending entityname, columnname, value
}
}
Here you have two basic options:
Do it at the database level
Do it in the c# code
Doing it at the data base level, means using triggers. In that case there is no difference if you are using enterprise library or another data access technology.
To do it in the C# code you would add a log table to your datamodel, and write the changes to the log table. When you do a save changes both the changes to the data and the information which you wrote to the log table would be saved.
Are you inserting the new record using a stored proc? If not (i.e. you are newing up an object, setting values, inserting on submit and then saving changes the new object id will be automatically loaded into the id property of the object you created. If you are using a stored proc to do the insert then you need to return the ##IDENTITY from the proc as a return value.
EX:
StoreDateContext db = new StoreDataContext(connString);
Product p = new Product();
p.Name = "Hello Kitty Back Scratcher";
p.CategoryId = 5;
db.Products.Add(p);
try
{
db.SaveChanges();
//p.Id is now set
return p.Id;
}
finally
{
db.Dispose;
}

Linq Query Using DataTable with Paging

I have a Linq query that I copy to a DataTable which is then used to populate a gridview. I am using a group by "key" and "count" which I evaluate in the aspx page for a master/detail gridview with a repeater.
The problem that I am encountering is that the gridview datasource and bind to the datatable is not presenting me with any additional pages that are part of the data.
My query is:
// Using Linq generate the query command from the DataTable
var query = from c in dtDataTable_GridView.AsEnumerable()
group c by c.Field<string>("CLIN") into g
select new
{
Key = g.Key,
Count = g.Count(),
Items = from i in g
select new
{
CLIN = i.Field<string>("CLIN"),
SLIN = i.Field<string>("SLIN"),
ACRN = i.Field<string>("ACRN"),
CLINType = i.Field<string>("CLINType"),
Option = i.Field<string>("Option"),
Unit = i.Field<string>("Unit")
}
};
// Use extension methods to create new DataTable from query
dtTaskOrderTable = query.CopyToDataTable();
// Set the datasource
gridview1.DataSource = dtTaskOrderTable;
// Bind to the GridView
gridview1.DataBind();
If I use the original datatable (dtDataTable_GridView) directly I have paging but once I do the Linq Query and copy it back to a new datatable (dtTaskOrderTable) I lose the paging feature.
Also how do I get a value from a column name ("Option" for instance) if it is part of "Items"?
Any help would be appreciated.
Thanks,
ChrisB
Please disregard the previous answer I will delete it
It requires ICollection interface for paging.
Neither IEnumerable nore IQuerable will not work
List<(Of <(T>)>) will work as Lists implement Icollection interface
So you need
gridview1.DataSource = dtTaskOrderTable.ToList();

Resources