Pass parameters to cron job on hybris - cron

public class UserRetrievePerformable extends AbstractJobPerformable<UserRetrieveJobModel>
{
private static final Logger LOG = Logger.getLogger(UserRetrievePerformable.class);
#Autowired
TotalCustomerFacade totalCustomerFacade;
#Override
public PerformResult perform(UserRetrieveJobModel userRetrieveJobModel) {
LOG.info("**********************************");
LOG.info("Greeting from MyJobPerformable!!!");
LOG.info("**********************************");
return new PerformResult(CronJobResult.SUCCESS, CronJobStatus.FINISHED);
}
i want to make this job to get parameter as string.
how can i do this? according to spring docs, it cant be done. But there must be another way?
when i give string from backoffice or somewhere, job will give output that string?

I think you might confuse Job and CronJob. A Job is the task that should be done. A CronJob is a single execution of that task. So if you want to execute a Job, you have to create an instance of a CronJob. You define the task a CronJob should execute by selecting the right Job instance. When this CronJob is started, the perform method of the corresponding AbstractJobPerformable class is called with the CronJob as parameter.
So how do you create parameters for an execution? Create a subtype of the type CronJob and add all your needed parameters as attributes. When you then create the CronJob, set all attributes accordingly. In the perform method you can then access the attributes of that CronJob instance.
You create the CronJob like this:
HelloWorldCronJobModel cronJob = modelService.create(HelloWorldCronJobModel.class);
JobModel myJob = cronJobService.getJob("myJob");
cronJob.setJob(myJob);
// Add own attribute to Job
cronJob.setGreetedPerson("John Doe");
modelService.save(cronJob);
cronJobService.performCronJob(cronJob);
When your perform method is called, access the attribute:
#Override
public PerformResult perform(MyCronJobModel myCronJob) {
LOG.info("Hello " + myCronJob.getGreetedPerson());
return new PerformResult(CronJobResult.SUCCESS, CronJobStatus.FINISHED);
}

Related

change label value using value stored at session

i have two jsf pages (home.jsf and employees.jsf) ,
home page has a button that navigates to employees page,
while navigating i store value in session scope
at (Managed bean)
public void putSessionAL(ActionEvent actionEvent) {
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("key","value");
}
public String navigate() {
return "employees";
}
i want to change Label at employees viewObject from UIHints tab depending on value stored at session using the following groovy expression
adf.context.sessionScope.key
and changed trustMode to trusted but it fires the following exception
oracle.jbo.script.ExprScriptException: JBO-29114 ADFContext is not setup to process messages for this exception. Use the exception stack trace and error code to investigate the root cause of this exception. Root cause error code is JBO-25188. Error message parameters are {0=Employees.FirstName, 1=, 2=oracle.jbo.script.ExprSecurityException}
at oracle.jbo.script.ExprScriptException.throwException(ExprScriptException.java:316)
at oracle.jbo.script.ExprScriptException.throwExceptionWithExprDef(ExprScriptException.java:387)
at oracle.jbo.ExprEval.processScriptException(ExprEval.java:599)
at oracle.jbo.ExprEval.doEvaluate(ExprEval.java:697)
at oracle.jbo.ExprEval.evaluate(ExprEval.java:508)
at oracle.jbo.ExprEval.evaluate(ExprEval.java:487)
at oracle.jbo.common.NamedObjectImpl.resolvePropertyRaw(NamedObjectImpl.java:680)
at oracle.jbo.server.DefObject.resolvePropertyRaw(DefObject.java:366)
One way to do it at the VO UIHint attribute label level will be programmaticaly by doing as follow :
In your VO go to the java tab and add the RowImpl java class
In the VORowImpl Add the following function
public String getMySessionLabel() {
return (String)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("key");
}
In the Label add the following EL expression :
adf.object.getMySessionLabel()
This technique allow you more control than pure EL, if you want to do more than getting from session for example. In your case pure EL, as you did, should work as well. (Would need to check what is wrong with yours, maybe just missing the
#{adf.context.sessionScope.key}
If you attempt to get your label from a method in viewRowImpl. So this will be executed at least once for each row. I think this solution isn't fit for your case.
anyway ADF as a framework added strong policy and validations in EL in general and especially in version 12.2.x.
The solution for you case as following:
Create new class in model layer which extends oracle.jbo.script.ExprSecurityPolicy class
Override checkProperty method.
#Override
public boolean checkProperty(Object object, String string, Boolean b) {
if (object.getClass().getName().equals("oracle.adf.share.http.ServletADFContext") && string.equals("sessionScope")) {
return true;
}
return super.checkProperty(object, string, b);
}
Open adf-config.xml source and in startup tag set your class ExprSecurityPolicy property.
like:
<startup ExprSecurityPolicy="model.CustomExprSecurityPolicy">

Java Cucumber: How to get string parameter values from all the steps in a single scenario

I'm looking for a way to get all the parameters that are being passed in each step before entering the actual scenario for each scenario in my feature file.
Sample feature file:
Feature: Login action
Background:
When "{login url}" is open
Scenario: Registered user provides valid username and password
Given user enters username "{username}" and password "test password"
And user clicks on "btnLogin"
Then user is logged in
Parameters I want to get:
{login url}
{username}
password
btnLogin
What I tried so far:
I have tried using a common hook that will be automatically used by all of my scenarios:
public class ScenarioHook {
public ScenarioHook() {
}
#Before
public void setupScenario(Scenario scenario) throws InterruptedException {
//Here I am currently watching the {scenario} object and I can see all the steps
//but I still dont know where to get the passed parameter values.
}
#After
public void teardownScenario() throws InterruptedException {
}
}
UPDATE 1:
The reason why I want to do this is I want to manipulate the strings (if possible). e.g. all data enclosed in "{}" will be transformed to something else before entering the actual scenario.
You can use the #Transform annotation to change the value of the parameter to the step definition.
For this you will need to create a class which contains the logic of the string modification and will return the modified value.
public class StringTransformer extends Transformer<String>{
public String transform(String value) {
return "transformed "+value;
}
}
Next you need to include this class in your stepdefinition using the #Transform annotation in front of the method argument.
#When("^Login with (.*?)$")
public void helloHere(#Transform(StringTransformer.class) String userName)
{
System.out.println("TEXT --- " + userName);
}
This should give you the new transformed string. You can use this to create objects from your initial string. (Actually that is what it is used for)
Cucumber Sceanario class will not provide you this information as I don't think all steps are actually being loaded and parsed before starting executing the scenario but parsed one by one during the scenario execution. One option I can think of is to create a background step where you will manually include all parameters involved in the scenario. For example:
Feature: Login action
Background:
Given these parameters are using within this scenario: "{login url}", "{username}", "password", "btnLogin"
When "{login url}" is open

how do I configure my testng IReporter to accept parameters

IReporter is an interface that has a single void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) method. I would like to make the behavior of the reporter configurable so I can pass options to it when run on the commandline. The documentation explains how to pass parameters to a reporter on the commandline:
-reporter The extended configuration for a custom report listener. Similar to the -listener option, except that it allows the
configuration of JavaBeans-style properties on the reporter instance.
Example: -reporter
com.test.MyReporter:methodFilter=insert,enableFiltering=true You
can have as many occurences of this option, one for each reporter that
needs to be added.
So it seems I should be able to call testng with -reporter com.my.reporter:key1=value1,key2=value2
but WHERE do I get the values passed in.
I've looked at the XMLReporter provided by testng, and it has a private final XMLReporterConfig config = new XMLReporterConfig(); line, but I can't find out how the config is ever populated.
Magic, that's how it's done :-) It appears it looks for instance variables on your class that implements IReporter with the same name. It does need a stronger type than Object or def though it seems. Here's an example.
class MyReporter implements IReporter {
int foo; //<-- populated when instantiated
#Override
void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
println "foo = ${foo}"
}
}
And then to execute it:
testng ... -reporter 'full.path.to.MyReporter:foo=42'

