Hazelcast map listener by predicate on map key - listener

how can i set a listener to a map which has a complex object for key and I want to listen only on changes that happens to entries with specific key parameter value.
for example i have a code similar to this:
private class MyListener implements EntryAddedListener<>,
EntryUpdatedListener<KeyObject, Value> {
#Override
public void entryAdded(EntryEvent<KeyObject, Value> event) {
processCallbaclk(event);
}
#Override
public void entryUpdated(EntryEvent<KeyObject, Value> event) {
processCallback(event);
}
}
KeyObject{
Integer id,
String language
}
MyListener listener = new MyListener();
Predicate<KeyObject, Value> filter = equal(language, "EN");
imap.addEntryListener(listener, filter, true)
but when an entry with key that contains "EN" language is updated, my callback functions are not called. So I assume my predicate is wrong.

To access the key portion of a entry, you need to use "__key"
For example, "__key.field1 > 5".
See https://docs.hazelcast.org/docs/4.0/manual/html-single/index.html#querying-entry-keys-with-predicates for more details.

Related

how update jtable? realize setValueAt?

So i create method in MytableModel
public void removeRow(int row) {
if (getSelectedUser(row) != null) {
Authorisation.userMap.remove(getSelectedUser(row).getName());
list.remove(getSelectedUser(row));
System.out.println(list);
Registration.writeToFile();
}
fireTableRowsDeleted(row, row);
fireTableDataChanged();
}
then in outer class i create method
public void deleteFromTable() {
delete.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int index = usersTable.getSelectedRow();
model.removeRow(index);
}
});
}
and in other class invoked as
AdminFrame af = new AdminFrame(frame);
af.deleteFromTable();
Before that selected row deleted from ArrayList and table not update. WHATS WRONG?
When I delete a row from the table, so I delete the record from ArrayList,
All updates should be done to the TableModel. So you need to create a method like removeRow(....) in your TableModel. This method will do two things:
remove the data from your ArrayList
invoke fireTableRowsDeleted(...)
Or an easier way is to use the Row Table Model which does most of the code for you. All you need to do is extend the class to implement the getValueAt() and setValueAt() methods as demonstrated by the JButtonTableModel.java example.
Even if you decide not to use the RowTableModel it will give you an idea of how to implement the removeRow(...) method in your TableModel

p:autocomplete with POJO - how to get null value?

I have a simple problem. I suggest a list of objects to the user to choose from using p:autocomplete. At the same time the user can create new objects 'on the fly' by typing names that are not in the suggestion. The whole setup is very similar to the vanilla POJO showcase, but I'm posting the code here:
<p:autoComplete id="player" value="#{entry.player}"
completeMethod="{abean.complete}"
var="s1" itemLabel="#{s1.name}" itemValue="#{s1}"
converter="PlayerConverter">
<p:ajax event="itemSelect" listener="#{abean.playerSelected}"/>
</p:autoComplete>
The converter:
#FacesConverter(forClass=Player.class, value = "PlayerConverter")
public class PlayerConverter implements Converter
{
#Override
public Object getAsObject(FacesContext ctx, UIComponent cmp, String value)
{
Player player = null;
if ( !(value==null || value.trim().isEmpty()))
{
// Autocomplete did find a match, the appropriate
// player is returned
try
{
Integer id = Integer.valueOf(value);
player = TeamService.getInstance().getPlayer(id);
}
catch (NumberFormatException e)
{
// Autocomplete found no match, the entered String
// is given. Create an ad-hoc player as response
player = new Player();
player.setName(value);
player.setAdHoc(true);
}
}
return player;
}
#Override
public String getAsString(FacesContext ctx, UIComponent cmp, Object value)
{
String result = null;
if (value instanceof String)
{
result = (String) value;
}
else if (value instanceof Spieler)
{
Integer id = ((Spieler)value).getId();
result = (id == null) ? null : id.toString();
}
return result;
}
}
The problem I am facing seems simple: how do I coerce this construction into allowing the user to simply erase a value? When the user deletes the content of the input field, I would expect to get a call to PlayerConverter with a null or empty value, but that doesn't happen.
How do I get my hands on empty input?
I found a workaround. Use a converter and add a flag to the objects you are displaying in the p:autocomplete. Set the flag to 'false' in the converter's getAsString method. Set it to 'true' in the converter's getAsObject method. The latter is not called when the user emptied the autocomplete field. Therefore the flag remains 'false'. And that you can check in the action method of your command button.
I hope it helps someone.

