java code to populate an transient attribute in Oracle ADF - oracle-adf

I very new to Oracle ADF.
I have created a view object and it has a transient attribute. (Select literal and provided no value.)
Now I want to populate the column using another column of the same viewobject(some business calculations are there) from a Java class.
Any idea which class or how I can do that?
Is it rowImpl class that I have to write my logic? If so can you give an example?
here is an example of what I am looking to create. in Hr schema we have Department and Employees table liked with foreign key relationship.
DepartmentName Noofemployees Percentage
HR 2 10.52631579
Admin 8 42.10526316
Engineer 9 47.36842105
Grand totatal 19 100
here percentage is my transient attribute. So value is 2/19*100 OR 8/19*100 ,etc

Why not consider Groovy for calculating the value of the transient attribute by setting it to "expression" then writing a Groovy expression to refer to the other attributes in the same VO.
Using Groovy Whitepaper
Webinar
Sample
Docs

Solution:
In departments VO create a transient attribute "TotalEmployees" with the following expression :
EmployeesView.count("Employee_id")
this gives you a count of employees in each department.
Now in DepartmentsView.xml create a view accessor , add DepartmentsView.
Thus now u can access each row of the same view with this accessor.
Create another transient attribute named "Percentage"
In the expression write the following code:
if(TotalEmployees == null){
return TotalEmployees}
else {
return TotalEmployees/DepartmentsView.sum("TotalEmployees")*100
}
Note: null condition is to avoid div by null value exception.

you can let jdev create the ViewRowImpl class which will give you geetter and setter methods for the attributes. in the get of your transient attribute you implement you calculations and return the result.

Related

Query of Arrays in Salesforce

I need to do 1 of two things (I believe):
1- Get a Custom Object ID so I can query it directly
2- Get a list of values of a specific field within the Object entries.
Ultimate End goal:
Add and modify rows in my custom object via external API. However to do this I need to check and make sure my new entry/row does not already exist.
What I have:
I have a custom object (called Customer_Arrays__c). It is a table that I can add new rows to (I will call entrys). Each entry has 6 or 7 fields. 1 of these fields is called (external_ID__c). This is the field I utilize to match to new incoming data to see if the entry already exists, or if it needs to add a new row to my table. This Customer_Arrays__c is a child to my opportunity I believe – it is part of every opportunity and each line item I add has a field defaulted to the opportunity.
Help I need:
1- How do I query the value of my Cutomer_Arrays__c based upon an opportunity ID?
2- How do I query a list of values in my (external_ID__c) based upon an opportunity ID?
Thanks for your help! I have read half a dozen+ posts on similar topics and am missing something. Examples of some Past try's that failed:
Select external_ID__c,FROM Custom_Arrays__c WHERE Opportunity='00...'
Select Id (Select ID, Custom_Arrays__c from Custom_Arrays__c) from Opportunity where id ='00...'
List FROM Custom_Arrays__c WHERE Opportunity='00...'
Select Id, external_ID__c, (Select external_ID__c FROM Custom_Arrays__c) WHERE Opportunity__c='00...'
Thanks again!
Only you know how did you name the lookup field (foreign key) from arrays to Opportunity. You'll need to check in setup, next to where external_ID__c is. Since it's a custom field (gets __c at the end), my guess is you went with default.
Try
SELECT Id, Name, External_Id__c
FROM Customer_Arrays__c
WHERE Opportunity__c = '006...'
Thank you eyescream, that got me almost all the way there. Turns out I also needed a __r for the parent child relationship.
Here is a snip out of my final code that works - I think it covers everything:
SELECT Field1__c, Opportunity__r.Id, Opportunity__r.Opportunity__c,
FROM Customer_Arrays__c
WHERE Opportunity__r.Id = '006...'.
Thank you so very much!!!

Changing abbreviation values coming for Database in ADF view Object