Construct/Destruct custom gradle task

I'm totally new in gradle/groovy. I want to create custom task which will do some stuff. My first problem is how to do something when task finishes its job? Can I override doFirst/doLast closures? Maybe I can override some method which will do something at the beginning and at the end?
This is my "task"
import java.nio.file.Files
class MyCustomTask extends DefaultTask {
File buildDir
File tempDir
public MyCustomTask() {
super()
buildDir = project.getBuildFile().getParentFile()
tempDir = Files.createTempDirectory(buildDir.toPath(), "MyCustomTask").toFile()
}
#TaskAction
def build() {
println("test");
}
// I want to delete tempDir ater task execution
}
As it says on the official Gradle help forums (where someone asked this same question), you should either
wrap your build() code in a try...finally block
or write your custom task folder to the build directory so it gets cleaned when the user does gradle clean

Castle Windsor Factory implementation

I am using a Typed Factory supplied by Windsor Castle DI container. I am quite new to DI containers, so am after a bit of guidance with my solution.
Here is my implementation:
*updated registration & DB call
public interface IAgent { }
public class Agent : IAgent { }
public interface IAgentFactory
{
IAgent Create();
IAgent Create(int agentId);
IAgent Create(AgentDTO agentDTO);
}
class AgentFactory : IAgentFactory
{
public IAgent Create()
{
return InitNewEntity(new Agent());
}
public IAgent Create(int agentId, IDBContext dbContext) //Not happy with this, as it's a dependency that isn't factored out :(
{
return dbContext.GetAgent(agentId);
}
public IAgent Create(AgentDTO agentDTO)
{
Agent agent = InitNewEntity(new Agent());
agent.ParseDTO(agentDTO);
return agent;
}
private IAgent InitNewEntity(IAgent agent)
{
agent.Username = ""; /// + other fields to initialise
agent.DOB = DateTime.Now; /// etc.
return agent;
}
...
Container.AddFacility<TypedFactoryFacility>()
.Register( Component.For<IAgentFactory>()
.ImplementedBy<AgentFactory>());
which I'm using the following call to get some entities
IAgentFactory agentFactory = ViewModel.Container.Resolve<IAgentFactory>();
IAgent agent = agentFactory.Create(); //Creates new Agent entity
agent = agentFactory.Create(66, dbContext); //Looks up entity in database, don't like passing in a db context, another dependency
agent = agentFactory.Create(agentDTO); //Creates Agent entity from DTO object
I have several concerns about this.
There are 3 possible scenarios regarding creating a new Agent,
1: Create a new agent from scratch
2: Create an agent using an existing DTO (Data Transfer Object)
3: Create an agent with a call to the database.
I decided to put all this functionality into a factory, as it seems to fit the bill, however, I am not entirely sure if this is the correct or best way to accomplish this.
The question is, is it possible to leverage all 3 Create scenarios into the DI container Register statement and omit the concrete factory implementation altogether, so that I no longer have an AgentFactory class, but only the IAgentFactory interface.
Another question I have regarding this implementation, is it possible to do a call, such that if I request an Agent entity, Windsor will first make a call to the factory and return the entity created from the factory, rather than create the entity without referencing the factory.
ie.
IAgent agent = Container.Resolve<IAgent>(66);
With a Callstack:
Container -> AgentFactory -> Create(66) -> returns agent from factory.
Which Windsor will intercept and automatically use the factory to create the instance. However, I tried this and it didn't use the factory to create it, it just went ahead and created the Agent without referencing the factory.
I did have this, but it doesn't call the factory methods that I have defined.
Container.AddFacility<TypedFactoryFacility>()
.Register(Component.For<IAgentFactory>().AsFactory(),
Component.For<IAgent>().ImplementedBy<Agent>());
Advice is appreciated, thank you.
Typed Factory is designed to create "behaviour" instance, not "data" instance.
You do not register into the container a "model" component, but compenent to work w/ the model.
In other words you register into the container everything but the model.
You AgentFactory has to be registerted into the container, but that's not a "Typed Factory".
You may use TF for "late dependency" purpose.
I prefer be more decoupled w/ my design and also more "single responsability" oriented.
AgentFactory won't hide a repository (as per your Create from db) within the factory: I will pass a datareader as Create parameter instead.

Resources