Detecting composite child items deleted - ria

Given a parent that contains child collection, attributed with:
[Include, Composition]
public virtual ICollection<FieldValue> FieldValues {get;set;}
and doing a load:
Context.Load(Context.GetQuery(),LoadBehavior.RefreshCurrent,true);
I see that FieldValues deleted by another user or a background process don't get removed from the client context. I can see how this could be by design, but how do I work around this? Is there a way to plug into load process to handle the deleted items?
Load callback happens too late, by the looks of it - everything has been merged already, there's nothing to compare.

Figured it out. Given a parent class Root, this is the way to run the load:
var originalValues = Context.EntityContainer
.GetEntitySet<FieldValue>()
.ToList(); // <-- make the pre-load copy of the child entities
Context.Load(Context.GetQuery(),
LoadBehavior.RefreshCurrent,
(LoadOperation<Root> o) =>
{
(from v in originalValues
join nv in o.AllEntities.OfType<FieldValue>() on v equals nv into g
from existing in g.DefaultIfEmpty()
where existing == null
select v)
.ToList()
.ForEach(Context.EntityContainer.GetEntitySet<FieldValue>().Detach);
},
null);
Note: FieldValue implements IEquatable<>, use PK if your child doesn't

Related

Get children from ListBox (gtk4)

When I was using gtk3 I was able to access a ListBox's children this way:
let list_box = ListBox::new();
let label = Label::new(Some("Label 1"));
list_box.append(&label);
for row in list_box.children() {
list_box.remove(&row);
}
In gtk4, it seems like children() doesn't exist anymore: "no method named `children` found for struct `gtk4::ListBox`".
I checked ListBox in the gtk4 docs, but I couldn't find anything related to accessing a ListBox's children.
How to access a ListBox's children in gtk4?
There is observe_children, but like it's name suggests you should not modify the container while using it. Or more specifically you can't keep using it's iterator once you modify the underlying container.
For your usecase you can iterate and remove children using first_child/last_child like this:
while let Some(row) = list_box.last_child() {
list_box.remove(&row);
}

How can I ensure CassandraOperations.selectOneById() initializes all fields in the POJO?

I'm using Spring Data Cassandra 1.3.4.RELEASE to persist instances of a class that I have. The class is written in Groovy, but I don't think that really matters. I have implemented a CrudRepository, and I'm injecting an instance of CassandraOperations into the repo implementation class. I can insert, delete, and do most of the other operations successfully. However, there's a scenario I'm running into which breaks my test case. My entity class looks something like this:
#Table("foo")
class FooData {
#PrimaryKey("id")
long id
#Column("created")
long updated
#Column("name")
String name
#Column("user_data")
String userData
#Column("numbers")
List numberList = []
}
In my test case, I happened to only set a few fields like 'id' and 'updated' before calling CassandraOperations.insert(entity), so most of them were null in the entity instance at the time of insertion. But the numberList field was not null, it was an empty List. Directly after the insert(), I'm calling CassandraOperations.selectOneById(FooData.class, id). I get a FooData instance back, and the fields that were initialized when I saved it are populated with data. However, I was checking content equality in my test, and it failed because the empty list was not returned as an empty list in the POJO coming back from CassandraOperations.selectOneById(). It's actually null. I assume this may be some sort of Cassandra optimization. It seems to happen in the CGLIB code that instantiates the POJO/entity. Is this a known "feature"? Is there some annotation I can mark the 'numberList' field with to indicate that it cannot be null? Any leads are appreciated. Thanks.
In short
Cassandra stores empty collections as null and Spring Data Cassandra overwrites initialized fields.
Explanation
Cassandra list/set typed columns represent an empty collection as null. It does not matter whether the list/set (as viewed from Java/Groovy) was empty or null. Storing an empty list yields therefore in null. From here one can't tell whether the state was null or empty at the time saving the value.
Spring Data Cassandra overwrites all fields with values retrieved from the result set and so your pre-initialized fields is set to null.
I created a ticket DATACASS-266 to track the state of this issue.
Workaround
Spring Data uses setters if possible so you have a chance to intervene. A very simple null guard could be:
public void setMyList(List<Long> myList) {
if(myList == null){
this.myList = new ArrayList<>();
return;
}
this.myList = myList;
}
As important addition to mp911de answer you have to set #org.springframework.data.annotation.AccessType(AccessType.Type.PROPERTY) to make this solution work.

