public sealed class Parent : NativeActivity
{
public Parent()
{
Childrens = new Collection<Activity>();
Variables = new Collection<Variable>();
_currentActivityIndex = new Variable<int>();
CurrentCustomTypeInstance= new Variable<MyCustomType>();
}
[Browsable(false)]
public Collection<Activity> Childrens { get; set; }
protected override void Execute(NativeActivityContext context)
{
_currentActivityIndex.Set(context, 0);
context.ScheduleActivity(FirstActivity, Callback);
}
private void Callback(NativeActivityContext context, ActivityInstance completedInstance, MyCustomType customTypeInstance)
{
CurrentCustomTypeInstance.Set(context, customTypeInstance);
ScheduleNextChildren(context, completedInstance);
}
private void ScheduleNextChildren(NativeActivityContext context, ActivityInstance completedInstance)
{
int nextActivityIndex = _currentActivityIndex.Get(context) + 1;
if (nextActivityIndex >= Childrens.Count)
return;
Activity nextActivity = Childrens[nextActivityIndex];
IFoo nextActivityAsIFoo = nextActivity as IFoo;
if (nextActivityAsIFoo != null)
{
var currentCustomTypeInstance = CurrentCustomTypeInstance.Get(context);
// HERE IS MY EXCEPTION
nextActivityAsIFoo.FooField.Set(context, currentCustomTypeInstance);
}
context.ScheduleActivity(nextActivity);
_currentActivityIndex.Set(context, nextActivityIndex);
}
}
And in register metadata:
metadata.SetChildrenCollection(Childrens);
I've already read http://msmvps.com/blogs/theproblemsolver/archive/2011/04/05/scheduling-child-activities-with-input-parameters.aspx but in my case, parent does not know the child activity
Edit
Similar to: Activity cannot set a Variable defined within its scope?
Activity '1.1: Parent' cannot access this variable because it is declared at the scope
of activity '1.1: Parent'. An activity can only access its own implementation variables.
But in my case, I don't need to get the return value, so, hope to be easier. Just need to pass FooField implicitly instead of leting it to flow author.
I need to do it implicitly! If it doesn't work at all, I will go with NativeActivityContext Properties
Got it! Looking at the Maurice's blog I got inspired to do it
Workflow(very very simple!)
static void Main(string[] args)
{
Parent parent = new Parent();
parent.Childrens.Add(new FooWriter());
parent.Childrens.Add(new FooFormater());
parent.Childrens.Add(new FooWriter());
WorkflowInvoker.Invoke(parent);
Console.Read();
}
Output
What's the Foo name?
Implicit FTW!
Im a custom Foo Handler, my Foo name is: Implicit FTW!
Im a custom Foo Handler, my Foo name is: IMPLICIT FTW!
Implementation
using System;
using System.Activities;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace WorkflowConsoleApplication2
{
// Parent class that creates a Foo and passes it to their childrens
public sealed class Parent : NativeActivity
{
private Variable<int> _currentActivityIndex;
private Variable<Foo> _currentFoo;
public Parent()
{
Childrens = new Collection<Activity>();
_executionChildrens = new Collection<Tuple<Activity, ActivityAction<Foo>>>();
_currentActivityIndex = new Variable<int>();
_currentFoo = new Variable<Foo>();
}
[Browsable(false)]
public Collection<Activity> Childrens { get; set; }
private Collection<Tuple<Activity, ActivityAction<Foo>>> _executionChildrens;
protected override void Execute(NativeActivityContext context)
{
Console.WriteLine("What's the Foo name?");
_currentFoo.Set(context, new Foo { Name = Console.ReadLine() });
_currentActivityIndex.Set(context, 0);
ScheduleNextChildren(context, null);
}
private void ScheduleNextChildren(NativeActivityContext context, ActivityInstance completedInstance)
{
int currentActivityIndex = _currentActivityIndex.Get(context);
if (currentActivityIndex >= Childrens.Count)
return;
Tuple<Activity, ActivityAction<Foo>> nextActivity = _executionChildrens[currentActivityIndex];
if (IsFooHandler(nextActivity))
{
context.ScheduleAction(nextActivity.Item2, _currentFoo.Get(context),
ScheduleNextChildren);
}
else
{
context.ScheduleActivity(nextActivity.Item1,
ScheduleNextChildren);
}
_currentActivityIndex.Set(context, currentActivityIndex + 1);
}
protected override void CacheMetadata(NativeActivityMetadata metadata)
{
metadata.SetArgumentsCollection(metadata.GetArgumentsWithReflection());
metadata.AddImplementationVariable(_currentActivityIndex);
metadata.AddImplementationVariable(_currentFoo);
RegisterChildrens(metadata, Childrens);
// remove "base.Cachemetadata" to "Childrens collection" doesn't become a child again }
public void RegisterChildrens(NativeActivityMetadata metadata, IEnumerable<Activity> childrens)
{
foreach (Activity child in childrens)
{
IFooHandler childAsIFooHandler = child as IFooHandler;
if (childAsIFooHandler != null)
{
ActivityAction<Foo> childsWrapperAction = new ActivityAction<Foo>();
var activityToActionBinderArgument = new DelegateInArgument<Foo>();
childsWrapperAction.Argument = activityToActionBinderArgument;
childAsIFooHandler.Foo = activityToActionBinderArgument;
childsWrapperAction.Handler = child;
metadata.AddDelegate(childsWrapperAction);
_executionChildrens.Add(new Tuple<Activity, ActivityAction<Foo>>(child, childsWrapperAction));
}
else
{
metadata.AddChild(child);
_executionChildrens.Add(new Tuple<Activity, ActivityAction<Foo>>(child, null));
}
}
}
public static bool IsFooHandler(Tuple<Activity, ActivityAction<Foo>> activity)
{
return activity.Item2 != null;
}
}
// samples of Foo handlers
public class FooWriter : CodeActivity, IFooHandler
{
/// When FooWriter is direct child of "Parent" this argument is passed implicitly
public InArgument<Foo> Foo { get; set; }
protected override void Execute(CodeActivityContext context)
{
Console.WriteLine("Im a custom Foo Handler, my Foo name is: {0}", Foo.Get(context).Name);
}
}
public class FooFormater : CodeActivity, IFooHandler
{
public InArgument<Foo> Foo { get; set; }
protected override void Execute(CodeActivityContext context)
{
Foo foo = Foo.Get(context);
foo.Name = foo.Name.ToUpper();
}
}
// sample classes
public class Foo
{
public string Name { get; set; }
}
public interface IFooHandler
{
InArgument<Foo> Foo { get; set; }
}
}
If anyone know how to do it in a better way please fell free to tell me. I will also need to pass the values to nested activities
Related
In a JavaFX TreeView I'm using 'custom' classes which extend TreeItem. This makes me able to edit the items in the TreeView (I can double click them and edit the contents when running the application) but I can't seem to be able to set the .setOnEditCommit() method properly. I was hoping it'd work similar as the function in a tableview but I didn't have any luck yet.
This is my code in my controller in which I try to set the setOnEditCommit() method. In my TreeView called 'trvDivisies' I display football team divisions / competitions and one level lower I display all the teams that are in a certain division.
private void setUpTreeView() {
trvDivisies.setEditable(true);
trvDivisies.setShowRoot(false);
TreeItem<String> root = new TreeItem<>();
for (Divisie d : divisies) {
TreeItem<String> divisieTreeItem = d;
divisieTreeItem.valueProperty().set(d.getNaam());
for (VoetbalTeam vt : d.getVoetbalTeams()) {
TreeItem<String> voetbalTeamTreeItem = vt;
voetbalTeamTreeItem.valueProperty().setValue(vt.getTeamNaam());
divisieTreeItem.getChildren().add(voetbalTeamTreeItem);
}
root.getChildren().add(divisieTreeItem);
}
trvDivisies.setRoot(root);
trvDivisies.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
#Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
System.out.println(newValue);
}
});
trvDivisies.setCellFactory(TextFieldTreeCell.forTreeView());
// I get an error at the following line when compiling
trvDivisies.setOnEditCommit((TreeView.EditEvent p) -> {
TreeItem<String> selectedItem = p.getTreeItem();
if (selectedItem instanceof Divisie) {
updateDivisie((Divisie)selectedItem);
} else if (selectedItem instanceof VoetbalTeam) {
updateTeam((VoetbalTeam)selectedItem);
}
});
}
This is what my 'custom' classes look like.
public class Divisie extends TreeItem<String> {
private static int idCount = 0;
private int id;
private String naam;
private List<VoetbalTeam> voetbalTeams;
public int getId() {
return id;
}
public String getNaam() {
return naam;
}
public List<VoetbalTeam> getVoetbalTeams() {
return voetbalTeams;
}
public Divisie(int id, String naam) {
super(naam);
this.id = id;
this.naam = naam;
}
public Divisie(String naam) {
this.id = ++idCount;
this.naam = naam;
}
public void addTeam(VoetbalTeam toBeAdded) {
if (voetbalTeams == null) {
voetbalTeams = new LinkedList<>();
}
voetbalTeams.add(toBeAdded);
}
#Override
public String toString() {
return this.naam;
}
}
Second 'lower level' class
public class VoetbalTeam extends TreeItem<String> {
private static int idCount = 0;
private int id;
private String teamNaam;
private List<Speler> spelers;
public int getId() {
return id;
}
public String getTeamNaam() {
return teamNaam;
}
public List<Speler> getSpelers() {
return this.spelers;
}
public VoetbalTeam(int id, String teamNaam) {
super(teamNaam);
this.id = id;
this.teamNaam = teamNaam;
}
public VoetbalTeam(String teamNaam) {
super(teamNaam);
this.id = ++idCount;
this.teamNaam = teamNaam;
}
public void addSpeler(Speler nieuweSpeler) {
if (spelers == null) {
spelers = new LinkedList<>();
}
this.spelers.add(nieuweSpeler);
}
#Override
public String toString() {
return this.teamNaam;
}
}
When trying to run the application WITH the .setOnEditCommit() method I get an error saying:
Error:(97, 37) java: incompatible types: incompatible parameter types in lambda expression
I was hoping you guys can tell me what I need to change my TreeView.EditEvent lambda to or help me find an easier solution.
For a TreeView<T>, the signature of setOnEditCommit is
void setOnEditCommit(EventHandler<TreeView.EditEvent<T>> value)
Since you have (apparently) a TreeView<String>, you need
trvDivisies.setOnEditCommit((TreeView.EditEvent<String> p) -> {
// ...
});
Or, of course, you can just let the compiler do the work for you:
trvDivisies.setOnEditCommit(p -> {
// ...
});
Why is it not possible to pass the arguments to CodeActivity via WorkflowInvoker's input dictionary, if the activities are within a Sequence? The WorkflowInvoker.Invoke(sequence, dict) method throws the following exception:
Additional information: The values provided for the root activity's arguments did not satisfy the root activity's requirements:
'Sequence': The following keys from the input dictionary do not map to arguments and must be removed: Arg. Please note that argument names are case sensitive.
class Program
{
static void Main(string[] args)
{
var sequence = new Sequence();
var start = new Start();
var end = new End();
sequence.Activities.Add(start);
sequence.Activities.Add(end);
var dict = new Dictionary();
dict["Arg"] = "Debug text.";
WorkflowInvoker.Invoke(sequence, dict);
}
}
public class Start : CodeActivity
{
public InArgument Arg { get; set; }
protected override void Execute(CodeActivityContext context)
{
Debug.WriteLine(Arg.Get(context));
}
}
public class End : CodeActivity
{
public InArgument Arg { get; set; }
protected override void Execute(CodeActivityContext context)
{
Debug.WriteLine(Arg.Get(context));
}
}
// ************** Second example with custom sequence *************************
class Program
{
static void Main(string[] args)
{
var seq = new MySequence();
seq.Activities.Add(new Last());
seq.Activities.Add(new First());
var dict = new Dictionary();
dict["Arg"] = "Text";
WorkflowInvoker.Invoke(seq, dict);
}
}
public class First : CodeActivity
{
public InArgument Arg { get; set; }
protected override void Execute(CodeActivityContext context)
{
var val = Arg.Get(context);
}
}
public class Last : CodeActivity
{
public InArgument Arg { get; set; }
protected override void Execute(CodeActivityContext context)
{
var val = Arg.Get(context);
}
}
public class MySequence : NativeActivity
{
public InArgument Arg { get; set; }
public Collection Activities = new Collection();
protected override void CacheMetadata(NativeActivityMetadata metadata)
{
base.CacheMetadata(metadata);
metadata.SetChildrenCollection(Activities);
}
protected override void Execute(NativeActivityContext context)
{
foreach (var activity in Activities)
context.ScheduleActivity(activity);
}
}
The code activities take their arguments from the container they are in not the input dictionary. The container needs to have an in argument matching the one in the dictionary.
Sequences don't accept arguments so you wrap them in an Activity.
An Activity constructed as below is a worklfow
public class MyCodeWorkflow : Activity
{
public InArgument<string> inMSG { get; set; }
public OutArgument<string> outMSG { get; set; }
public MyCodeWorkflow()
{
this.Implementation = () => new Sequence {
Activities =
{
new WriteLine
{
Text=new InArgument<string>((activityContext)=>this.inMSG.Get(activityContext))
},
new Assign<string>
{
To=new ArgumentReference<string>("outMSG"),
Value=new InArgument<string>
(
(activityContext)=>this.inMSG.Get(activityContext)
)
}
}
};
}
}
//host
static void Main(string[] args)
{
IDictionary<string, object> input = new Dictionary<string, object>();
input.Add("inMSG","hello");
IDictionary<string, object> output = new Dictionary<string, object>();
MyCodeWorkflow activity = new MyCodeWorkflow();
output = WorkflowInvoker.Invoke(activity,input);
Console.WriteLine(output["outMSG"]);
}
The code above was taken from http://xhinker.com/post/WF4Authoring-WF4-using-imperative-code%28II%29.aspx
I have built a custom component button, but somehow the action is not invoked. When debugging the getAction-Method within the component and invoking the supplied MethodeExpression the Bean-Method is called as expected. But due to some reason, the Expression is not invoked when pressing the button in the browser.
Is there some kind of additional Interface necessary to pass the action to the embedded button-component?
Any help is very appreciated since I am stuck at this issue for some days now
MyClass:
public class MyClass extends UIPanel implements SystemEventListener
{
private UIForm form;
private HtmlCommandButton buttonOk;
public MyClass()
{
FacesContext context = getFacesContext();
UIViewRoot root = context.getViewRoot();
root.subscribeToViewEvent(PostAddToViewEvent.class, this);
}
#Override
public void processEvent(SystemEvent event)
{
this.form = new UIForm();
this.buttonOk = new HtmlCommandButton();
this.buttonOk.setId("okButtonId");
this.buttonOk.setActionExpression(getAction());
this.buttonOk.setValue("OK");
this.form.getChildren().add(this.buttonOk);
getChildren().add(this.form);
}
private enum PropertyKeys
{
action, text, titel
}
public MethodExpression getAction()
{
return (MethodExpression) getStateHelper().eval(PropertyKeys.action);
}
public void setAction(MethodExpression actionExpression)
{
getStateHelper().put(PropertyKeys.action, actionExpression);
}
public String getText()
{
return (String) getStateHelper().eval(PropertyKeys.text);
}
public void setText(String text)
{
getStateHelper().put(PropertyKeys.text, text);
}
public String getTitel()
{
return (String) getStateHelper().eval(PropertyKeys.titel);
}
public void setTitel(String titel)
{
getStateHelper().put(PropertyKeys.titel, titel);
}
#Override
public void encodeAll(FacesContext context) throws IOException
{
ResponseWriter writer = context.getResponseWriter();
writer.startElement(HTML.DIV_ELEM, this);
writer.writeText(getText(), null);
this.form.encodeAll(context);
writer.endElement(HTML.DIV_ELEM);
}
#Override
public void encodeChildren(FacesContext context) throws IOException
{
}
#Override
public boolean isListenerForSource(Object source)
{
return (source instanceof MyClass);
}
}
MyClassHandler:
public class MyClassHandler extends ComponentHandler
{
public MyClassHandler(ComponentConfig config)
{
super(config);
}
#SuppressWarnings("rawtypes")
#Override
protected MetaRuleset createMetaRuleset(Class type)
{
return super.createMetaRuleset(type).addRule(new MethodRule("action", String.class, new Class[] { ActionEvent.class }));
}
}
myView Method:
...
public String myMethod()
{
System.err.println("myMethod");
return "/some/path/yadayada.xhtml";
}
...
MyView.xhtml
<myTag action="#{myView.myMethod}" id="id1" titel="bla" text="bleh" />
Exdending UICommand is enough, since you only want one action to be executed.
You have to provide two additional MethodExpressions via the tag-attributes and within the decode-method you can check which button has been pressed and redirect the particular MethodExpression to the standard-action provided by UICommand. This way, you dont have to worry about the legacy-interface ActionSource, or how Events are broadcasted.
public void decode(FacesContext contex)
{
Map<String,String> map = context.getExternalContext.getRequestParameterMap();
// your rendered buttons need a name you check for
final boolean okPressed = map.containsKey( getClientId + ":ok" );
final boolean cancelPressed = map.containsKey( getClientId + ":cancel" );
if(okPressed || cancelPressed)
{
MethodExpression exp = null;
if(okPressed)
{
exp = getActionOk();
}
else
{
exp = getActionCancel();
}
// redirect to standard action
setActionExpression(exp);
queueEvent(new ActionEvent(this));
}
}
In order to make use of of this you need two attributes (actionOk and actionCancel) which use Method Expressions (setter and getter). Those have to be configured by a ComponentHandler as you did for the action-attribute.
I was reading a post at VS 2008, ASP.NET: Generate Local Resources.
Mehdi Golchin showed us a beautiful job of StateManagedCollection.
However I was wondered about using multiple classes of IStateManager in one StateManagedCollection.
As you can see below:
public class MenuItemCollection : StateManagedCollection
{
public MenuItem this[int index]
{
get { return (MenuItem)((IList)this)[index]; }
}
public int Add(MenuItem item)
{
return ((IList)this).Add(item);
}
public void Remove(MenuItem item)
{
((IList)this).Remove(item);
}
// Write Insert and RemoveAt methods
protected override void SetDirtyObject(object o)
{
((MenuItem)o).SetDirty();
}
}
This MenuItemCollection class can have only one child class("MenuItem").
If I want to use another class as well as MenuItem class, for example MenuItem2 class, how do I have to write the codes?
Anyone can help me?
Thanks in advance.
Write a generic version - for example,
public class GenericStateManagedCollection<T> : StateManagedCollection
where T: IStateManager, new()
{
public T this[int index]
{
get { return (T)((IList)this)[index]; }
}
public int Add(T item)
{
return ((IList)this).Add(item);
}
public void Remove(T item)
{
((IList)this).Remove(item);
}
// Write Insert and RemoveAt methods
protected override void SetDirtyObject(object o)
{
((T)o).SetDirty();
}
protected override object CreateKnownType(int index)
{
return Activator.CreateInstance<T>();
}
protected override Type[] GetKnownTypes()
{
return new Type[] { typeof(T) };
}
}
And use it as
public class MenuItemCollection : GenericStateManagedCollection<MenuItem> { }
public class XyzItemCollection : GenericStateManagedCollection<XyzItem> { }
EDIT:
I have most probably mis-understood your question! Assuming now that you want to put two different type of objects into the StateManagedCollection. From usage perspective, it doesn't make sense to have objects of completely unrelated types into the collection - you need to have some base class. For example, consider DataControlFieldCollection which holds instances of (abstract) type 'DataControField. BoundField, ButtonField etc inherits fromDataControField`.
So you need to go via similar route - for example,
public class MenuItemBase : IStateManager
{
// Use implementation from link you quoted (Mehdi Golchin's answer)
...
}
public class MenuItem : MenuItemBase
{
...
}
public class MenuItem2 : MenuItemBase
{
...
}
public class MenuItemCollection : StateManagedCollection
{
public MenuItemBase this[int index]
{
get { return (MenuItemBase)((IList)this)[index]; }
}
public int Add(MenuItemBaseitem)
{
return ((IList)this).Add(item);
}
public void Remove(MenuItemBaseitem)
{
((IList)this).Remove(item);
}
// Write Insert and RemoveAt methods
protected override void SetDirtyObject(object o)
{
((MenuItemBase)o).SetDirty();
}
// important to override CreateKnownType and GetKnownTypes
private static readonly Type[] _knownTypes = new Type[] {typeof(MenuItem), typeof(MenuItem2) }
protected override Type[] GetKnownTypes()
{
return _knownTypes;
}
protected override object CreateKnownType(int index)
{
switch (index)
{
case 0:
return new MenuItem();
case 1:
return new MenuItem2();
default:
throw new Exception("Invalid Index");
}
}
}
Note: Untested code
I want to update a log file(txt) everytime when methods in a an interface class are called?
Is there any way to do this other than writing code in every method to create log?
Here's my 30 mins. you'll have to implement the logging code somewhere so you have to create another abstraction for your code. thus an abstract class is needed. i think. this is very quick and dirty.
public interface IService<T>
{
List<T> GetAll();
bool Add(T obj);
}
then you'll need the abstract class where you'll need to implement your logging routine
public abstract class Service<T> : IService<T>
{
private void log()
{
/// TODO : do log routine here
}
public bool Add(T obj)
{
try
{
log();
return AddWithLogging(obj);
}
finally
{
log();
}
}
public List<T> GetAll()
{
try
{
log();
return GetAllWithLog();
}
finally
{
log();
}
}
protected abstract List<T> GetAllWithLog();
protected abstract bool AddWithLogging(T obj);
}
as for your concrete classes
public class EmployeeService : Service<Employee>
{
protected override List<Employee> GetAllWithLog()
{
return new List<Employee>() { new Employee() { Id = 0, Name = "test" } };
}
protected override bool AddWithLogging(Employee obj)
{
/// TODO : do add logic here
return true;
}
}
public class CompanyService : Service<Company>
{
protected override List<Company> GetAllWithLog()
{
return new List<Company>() { new Company() { Id = 0, Name = "test" } };
}
protected override bool AddWithLogging(Company obj)
{
/// TODO : do add logic here
return true;
}
}
public class Employee
{
public int Id {get;set;}
public string Name { get; set;}
}
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
}
then on your implementation you can just..
static void Main(string[] args)
{
IService<Employee> employee = new EmployeeService();
List<Employee> employees = employee.GetAll();
foreach (var item in employees)
{
Console.WriteLine(item.Name);
}
IService<Company> company = new CompanyService();
List<Company> companies = company.GetAll();
foreach (var item in companies)
{
Console.WriteLine(item.Name);
}
Console.ReadLine();
}
hope this helps!
I think you would have to use Aspect Oriented Programming to achieve that. Read http://www.sharpcrafters.com/aop.net
I think you meant class (instead of interface)
Two options I can think of:
Implementing INotifyPropertyChanged which is in lines of writing code in every method
or
to adopt on of the AOP frameworks in the article http://www.codeproject.com/KB/cs/AOP_Frameworks_Rating.aspx if that is not a major leap