I am using ADF JDeveloper 11g Release 2
I am using Entity object called Project referring to actual database table. This table contains fileds that hold abbreviation values; for example this table have filed called STATUS that describe the current status for the project. this filed will have values like: 'A' for Approved, 'X' for Rejected, and so on.
In the interface( JSPX, or JSF Pages) I am just drag and drop the View object that refers to the Project Entity object. and the page will display the project records with their status as specified.
Question is:
Is there any way to change this observation values to the actual value somewhere; That is, Instead of having values like ( A, X,...) I want to have ( Approved, Rejected,...)
You can create a transient attribute at VO level. In the value of this attribute you can write groovy expression which will use the value of attribute named status and decode it.
Alternatively, you can alter the VO query using DECODE function by doing something like this :
SELECT name and other fields needed,
DECODE(status, 'A', 'Approved',
'X', 'Rejected',
'P', 'Pending',
'Default') decodedstatus
FROM projects;
You will need to have an additional attribute in VO in this case and can directly use the value returned by VO in UI.

objectify query filter by list in entity contains search parameter

in an app i have an entity that contains a list of other entities (let's say an event holding a list of assigned employees)
using objectify - i need to find all the events a particular employee is assigned to.
is there a basic way to filter a query if it contains the parameter - kind of the opposite of the query in
... quick pseudocode
findAll(Employee employee) {
...
return ofy.query(Event.class).filter("employees.contains", employee).list();
}
any help would be greatly appreciated
i tried just doing filter("employees", employee) after seeing this http://groups.google.com/group/objectify-appengine/browse_thread/thread/77ba676192c08e20 - but unfortunately this returns me an empty list
currently i'm doing something really inefficient - going through each event, iterating through the employees and adding them to a new list if it contains the given employee just to have something that works - i know this is not right though
let me add one thing,
the above query is not actually what it is, i was just using that because i did not think this would make a difference.
The Employee and Events are in the same entity group with Business as a parent
the actual query i am using is the following
ofy.query(Event.class).ancestor(businessKey).filter("employees", employee).list();
unfortunately this is still returning an empty list - does having the ancestor(key) in there mess up the filter?
solution, the employees field was not indexed correctly.
I added the datastore-indexes file to create a composite index, but was testing originally on a value that I added before the employees field was indexed, this was something stupid i was doing - simply having an index on the "business" field and the "employees" field fixed everything. the datastore-indexes file did not appear to be necessary, after deleting it and trying again everything worked fine.
Generally, you do this one of two ways:
Put a property of Set<Key<Employee>> on the Event
or
Put a property of Set<Key<Event>> on the Employee
You could also create a relationship entity, but if you're just doing filtering on values with relatively low counts, usually it's easier to just put the set property on one entity or the other.
Then filter as you describe:
ofy.query(Event.class).filter("employees", employee).list()
or
ofy.query(Employee.class).filter("events", event).list()
The list property should hold a Keys to the target entity. If you pass in an entity to the filter() method, Objectify will understand that you want to filter by the key instead.
Example :
/***************************************************/
#Entity
#Cache
public class News {
#Id Long id;
String news ;
#Index List<Long> friend_list = new ArrayList<Long>();
// My friends who can see my news , exemele : friend_list.add(id_f1); friend_list.add(id_f2); friend_list.add(id_f3);
//To make an operation on "friend_list", it is obligatory to index it
}
/*************************************************/
public News(Long id_f){
List<Long> friend_id = new ArrayList<Long>();
friend_id.add(id_f);
Query<Nesw> query = ofy().load().type(News.class).filter("friend_list in",friend_id).limit(limit);
//To filter a list, just after the name of the field you want to filter, add "IN".
//here ==> .filter("friend_list in",friend_id);
// if friend_list contains "id_friend" ==> the query return value
.........
}

Elementary Apex Object IDs

Quick Question. In the below code, you can see that the for loop (which takes all of the records in newTimecards and puts them as a variable called timecard) and adds the Resource_c to the resourceIds set. I'm confused about how this object is considered an ID data type. When an object is made in Salesforce does it automatically have an ID made, so that it knows Resource_c ID can be added to a set? Note that within the Resource_c Object there is also a field called Resource_ID_c. Resource_c within Timecard_c is a Master-Detail data type. Resource_c is the parent of Timecard_c.
Now that I think about it, resourceIds.add(timecard.Resource_c), does that reference the relationship between the two objects and then searches through Resource_c and adds the ID field Resource_ID_c automactically since it's a unique field?
Thanks for your help.
public class TimecardManager {
public class TimecardException extends Exception {}
public static void handleTimecardChange(List<Timecard__c> oldTimecards,
List<Timecard__c> newTimecards) {
Set<ID> resourceIds = new Set<ID>();
for (Timecard__c timecard : newTimecards) {
resourceIds.add(timecard.Resource__c);
}
Every object instance (and that means EVERY, including factory ones) has a unique organization level ID, whose field name is always Id, is covered by Apex type ID and is a case-sensitive string of 15 characters that also has an 18 character case-insensitive representation. The first three characters are object prefix code (e.g. 500 for a Case) so all instances of the same object share the same prefix. You see these values all across SF (for example in https://na1.salesforce.com/02s7000000BW59L the 02s7000000BW59L in the URL is the ID). When an instance of the object is created using INSERT DML operation, the salesforce automatically assigns unique value based on the prefix and the next available transactional sub ID, it all happens transparently to you.
This is not to be confused with object Name field which is a field you define when you create an object and which can be auto-incremented and so on (e.g. MYOBJ-{00000}) and which can have more meaning to a user than a cryptic ID
When you create a lookup or master-detail relationship it is ID that is being used to link the two instances, not the Name. In the above example Resource__c seems to be that lookup field and it contains Id value of row's master.
What the code does is it enumerates all resources used in timelines and builds a set of their IDs, the purpose of which is most probably to be used via WHERE Id IN :resourceIds clause to load resource details from master table.
mmix's answer is a great overview to what an ID is and where it comes from. To answer what I think is your specific question:
Any time there is a reference from one object to another (like here, between Timecard_c and Resource_c), the field representing the reference will be an ID. So, the for loop that calls resourceIds.add(timecard.Resource__c) is just building up your set of ID's (those 15-character strings). The timecard.Resource__c doesn't look through the Resource__c table to find the ID, timecard.Resource__c is the ID.

Set a custom identity value on a auto increment field

I have in my DB (Sql server 2008) a id column with auto numeric set on.
I'm using EF and linq2entities
In some specific scenario I would like to be able to set a custom Id number (obviously I'm totally sure this value is not repeated), for example I would use it to "fill" missing Id numbers caused by deletions. I want to keep the auto increment prop in database, the problem is that when I do the linq sentence, the database assign the next Id number, not the one that I like.
Maybe it's a little weird but is it possible to do using linq2entities ?
Thanks in advance!
I believe Its not possible unless there is some way to turn off "SET Identity_Insert TableName ON" within Entity Framework.
Basically in SQL Server when you sent Identity on a field it cannot be populated manually unless you run the following statement
SET Identity_Insert TableName ON
After running this statement you will be able to populate Identity Fields manually.
The only other options I can think of is to remove the Identity attribute from the column and create your own incrementer for the field in the Entity Framework using a partial Class
Something like this
public partial class EntityClassName : global::System.Data.Objects.DataClasses.EntityObject, IEntity
{
partial void InitializeFields();
Int64 IEntity.IdentityColumn
{
get { return IdentityColumn; }
set { //some code for an incrementer
//and the ability to set manually
//if value provide is not null
}
}
}
I don't like to say flat-out that it's impossible, but this is pretty inside baseball for L2E. But it's a pretty simple INSERT trigger. You'll get the inserted row via INSERTED (Google will explain), and then you update that row with whatever crazy logic you want.
I think you can bang your head against L2E for hours trying to figure it out, or do it inside of twenty minutes with a trigger.

Resources