GXT re-arrange data array index in TreeStore

Currently, I'm using gxt 3.0.6
I have a TreeStore let's called it "treeStore", with model data "ParentDto".
private TreeStore<ParentDto> treeStore;
treeStore = new TreeStore<ParentDto>(new ModelKeyProvider<ParentDto>(){
#Override
public String getKey(ParentDto item){
return String.valueOf(item.getParentId());
}
});
Inside ParentDto there is a list of ChildDto. If there is ParentDto data which has list of ChildDto, I want to show it in a tree grid. I use basic tree grid from this link
https://www.sencha.com/examples/#ExamplePlace:basictreegrid
Using that reference, if I try to add 1 ParentDto everything works fine, but when the problem is when I add many Parent Dto.
Here is my code for adding data into the treeStore
public void fillTreeStore(List<ParentDto) listParent){
treeStore.clear();
for(ParentDto parentDto : listParent){
treeStore.add(parentDto);
if(parentDto.getListChild().size() > 0){
for(ChildDto childDto : parent.getListChild()){
treeStore.add(parentDto,childDto);
}
}
}
In my case, I only need 1 level parent and child tree so this code is enough.
I try to debug my code use this expression
treeStore.getAll().get(index);
When I add 1 ParentDto (parentA) which has 1 Child (childA). The result will be
treeStore.getAll().get(0) -> contain parentA
treeStore.getAll().get(1) -> contain childA
But if I add 2 ParentDto (parentA, parentB) and each of them have 1 child (childA,childB). The result will be
treeStore.getAll().get(0) -> contain parentA
treeStore.getAll().get(1) -> contain parentB
treeStore.getAll().get(2) -> contain childA
treeStore.getAll().get(3) -> contain childB
But in the grid, those data will be shown perfectly fine :
row 1 : parentA (this row can expand)
row 2 : childA (the expanded row form parentA)
row 3 : parentB (this row can expand)
row 4 : childB (the expanded row form parentB)
I need to render icon if the data is "parent" so I use this code :
(icon_variable).addBeforeRenderIconCellEventHandler(new BeforeRenderIconCellEventHandler() {
#Override
public void onBeforeRenderIconCell(BeforeRenderIconCellEvent event) {
if(treeStore.getParent(treeStore.get(event.getSelectedRowIndex())) == null){
//#render icon here
}
}
});
The problem is at this code
treeStore.get(event.getSelectedRowIndex())
When parentB is added it will trigger addBeforeRenderIconCellEventHandler method. event.getSelectedRowIndex() will get the row index based on "grid's perspective". At the second row, from grid's perspective (childA), event.getSelectedRowIndex() will return 1. But from "treeStore's perspective", index 1 is "parentB", so my icon render is messed up.
That's why, the result I need in treeStore is like this
treeStore.getAll().get(0) -> contain parentA
treeStore.getAll().get(1) -> contain childA
treeStore.getAll().get(2) -> contain parentB
treeStore.getAll().get(3) -> contain childB
My solution :
To solve this problem, for now, I use 2 Stores, the first one is TreeStore, and the second one is ListStore. Each time parent and child are added, I insert them at TreeStore and ListStore. In the ListStore, I keep parent's and child's index to always match with grid's perspective, so that whenever addBeforeRenderIconCellEventHandler is triggered, I use ListStore to get the data.
In my opinion, this solution is not good enough but because in my case, the maximum data can be added into the store less than 50, it's enough.
It looks like this is default behavior. You didn't say what it is you are trying to do but my guess is you can do it with the methods they provide. I'm guessing you are trying to traverse the tree by looking at the parent and then all of it's children before moving on to the next parent. Something like this would do it.
for (ParentDto parent : treeStore.getRootItems()){
for (ChildDto child : treeStore.getChildren(parent)){
}
}

Can't modify/remove a field from an ActivityNode using sbt

I created an ActivityNode (an Entry) and I can add custom fields with the
setFields(List<Field> newListField)
fonction.
BUT
I am unable to modify these fields. (In this case I try to modify the value of the field named LIBENTITE)
FieldList list = myEntry.getTextFields();
List<Field> updatedList = new ArrayList<Field>();
//I add each old field in the new list, but I modify the field LIBENTITE
for(Field myField : list){
if(myField.getName().equals("LIBENTITE")){
((TextField)myField).setTextSummary("New value");
}
updatedList.add(myField);
}
myEntry.setFields(updatedList);
activityService.updateActivityNode(myEntry);
This code should replace the old list of fields with the new one, but I can't see any change in the custom field LIBENTITE of myEntry in IBM connections.
So I tried to create a new list of fields, not modifying my field but adding a new one :
for(Field myField:list){
if(!myField.getName().equals("LIBENTITE")){
updatedList.add(myField);
}
}
Field newTextField = new TextField("New Value");
newTextField .setFieldName("LIBENTITE");
updatedList.add(newTextField );
And this code is just adding the new field in myEntry. What I see is that the other custom fields did not change and I have now two custom fields named LIBENTITE, one with the old value and the second with the new value, in myEntry.
So I though that maybe if I clear the old list of Fields, and then I add the new one, it would work.
I tried the two fonctions
myEntry.clearFieldsMap();
and
myEntry.remove("LIBENTITE");
but none of them seems to work, I still can't remove a custom field from myEntry using SBT.
Any suggestions ?
I have two suggestions, as I had (or have) similar problems:
If you want to update an existing text field in an activity node, you have to call node.setField(fld) to update the field in the node object.
Code snippet from my working application, where I'm updating a text field containing a (computed) start time:
ActivityNode node = activityService.getActivityNode(id);
node.setTitle(formatTitle()); // add/update start and end time in title
boolean startFound = false;
// ...
FieldList textfields =node.getTextFields();
Iterator<Field> iterFields = textfields.iterator();
while (iterFields.hasNext()) {
TextField fld = (TextField) iterFields.next();
if (fld.getName().equals(Constants.FIELDNAME_STARTTIME)) {
fld.setTextSummary(this.getStartTimeString()); // NOTE: .setFieldValue does *not* work
node.setField(fld); // write updated field back. This seems to be the only way updating fields works
startFound=true;
}
}
If there is no field with that name, I create a new one (that's the reason I'm using the startFound boolean variable).
I think that the node.setField(fld) should do the trick. If not, there might be a way to sidestep the problem:
You have access to the underlying DOM object which was parsed in. You can use this to tweak the DOM object, which finally will be written back to Connections.
I had to use this as there seems to be another nasty bug in the SBT SDK: If you read in a text field which has no value, and write it back, an error will be thrown. Looks like the DOM object misses some required nodes, so you have to create them yourself to avoid the error.
Some code to demonstrate this:
// ....
} else if (null == fld.getTextSummary()) { // a text field without any contents. Which is BAD!
// there is a bug in the SBT API: if we read a field which has no value
// and try to write the node back (even without touching the field) a NullPointerException
// will be thrown. It seems that there is no value node set for the field. We
// can't set a value with fld.setTextSummary(), the error will still be thrown.
// therefore we have to remove the field, and - optionally - we set a defined "empty" value
// to avoid the problem.
// node.remove(fld.getName()); // remove the field -- this does *not* work! At least not for empty fields
// so we have to do it the hard way: we delete the node of the field in the cached dom structure
String fieldName = fld.getName();
DeferredElementNSImpl fldData = (DeferredElementNSImpl) fld.getDataHandler().getData();
fldData.getParentNode().removeChild(fldData); // remove the field from the cached dom structure, therefore delete it
// and create it again, but with a substitute value
Field newEmptyField = new TextField (Constants.FIELD_TEXTFIELD_EMPTY_VALUE); // create a field with a placeholder value
newEmptyField.setFieldName(fieldName);
node.setField(newEmptyField);
}
Hope that helps.
Just so that post does not stay unanswered I write the answer that was in a comment of the initial question :
"currently, there is no solution to this issue, the TextFields are read-only map. we have the issue recorded on github.com/OpenNTF/SocialSDK/issues/1657"