Entity Framework, trigger mechanism for before update values

Is there anyway in EF to have before update values of object?
e.g. When entity object let's say User is saved, i would like to know for logging purpose before update User object values.
Thanks,
If you work with ObjectContext (edmx) you can subscribe to the SavingChanges event.
context.SavingChanges += context_SavingChanges;
This gives access to the original and current values when SaveChanges() is executed:
private void context_SavingChanges (object sender, EventArgs e)
{
ObjectContext context = sender as ObjectContext;
if (context != null)
{
foreach (ObjectStateEntry entry in context.ObjectStateManager
.GetObjectStateEntries(EntityState.Modified))
{
// TODO: do some logging with these values.
entry.OriginalValues;
entry.CurrentValues;
}
}
}
If you work with DbContext you can get to the event by
((IObjectContextAdapter)this).ObjectContext.SavingChanges

LinqToSql Calculated Field OnPropertyIDChanged

I have a partial class to extend one of my LinqToSql classes. In this partial class I have the following calculated field.
public bool IsInCluster
{
get
{
return Cluster != null;
}
}
In order for a grid column databound to this field to update automatically I have implemented the following partial method.
partial void OnClusterIDChanged()
{
SendPropertyChanged("IsInCluster");
}
However when I update the Cluster property as shown in the following code the OnClusterIDChanged method does not get called:
private void ExecCreateClusterCommand()
{
var cluster = new Cluster()
{
ID = Guid.NewGuid(),
MailService = AppState.CurrentMailService
};
App.DataContext.Clusters.InsertOnSubmit(cluster);
foreach (DeliveryPoint deliveryPoint in SelectedDeliveryPoints)
{
deliveryPoint.Cluster = cluster;
}
App.DataContext.SubmitChanges();
}
I have successfully used this technique with other non navigation properties related to calculated fields. Is there a way to make this work?
In your setter for Cluster, call OnClusterIDChanged, if the state has changed.
The only solution I could find for this was to create a public method in the DeliveryPoint class enabling me to call SendPropertyChanged for the required field (navigation property):
public void CallSendPropertyChanged(string propertyName)
{
SendPropertyChanged(propertyName);
}

log LINQ-to-SQL generated SQL to NLog

I would like to use NLog to output my LINQ to SQL generated SQL to the log file
e.g.
db.Log = Console.Out
reports the generated SQL to the console, http://www.bryanavery.co.uk/post/2009/03/06/Viewing-the-SQL-that-is-generated-from-LINQ-to-SQL.aspx
How can I get the log to log to NLog?
You just need a class to act as a TextWriter that LINQ to SQL needs to dispatch it via the method you want, e.g.
db.Log = new ActionTextWriter(s => logger.Debug(s));
Here is a little text writer I wrote that takes a delegate and dispatches to that so you use the code above. You would probably want to change this class so it took a logger, did some processing/splitting on the text and then dispatched it out to NLog.
class ActionTextWriter : TextWriter {
private Action<string> action;
public ActionTextWriter(Action<string> action) {
this.action = action;
}
public override void Write(char[] buffer, int index, int count) {
Write(new string(buffer, index, count));
}
public override void Write(string value) {
action.Invoke(value);
}
public override Encoding Encoding {
get { return System.Text.Encoding.Default; }
}
}