GlobalSession using struts2 - struts2

I want to maintain a list of objects in global session using struts so that if there are multiple users editing the list each user should see the updated list.
#Action(value = "/listAuthoredFiles", results = { #Result(name = "success", type = "json", params = {
"excludeProperties", "gridModel.*\\.*Map, gridModel.*\\.*List" }) })
public String execute() throws Exception {
log.debug("Page: " + getPage() + " Rows: " + getRows() + " Sorting Order: " + getSord() + " Index Row: " + getSidx());
List<FilterParameter> filters = getFilterParameters();
List<UserVO> gridModel = new ArrayList<UserVO>();
if (!CommonUtil.isEmpty(filters)) {
gridModel = searchService.findByCriteria(filters);
} else {
log.debug("filters is empty");
}
ActionContext.getContext().getSession().put(SearchConstants.LIST_AUTHORED_FILES, gridModel);
In another action class i am accessing the list :
if (MapUtils.isNotEmpty(ActionContext.getContext().getSession())
&& ActionContext.getContext().getSession().containsKey(SearchConstants.LIST_AUTHORED_FILES)) {
List<UserVO> gridModel = (List<UserVO>) ActionContext.getContext().getSession()
.get(SearchConstants.LIST_AUTHORED_FILES);
but the problem here is if i put in session and if there are multiple users accessing this list then there would be possibility users won't be seeing the updated list as i am storing the list in user's session not at global session level. So is there any way to store list in global session level.

Related

How can I access the node.depth attribute in the (Grails plugin) <RichUI:treeview> tag?

The plugin says that you can use 'node.depth' from the tag to determine what label level has been clicked (country or province). I can't seem to access node.depth from the RichUI:treeview tag. My code works fine when the value of 1 or 2 is hard coded into the onLabelClick. But when I specify node.depth as the parameter, nothing gets passed to the javascript. How can I access node.depth? My alert says that "level is undefined"
<richui:treeView id="tree" xml="${data}"
onLabelClick="treeClickHandler(node.depth, id)" showRoot="false"/>
function treeClickHandler(level, id){
alert("level is " + level + " and id is " + id);
if (level == 1){
postForCountryIdeas(id);
}
else{
postForProvControls(id);
}
}
def index() {
def countryList = Country.list()
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
def writer2 = new StringWriter()
def xml2 = new MarkupBuilder(writer2)
xml2.mkp.xmlDeclaration(version: "1.0", encoding: "utf-8")
xml2.countrys {
countryList.each{item->
xml2.country(name:"${item.name}", id: item.id){
item.provinces.each{ prov->
province(name:"${prov.name}", id: prov.id)
}
}
}
}
[data: writer2.toString()]
}
The solution was to use node.node.depth instead of node.depth as per the docs.

Retrieve TextAd using Adwords Api

I'm trying to retrieve TextAd (Headline,Desc1,Desc2,Display URL and Destination URL) and i failed.
This is my Code on retrieving Text ad it returns Null result
TextAd text = new TextAd();
System.out.println("Headline:"+text.getHeadline());
Syso... etc.
I want to retrieve All details of TextAd , I'm using java.
This is my the code for adding TextAd
public static void runExample(
AdWordsServices adWordsServices, AdWordsSession session, long adGroupId) throws Exception {
// Get the AdGroupAdService.
AdGroupAdServiceInterface adGroupAdService =
adWordsServices.get(session, AdGroupAdServiceInterface.class);
// Create text ads.
TextAd textAd1 = new TextAd();
textAd1.setHeadline("Luxury Cruise to Mars");
textAd1.setDescription1("Visit the Red Planet in style.");
textAd1.setDescription2("Low-gravity fun for everyone!");
textAd1.setDisplayUrl("www.example.com");
textAd1.setFinalUrls(new String[] {"http://www.example.com/1"});
TextAd textAd2 = new TextAd();
textAd2.setHeadline("Luxury Cruise to Mars");
textAd2.setDescription1("Enjoy your stay at Red Planet.");
textAd2.setDescription2("Buy your tickets now!");
textAd2.setDisplayUrl("www.example.com");
textAd2.setFinalUrls(new String[] {"http://www.example.com/2"});
// Create ad group ad.
AdGroupAd textAdGroupAd1 = new AdGroupAd();
textAdGroupAd1.setAdGroupId(adGroupId);
textAdGroupAd1.setAd(textAd1);
// You can optionally provide these field(s).
textAdGroupAd1.setStatus(AdGroupAdStatus.PAUSED);
AdGroupAd textAdGroupAd2 = new AdGroupAd();
textAdGroupAd2.setAdGroupId(adGroupId);
textAdGroupAd2.setAd(textAd2);
// Create operations.
AdGroupAdOperation textAdGroupAdOperation1 = new AdGroupAdOperation();
textAdGroupAdOperation1.setOperand(textAdGroupAd1);
textAdGroupAdOperation1.setOperator(Operator.ADD);
AdGroupAdOperation textAdGroupAdOperation2 = new AdGroupAdOperation();
textAdGroupAdOperation2.setOperand(textAdGroupAd2);
textAdGroupAdOperation2.setOperator(Operator.ADD);
AdGroupAdOperation[] operations =
new AdGroupAdOperation[] {textAdGroupAdOperation1, textAdGroupAdOperation2};
// Add ads.
AdGroupAdReturnValue result = adGroupAdService.mutate(operations);
// Display ads.
for (AdGroupAd adGroupAdResult : result.getValue()) {
System.out.println("Ad with id \"" + adGroupAdResult.getAd().getId() + "\"" + " and type \""
+ adGroupAdResult.getAd().getAdType() + "\" was added.");
}
}
How can i retrieve Those values from adwords.
this is my selector for retrieving the data from adword
SelectorBuilder builder = new SelectorBuilder();
Selector selector = builder
.fields(AdGroupAdField.Id, AdGroupAdField.AdGroupId, AdGroupAdField.Status,
AdGroupAdField.Description1,AdGroupAdField.Description2,AdGroupAdField.Headline)
.orderAscBy(AdGroupAdField.Id)
.offset(offset)
.limit(PAGE_SIZE)
.equals(AdGroupAdField.AdGroupId, adGroupId.toString())
.in(AdGroupAdField.Status, "ENABLED", "PAUSED", "DISABLED")
.equals("AdType", "TEXT_AD")
.build();
Typecast adGroupAd.getAd() to TextAd then you can get headline and other methods.
TextAd textAd = (TextAd)adGroupAd.getAd();
textAd.getHeadline();

Crawler4j With Grails App

I am making a crawler application in Groovy on Grails. I am using Crawler4j and following this tutorial.
I created a new grails project
Put the BasicCrawlController.groovy file in controllers->package
Did not create any view because I expected on doing run-app, my crawled data would appear in my crawlStorageFolder (please correct me if my understanding is flawed)
After that I just ran the application by doing run-app but I didn't see any crawling data anywhere.
Am I right in expecting some file to be created at the crawlStorageFolder location that I have given as C:/crawl/crawler4jStorage?
Do I need to create any view for this?
If I want to invoke this crawler controller from some other view on click of a submit button of a form, can I just write <g:form name="submitWebsite" url="[controller:'BasicCrawlController ']">?
I asked this because I do not have any method in this controller, so is it the right way to invoke this controller?
My code is as follows:
//All necessary imports
public class BasicCrawlController {
static main(args) throws Exception {
String crawlStorageFolder = "C:/crawl/crawler4jStorage";
int numberOfCrawlers = 1;
//int maxDepthOfCrawling = -1; default
CrawlConfig config = new CrawlConfig();
config.setCrawlStorageFolder(crawlStorageFolder);
config.setPolitenessDelay(1000);
config.setMaxPagesToFetch(100);
config.setResumableCrawling(false);
PageFetcher pageFetcher = new PageFetcher(config);
RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
CrawlController controller = new CrawlController(config, pageFetcher, robotstxtServer);
controller.addSeed("http://en.wikipedia.org/wiki/Web_crawler")
controller.start(BasicCrawler.class, 1);
}
}
class BasicCrawler extends WebCrawler {
final static Pattern FILTERS = Pattern
.compile(".*(\\.(css|js|bmp|gif|jpe?g"+ "|png|tiff?|mid|mp2|mp3|mp4" +
"|wav|avi|mov|mpeg|ram|m4v|pdf" +"|rm|smil|wmv|swf|wma|zip|rar|gz))\$")
/**
* You should implement this function to specify whether the given url
* should be crawled or not (based on your crawling logic).
*/
#Override
boolean shouldVisit(WebURL url) {
String href = url.getURL().toLowerCase()
!FILTERS.matcher(href).matches() && href.startsWith("http://en.wikipedia.org/wiki/Web_crawler/")
}
/**
* This function is called when a page is fetched and ready to be processed
* by your program.
*/
#Override
void visit(Page page) {
int docid = page.getWebURL().getDocid()
String url = page.getWebURL().getURL()
String domain = page.getWebURL().getDomain()
String path = page.getWebURL().getPath()
String subDomain = page.getWebURL().getSubDomain()
String parentUrl = page.getWebURL().getParentUrl()
String anchor = page.getWebURL().getAnchor()
println("Docid: ${docid} ")
println("URL: ${url} ")
println("Domain: '${domain}'")
println("Sub-domain: ' ${subDomain}'")
println("Path: '${path}'")
println("Parent page:${parentUrl} ")
println("Anchor text: ${anchor} " )
if (page.getParseData() instanceof HtmlParseData) {
HtmlParseData htmlParseData = (HtmlParseData) page.getParseData()
String text = htmlParseData.getText()
String html = htmlParseData.getHtml()
List<WebURL> links = htmlParseData.getOutgoingUrls()
println("Text length: " + text.length())
println("Html length: " + html.length())
println("Number of outgoing links: " + links.size())
}
Header[] responseHeaders = page.getFetchResponseHeaders()
if (responseHeaders != null) {
println("Response headers:")
for (Header header : responseHeaders) {
println("\t ${header.getName()} : ${header.getValue()}")
}
}
println("=============")
}
}
I'll try to translate your code into a Grails standard.
Use this under grails-app/controller
class BasicCrawlController {
def index() {
String crawlStorageFolder = "C:/crawl/crawler4jStorage";
int numberOfCrawlers = 1;
//int maxDepthOfCrawling = -1; default
CrawlConfig crawlConfig = new CrawlConfig();
crawlConfig.setCrawlStorageFolder(crawlStorageFolder);
crawlConfig.setPolitenessDelay(1000);
crawlConfig.setMaxPagesToFetch(100);
crawlConfig.setResumableCrawling(false);
PageFetcher pageFetcher = new PageFetcher(crawlConfig);
RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
CrawlController controller = new CrawlController(crawlConfig, pageFetcher, robotstxtServer);
controller.addSeed("http://en.wikipedia.org/wiki/Web_crawler")
controller.start(BasicCrawler.class, 1);
render "done crawling"
}
}
Use this under src/groovy
class BasicCrawler extends WebCrawler {
final static Pattern FILTERS = Pattern
.compile(".*(\\.(css|js|bmp|gif|jpe?g"+ "|png|tiff?|mid|mp2|mp3|mp4" +
"|wav|avi|mov|mpeg|ram|m4v|pdf" +"|rm|smil|wmv|swf|wma|zip|rar|gz))\$")
/**
* You should implement this function to specify whether the given url
* should be crawled or not (based on your crawling logic).
*/
#Override
boolean shouldVisit(WebURL url) {
String href = url.getURL().toLowerCase()
!FILTERS.matcher(href).matches() && href.startsWith("http://en.wikipedia.org/wiki/Web_crawler/")
}
/**
* This function is called when a page is fetched and ready to be processed
* by your program.
*/
#Override
void visit(Page page) {
int docid = page.getWebURL().getDocid()
String url = page.getWebURL().getURL()
String domain = page.getWebURL().getDomain()
String path = page.getWebURL().getPath()
String subDomain = page.getWebURL().getSubDomain()
String parentUrl = page.getWebURL().getParentUrl()
String anchor = page.getWebURL().getAnchor()
println("Docid: ${docid} ")
println("URL: ${url} ")
println("Domain: '${domain}'")
println("Sub-domain: ' ${subDomain}'")
println("Path: '${path}'")
println("Parent page:${parentUrl} ")
println("Anchor text: ${anchor} " )
if (page.getParseData() instanceof HtmlParseData) {
HtmlParseData htmlParseData = (HtmlParseData) page.getParseData()
String text = htmlParseData.getText()
String html = htmlParseData.getHtml()
List<WebURL> links = htmlParseData.getOutgoingUrls()
println("Text length: " + text.length())
println("Html length: " + html.length())
println("Number of outgoing links: " + links.size())
}
Header[] responseHeaders = page.getFetchResponseHeaders()
if (responseHeaders != null) {
println("Response headers:")
for (Header header : responseHeaders) {
println("\t ${header.getName()} : ${header.getValue()}")
}
}
println("=============")
}
}

Bind data from Database to dhtmlx scheduler

I use dhtmlx schedule and problem in loading data into scheduler view.
I use databasefirst, so i have a sp to fetch all details to display on scheduler,
public ContentResult Data()
{
var data = new SchedulerAjaxData(
new Entities().tblAppTime.Select(e => new { e.SharedId, e.StartTime, e.Duration}));
return data;}
output from var data (during debugging) :
{[{"SharedId":54,"StartTime":"09/14/2013 10:00","Duration":"09/14/2013 11:20"},{"SharedId":56,"StartTime":"09/14/2013 10:00","Duration":"09/14/2013 10:40"},
{[{"SharedId":10,"StartTime":"09/12/2013 8:50","Duration":"09/12/2013 8:55"},{"SharedId":56,"StartTime":"09/14/2013 10:00","Duration":"09/14/2013 10:40"}]}
still I dont get this binded in scheduler, Please help.
Update:
controller.cs
sched.BeforeInit.Add("customeventbox()");
public ContentResult Data()
{
var scheduler = new DHXScheduler();
scheduler.InitialDate= DateTime.Today ;
var data = new SchedulerAjaxData(new OnlineABEntities().GetAppointmentsDisplay(scheduler.InitialDate).Select(e => new {id= e.ID, e.ResourceID,start_date= e.StartTime,end_date= e.Duration, e.Color,text=""}));
return data;
}
schedscript.js
function customeventbox() {
debugger;
scheduler.attachEvent("onTemplatesReady", function () {
alert("eventbox");
scheduler.templates.event_header = function (start, end, ev) {
alert("eventbox1");
return scheduler.templates.event_date(ev.StartTime) + "-" +
scheduler.templates.event_date(ev.Duration);
};
scheduler.templates.event_text = function (start, end, event) {
alert("eventboxtext");
debugger;
return "<br>" + event.ID + "<br>"+event.Duration +"<br>"+event.StartTime+"<br>"+event.Color+ "sampleready" + "<br>"+ "sampletext" ;
}
});
}
Scheduler have some requirements to the loaded data,
check the article in the docs.
In short, the output data must contain at least four following properties, all case-sensitive - id, start_date, end_date and text
If you fetch data like this, it will be displayed in the scheduler
var data = new SchedulerAjaxData(
new Entities().tblAppTime.Select(e => new { id = e.SharedId, start_date = e.StartTime, end_date = e.Duration, text = ""})
);
Update
on the client data objects will have the same set of properties as objects that has been passed to SchedulerAjaxData. Start and end dates of the event are JS date objects, so they should be converted to string before output.
scheduler.templates.event_text = function (start, end, event) {
var dateToStr = scheduler.date.date_to_str("%H:%i");
return "<br>" + event.id +
"<br>" + dateToStr(event.end_date) +
"<br>" + dateToStr(event.start_date) +
"<br>" + event.Color +
"sampleready" + "<br>"+ "sampletext" ;
}
here is details on date format mask
http://docs.dhtmlx.com/scheduler/settings_format.html

displaying errors in grails without refreshing the page

I have a page with dynamic list boxes(selecting value from the first list populates the values in the second list box).
The validation errors for the list boxes are working fine, but while displaying the error messages the page is getting refreshed and the selected values are been set to initial status(need to select the values again in the list boxes)
The page is designed to add any number of list boxes using ajax calls, so adding and selecting the values again is going to be a rework.
Could you help me in displaying the validation errors and keeping the selected values as they are(previously I faced a similar situation which was resolved by replacing local variables of preprocess and postprocess with a global variable, this time no luck with that approach)
Any hints/help would be great
static constraints = {
deviceMapping(
validator: {val, obj ->
Properties dm = (Properties) val;
def deviceCheck = [:];
if (obj.customErrorMessage == null) {
for (def device : dm) {
if (device.key == null || "null".equalsIgnoreCase(device.key)) {
return ["notSelected"];
}
deviceCheck.put(device.key, "");
}
if (deviceCheck.size() != obj.properties["numberOfDevices"]) {
return ["multipleDevicesError"];
}
}
}
)
customErrorMessage (
validator: {
if ("sameDeviceMultipleTimes".equals(it)) {
return ['sameDeviceMultipleTimes']
}
}
)
}
public LinkedHashMap<String, Object> preProcess(sessionObject, params, request) {
Submission submission = (Submission) sessionObject;
def selectedFileName = sessionObject.fileName;
logger.debug("submission.deviceMapping :"+submission.deviceMapping)
try {
Customer customer = Customer.get(submission.customerId);
OperatingSystem operatingSystem = OperatingSystem.get(submission.operatingSystemId)
def ftpClientService = new FtpClientService();
def files = ftpClientService.listFilesInZip(customer.ftpUser, customer.ftpPassword, customer.ftpHost, customer.ftpToPackageDirectory, selectedFileName, operatingSystem, customer.ftpCustomerTempDirectory);
def terminalService = new TerminalService();
OperatingSystem os = OperatingSystem.get(submission.getOperatingSystemId());
def manufacturers = terminalService.getAllDeviceManufacturersForType(os.getType());
logger.debug("manufacturers after os type :"+manufacturers)
logger.debug("files in preprocess :"+files)
def devicesForFiles = [:]
files.each { file ->
def devicesForThisFile = [];
submission.deviceMapping.each { device ->
if (device.value == file.fileName) {
String manufacturer = terminalService.getManufacturerFromDevice("${device.key}");
def devicesForManufacturer = terminalService.getDevicesForManufacturerAndType(manufacturer, os.getType());
devicesForThisFile.push([device:device.key, manufacturer: manufacturer, devicesForManufacturer: devicesForManufacturer]);
}
}
devicesForFiles.put(file.fileName,devicesForThisFile);
}
logger.debug("devicesForFiles :"+devicesForFiles)
return [command: this, devicesForFiles: devicesForFiles, files: files, manufacturers: manufacturers];
} catch (Exception e) {
logger.warn("FTP threw exception");
logger.error("Exception", e);
this.errors.reject("mapGameToDeviceCommand.ftp.connectionTimeOut","A temporary FTP error occurred");
return [command: this];
}
}
public LinkedHashMap<String, Object> postProcess(sessionObject, params, request) {
Submission submission = (Submission) sessionObject;
Properties devices = params.devices;
Properties files = params.files;
mapping = devices.inject( [:] ) { map, dev ->
// Get the first part of the version (up to the first dot)
def v = dev.key.split( /\./ )[ 0 ]
map << [ (dev.value): files[ v ] ]
}
deviceMapping = new Properties();
params.files.eachWithIndex { file, i ->
def device = devices["${file.key}"];
if (deviceMapping.containsKey("${device}")) {
this.errors.reject("You cannot use the same device more than once");
return [];
//customErrorMessage = "sameDeviceMultipleTimes";
}
deviceMapping.put("${device}", "${file.value}");
}
if (params.devices != null) {
this.numberOfDevices = params.devices.size(); //Used for the custom validator later on
} else {
this.numberOfDevices = 0;
}
//logger.debug("device mapping :"+deviceMapping);
submission.deviceMapping = mapping;
return [command: this, deviceMapping: mapping, devicesForFiles: devicesForFiles ];
}
}
The problem is in your gsp page. Be sure that all field are initialised with a value
<g:text value="${objectInstance.fieldname}" ... />
Also the way it is selecting values is through id, so be sure to set it as well:
<g:text value="${objectInstance.fieldname}" id=${device.manufacturer.id} ... />

Resources