CRM PlugIn Pass Variable Flag to New Execution Pipeline

I have records that have an index attribute to maintain their position in relation to each other.
I have a plugin that performs a renumbering operation on these records when the index is changed or new one created. There are specific rules that apply to items that are at the first and last position in the list.
If a new (or existing changed) item is inserted into the middle (not technically the middle...just somewhere between start and end) of the list a renumbering kicks off to make room for the record.
This renumbering process fires in a new execution pipeline...We are updating record D. When I tell record E to change (to make room for D) that of course fires the plugin on update message.
This renumbering is fine until we reach the end of the list where the plugin then gets into a loop with the first business rule that maintains the first and last record differently.
So I am trying to think of ways to pass a flag to the execution context spawned by the renumbering process so the recursion skips the boundary edge business rules if IsRenumbering == true.
My thoughts / ideas:
I have thought of using the Depth check > 1 but that isn't a reliable value as I can't explicitly turn it on or off....it may happen to work but that is not engineering a solid solution that is hoping nothing goes bump. Further a colleague far more knowledgeable than I said that when a workflow calls a plugin the depth value is off and can't be trusted.
All my variables are scoped at the execute level so as to avoid variable pollution at the class level....However if I had a dictionary object, tuple, something at the class level and one value would be the thread id and the other the flag value then perhaps my subsequent execution context could check if the same owning thread id had any values entered.
Any thoughts or other ideas on how to pass context information to a new pipeline would be greatly appreciated.
Per Nicknow sugestion I tried sharedvariables but they seem to be going out of scope...:
First time firing post op:
if (base.Stage == EXrmPluginStepStage.PostOperation)
{
...snip...
foreach (var item in RenumberSet)
{
Context.ParentContext.SharedVariables[recordrenumbering] = "googly";
Entity renumrec = new Entity("abcd") { Id = item.Id };
#region We either add or subtract indexes based upon sortdir
...snip...
renumrec["abc_indexfield"] = TmpIdx + 1;
break;
.....snip.....
#endregion
OrganizationService.Update(renumrec);
}
}
Now we come into Pre-Op of the recursion process kicked off by the above post-op OrganizationService.Update(renumrec); and it seems based upon this check the sharedvariable didn't carry over...???
if (!Context.SharedVariables.Contains(recordrenumbering))
{
//Trace.Trace("Null Set");
//Context.SharedVariables[recordrenumbering] = IsRenumbering;
Context.SharedVariables[recordrenumbering] = "Null Set";
}
throw invalidpluginexception reveals:
Sanity Checks:
Depth : 2
Entity: ...
Message: Update
Stage: PreOperation [20]
User: 065507fe-86df-e311-95fe-00155d050605
Initiating User: 065507fe-86df-e311-95fe-00155d050605
ContextEntityName: ....
ContextParentEntityName: ....
....
IsRenumbering: Null Set
What are you looking for is IExecutionContext.SharedVariables. Whatever you add here is available throughout the entire transaction. Since you'll have child pipelines you'll want to look at the ParentContext for the value. This can all get a little tricky, so be sure to do a lot of testing - I've run into many issues with SharedVariables and looping operations in Dynamics CRM.
Here is some sample (very untested) code to get you started.
public static bool GetIsRenumbering(IPluginExecutionContext pluginContext)
{
var keyName = "IsRenumbering";
var ctx = pluginContext;
while (ctx != null)
{
if (ctx.SharedVariables.Contains(keyName))
{
return (bool)ctx.SharedVariables[keyName];
}
else ctx = ctx.ParentContext;
}
return false;
}
public static void SetIsRenumbering(IPluginExecutionContext pluginContext)
{
var keyName = "IsRenumbering";
var ctx = pluginContext;
ctx.SharedVariables.Add(keyName, true);
}
A very simple solution: add a bit field to the entity called "DisableIndexRecalculation." When your first plugin runs, make sure to set that field to true for all of your updates. In the same plugin, check to see if "DisableIndexRecalculation" is set to true: if so, set it to null (by removing it from the TargetEntity entirely) and stop executing the plugin. If it is null, do your index recalculation.
Because you are immediately removing the field from the TargetEntity if it is true the value will never be persisted to the database so there will be no performance penalty.

Resources