I have a requirement in which i need to correct wrong data before it is completely posted to the DB...
I found that validations can be done in View Object Impl Class. I tried to make changes in the Code with the following code:
public void beforeCommit(TransactionEvent e) {
Row row = this.getCurrentRow();
Row[] locationRows = this.getAllRowsInRange();
System.out.println(this.getCurrentRowIndex());
for (int i = 0; i < locationRows.length; i++) {
System.out.println(locationRows[i].getAttribute("PortSequenceNo"));
locationRows[i].setAttribute("PortSequenceNo", 100 + i);
this.validate();
System.out.println("validated");
this.postChanges(e);
System.out.println("Changes posted");
}
validatePSN(locationRows);
super.beforeCommit(e);
}
But this code is giving below error :
javax.faces.el.EvaluationException: oracle.jbo.JboException:
JBO-28202: Entities invalidated in beforeCommit(). Need to revalidate
and post. at
org.apache.myfaces.trinidadinternal.taglib.util.MethodExpressionMethodBinding.invoke(MethodExpressionMethodBinding.java:58)
at
org.apache.myfaces.trinidad.component.UIXComponentBase.broadcastToMethodBinding(UIXComponentBase.java:1433)
Please help in as what should i do so that i can make the last minute changes to the actual values posted through the ADF form
Use the doDML method of the EntityImpl class.
Javadoc:
EntityImpl doDML method
Override this method to provide custom logic for processing inserts,
updates, and deletes.
You should override method protected void prepareForDML(int operation, TransactionEvent transactionEvent).
At this point you can safely modify values of attributes.
Related
Suppose POLine has a UsrCustomfield and also have some calculation to populate the value for the UsrCustomfield based on other fields in the same POLine.
Which event handlers are used to populate and save the value to the DB? Saving has to be done from Graph itself... But from where?
You have multiple ways to calculate the value of a custom field.
The first method is using a PXFormula attribute. This can work well if you have an easy query to calculate the value of the field. This would be set on the DAC extension of POLine that you declared the UsrCustomfield as an attribute. The help article has many different ways to accomplish this.
https://help-2018r1.acumatica.com/Wiki/(W(59936))/ShowWiki.aspx?pageid=1d25dc74-747c-4d44-8ad9-033e5a476b6f
Another way to accomplish this is trigger events based on the fields, or row, updating to re-calculate. For example, if you want to re-calculate every time the row is updated with ANY field, you can use the function:
public virtual void POLine_RowUpdated(PXCache sender, PXRowUpdatedEventArgs e, PXRowUpdated del)
{
//first, invoke the base method. can be placed anywhere in your code.
del?.Invoke(sender, e);
//get the row
POLine line = (POLine)e.Row;
if (line == null) return; //always good to check if the object is not null
//get the DAC extension from the object
POLineExt lineExt = line.GetExtension<POLineExt>();
decimal? CalculatedValue = 0;
//do some calculation to figure out what the value should be
//compare the new value against the existing value
if(lineExt.UsrCustomField != CalculatedValue)
{
//set the new value
sender.SetValueExt<POLineExt.usrCustomField>(line, CalculatedValue);
}
}
The issue with this is that it will call the code every time a POLine is updated. This may be too many calls. You can use functions to update each individual line using a FieldUpdated event on each field to recalculate as well. For example, you may want to check ExtCost:
public virtual void POLine_ExtCost_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e, PXFieldUpdated del)
{
//similar code as above
}
This method is more code, and more complex, but may be needed if your logic cannot fit into a PXFormula attribute.
In page edit mode I want to show a read-only text that is based on a page property value. The text could for example be "A content review reminder email will be sent 2015-10-10", where the date is based on the page published date + six months (a value that will be configurable and therefore can change anytime). So far I've tried to accomplish something like this by adding another property on the page.
I've added the property CurrentReviewReminderDate to an InformationPage class we use. In page edit mode the property name is shown, but it doesn't have a value. How do I do to show the value in page edit mode (preferably as a label)?
[CultureSpecific]
[Display(
Name = "Review reminder date",
Description = "On this date a reminder will be sent to the selected mail to remember to verify page content",
Order = 110)]
[Editable(false)]
public virtual string CurrentReviewReminderDate
{
get
{
var daysUntilFirstLevelReminder =
int.Parse(WebConfigurationManager.AppSettings["PageReviewReminder_DaysUntilFirstLevelReminder"]);
if (CheckPublishedStatus(PagePublishedStatus.Published))
{
return StartPublish.AddDays(daysUntilFirstLevelReminder).ToString();
}
return "";
}
set
{
this.SetPropertyValue(p => p.CurrentReviewReminderDate, value);
}
}
EPiServer internally uses the GetPropertyValue method (i.e. the opposite of SetPropertyValue) when retrieving content for the UI.
This makes sense, otherwise your "made-up" value would be stored as the real value whenever the content is saved. This would make fall-back values etc impossible to implement.
So, this is by-design (and quite wisely so) in EPiServer. :)
However, you can customize how properties work by:
Using custom editors by applying UI hints
Modifying property metadata (for example, to display a generated value as a watermark in a textbox without interfering with the actual value being saved)
I could be misunderstanding what you're trying to do, but off the top of my head it looks like a custom editor could be a viable option for your use case?
Another solution would be to hook into the LoadedPage-event and add the value from there. This might not be the best way performance-wise since you need to do a CreateWritableClone, but depending on the site it might not matter.
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class EventInitialization : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
ServiceLocator.Current.GetInstance<IContentEvents>().LoadedContent += eventRegistry_LoadedContent;
}
void eventRegistry_LoadedContent(object sender, ContentEventArgs e)
{
var p = e.Content as EventPage;
if (p != null)
{
p = p.CreateWritableClone() as EventPage;
p.EventDate = p.StartPublish.AddDays(10);
e.Content = p;
}
}
}
So I'm trying to prevent a race condition between applications.
Using IsolationLevel/TransactionScope, I can lock the table the way I need to, but need to run the update operation first, then operate on the list of modified objects.
To do this, I need to run the update and get the list of updated ID's all in one shot.
If I were to try to take the IDs first, that wouldn't lock the table, and another app instance could query for that same list, before they were flagged.
Is there a way to do something like:
//modify some objects
var updatedIds = context.SaveChanges();
//Process updatedIds
Is there a way to do this? I've tried looking through the ObjectContext entries, but after the Save there doesn't seem to be anything.
Maybe I'll have to do an sproc?
This code can go into your Context class and should give you what you need
public override int SaveChanges()
{
using (var scope = new System.Transactions.TransactionScope())
{
//pre processing
var result = base.SaveChanges();
//post processing
scope.Complete();
return result;
}
}
I'm exploring query notifications with the SQLDependency class. Building a simple working example is easy, but I feel like I'm missing something. Once I step past a simple one-table/one-dependency example I'm left wondering how can I figure out which dependency triggered my callback?
I'm having a bit of trouble explaining, so I included the simple example below. When AChange() is called I cannot look at the sql inside the dependency, and i don't have a reference to the associated cache object.
So what's a boy to do?
Option 1 - create a distinct function for each object i want to track and hard code the cache-key (or relevant information) in the callback. This feels dirty & eliminates the posibility of adding new cache items without deploying new code--ewww.
Option 2 - Use the Dependency Id property and a parallel tracking structure
Am I just missing something? Is this a deficiency in the SQLDependency structure? I've I've looked at 20 different articles on the topic and all of them seem to have the same hole. Suggestions?
Code Sample
public class DependencyCache{
public static string cacheName = "Client1";
public static MemoryCache memCache = new MemoryCache(cacheName);
public DependencyCache() {
SqlDependency.Start(connString);
}
private static string GetSQL() {
return "select someString FROM dbo.TestTable";
}
public void DoTest() {
if (memCache["TEST_KEY"] != null ) {
Debug.WriteLine("resources found in cache");
return;
}
Cache_GetData();
}
private void Cache_GetData() {
SqlConnection oConn;
SqlCommand oCmd;
SqlDependency oDep;
SqlDataReader oRS;
List<string> stuff = new List<string>();
CacheItemPolicy policy = new CacheItemPolicy();
SqlDependency.Start(connString);
using (oConn = new SqlConnection(connString) ) {
using (oCmd = new SqlCommand(GetSQL(), oConn) ) {
oDep = new SqlDependency(oCmd);
oConn.Open();
oRS = oCmd.ExecuteReader();
while(oRS.Read() ) {
resources.Add( oRS.GetString(0) );
}
oDep.OnChange += new OnChangeEventHandler (AChange);
}
}
memCache.Set("TEST_KEY", stuff, policy);
}
private void AChange( object sender, SqlNotificationEventArgs e) {
string msg= "Dependency Change \nINFO: {0} : SOURCE {1} :TYPE: {2}";
Debug.WriteLine(String.Format(msg, e.Info, e.Source, e.Type));
// If multiple queries use this as a callback how can i figure
// out WHAT QUERY TRIGGERED the change?
// I can't figure out how to tell multiple dependency objects apart
((SqlDependency)sender).OnChange -= Cache_SqlDependency_OnChange;
Cache_GetData(); //reload data
}
}
First and foremost: the handler has to be set up before the command is executed:
oDep = new SqlDependency(oCmd);
oConn.Open();
oDep.OnChange += new OnChangeEventHandler (AChange);
oRS = oCmd.ExecuteReader();
while(oRS.Read() ) {
resources.Add( oRS.GetString(0) );
}
Otherwise you have a window when the notification may be lost and your callback never invoked.
Now about your question: you should use a separate callback for each query. While this may seem cumbersome, is actually trivial by using a lambda. Something like the following:
oDep = new SqlDependency(oCmd);
oConn.Open();
oDep.OnChange += (sender, e) =>
{
string msg = "Dependency Change \nINFO: {0} : SOURCE {1} :TYPE: {2}";
Debug.WriteLine(String.Format(msg, e.Info, e.Source, e.Type));
// The command that trigger the notification is captured in the context:
// is oCmd
//
// You can now call a handler passing in the relevant info:
//
Reload_Data(oCmd, ...);
};
oRS = oCmd.ExecuteReader();
...
And remember to always check the notification source, info and type. Otherwise you run the risk of spinning ad-nauseam when you are notified for reasons other than data change, like invalid query. As a side comment I would add that a good cache design does not refresh the cache on invalidation, but simply invalidates the cached item and lets the next request actually fetch a fresh item. With your 'proactive' approach you are refreshing cached items even when not needed, refresh multiple times before they are accessed etc etc. I left out from the example error handling and proper thread synchronization (both required).
Finally, have a look at LinqtoCache which does pretty much what you're trying to do, but for LINQ queries.
Ok so I am really stuck now. I have a h:datatable and the only way to get the row details seems to be DataModel#getRowData(). Unfortunately ListDataModel which is needed to wrap the necessary data is NOT serializable so will not work on GAE, and I really need it to work! Does anyone have any idea about any workaround or some way to make it function.
Help much appreciated!
Mark the property transient (so that it will be skipped during serialization) and introduce lazy loading in the getter.
E.g.
private List<Item> itemList;
private transient DataModel<Item> itemModel;
public DataModel<Item> getItemModel() {
if (itemModel == null) {
itemModel = new ListDataModel<Item>(itemList);
}
return itemModel;
}
There are by the way alternate ways to retrieve the current row. See also How can I pass selected row to commandLink inside dataTable?