Monday, April 16, 2012

JSF 2 - How to destroy a session-scoped bean

How can I destroy a session-scoped bean?



The purpose of this would be to control the lifetime of the bean so it only lives when a tab in the web application is active. (Using Ajax Based Tab Navigation in the webapp)



Is there a better way to do that? (Custom Scoped Beans?)





'syntax error, unexpected T_CONSTANT_ENCAPSED_STRING' Adding google event tracking to button in Wordpress Plugin

I'm attempting to add a google event tracking to a wordpress plugin. The code for the submit button in this plugin is:



{
return "<input type=\"submit\" ".
"name=\"".fm_form_submit_btn_name()."\" ".
"id=\"".fm_form_submit_btn_id()."\" ".
"class=\"submit\" ".
"value=\"".fm_form_submit_btn_text()."\" ".
"onclick=\"".fm_form_submit_btn_script()."\" ".
" />\n";
}


I'm trying to add google analytics tracking code onclick=_gaq.push(['_trackEvent', 'Form', 'Submit', 'Apply']) to the above block, and eventually replace 'Apply' with fm_form_the_title() which returns the title of the form.



The problem: No matter what arrangement of quotes I use when inserting the tracking code block, I am faced with an error 'syntax error, unexpected T_CONSTANT_ENCAPSED_STRING' or 'T_STRING' which shuts down the entire site.



EDIT: The code block above works, and does not need simplifying, and is part of a much larger project. My question is how to add onclick=_gaq.push(['_trackEvent', 'Form', 'Submit', 'Apply']) and eventually replace 'Apply' with fm_form_the_title() and NOT break my website.





JavaScript convert mouse position to selection range

I would like to be able to convert the current mouse position to a range, in CKEditor in particular.



The CKEditor provides an API for setting the cursor according to a range:



var ranges = new CKEDITOR.dom.range( editor.document );
editor.getSelection().selectRanges( [ ranges ] );


Since CKEditor provides this API, the problem may be simplified by removing this requirement and just find a way to produce the range from the mouse coordinates over a div containing various HTML elements.



However, this is not the same as converting a mouse coordinate into the cursor position in a textarea since textareas have fixed column widths and row heights where the CKEditor renders HTML through an iframe.



Based on this, it looks like the range may be applied to elements.



How would you figure out the start/end range which is closest to the current mouse position?



Edit:
An example of how one might use the ckeditor API to select a range on the mouseup event.



editor.document.on('mouseup', function(e) {
this.focus();
var node = e.data.$.target;

var range = new CKEDITOR.dom.range( this.document );
range.setStart(new CKEDITOR.dom.node(node), 0);
range.collapse();

var ranges = [];
ranges.push(range);
this.getSelection().selectRanges( ranges );
});


The problem with the above example is that the event target node (e.data.$.target) is only firing for nodes such as HTML, BODY, or IMG but not for text nodes. Even if it did, these nodes represent chunks of text which wouldn't support setting the cursor to the position of the mouse within that chunk of text.





Thursday, April 12, 2012

Keeping variable value while making async call to FB.api

I am trying to get albums of a user with the following JavaScript function, first call gets album list, and while iterating in albums I get their cover picture:



function GetAlbums() {
FB.api('/me/albums', function(resp) {
var ul = document.getElementById('albums');
for (var i=0, l=resp.data.length; i<l; i++) {
var album = resp.data[i];
FB.api('/'+album.cover_photo, function(resp1) {
li = document.createElement('li'),
a = document.createElement('a');
a.innerHTML = "<img src='"+resp1.picture+"'/>"+album.name;
a.href = album.link;
li.appendChild(a);
ul.appendChild(li);
});
}
});
};


I don't know why but cover pictures are fetched ok, but album name is always same. Probably the inner FB.api call is async. and before it finishes the album iteration goes to the last element. How can I correct the code?





Apache Unexpected subelement exception while generating a webservice client

I'm trying to generate a webservice client with wsdl2java from axis2 (version 1.6.1).



./wsdl2java.sh -uri http://www.ncbi.nlm.nih.gov/entrez/eutils/soap/v2.0/efetch_snp.wsdl


When I call this service, I get an Exception.



org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement {http://www.ncbi.nlm.nih.gov/soap/eutils/efetch_snp}Rs



    try {
EFetchSnpServiceStub fetchService = new EFetchSnpServiceStub();
EFetchSnpServiceStub.EFetchRequest reqIdSnp = new EFetchSnpServiceStub.EFetchRequest();
reqIdSnp.setId("193925233");
EFetchSnpServiceStub.EFetchResult resIdSnp = fetchService.run_eFetch(reqIdSnp);
} catch (Exception e) {
System.out.println(e.toString());
}


With soaptest however I can see the Rs Tag in the result.



<Rs rsId="193925233" snpClass="snp" snpType="notwithdrawn" molType="genomic" bitField="050000000005000000000100" taxId="3702">


How can I fix this exception? The WSDL is not under my control.





Project created in Visual Studio, open in Visual Studio Express for Windows Phone

I am new to Windows Phone 7 and Visual Studio and was just trying to look at a sample project and apparently the project was created in Visual Studio 2010 (as the .sln file of the project says)



On my machine, for WP7 development I have installed Visual Studio 2010 Express for Windows Phone which comes with WP7 SDK.



Now, when I try to open the sample project on VS Express it says:



The project type is not supported by this installation.


Is there a way that I can try the sample project on Visual Studio Express for WP7 or I must have Visual Studio installed?



Edit:



This is the project that I wanted to open in VSE for WP





Can I and is there a need to avoid singleton pattern?

I'm writing a program where among lots of other stuff I need three classes we can call here writer, storage and reader.



Writer needs to access the storage class very very often while reader instead somewhat seldom especially compared to writer. Storage class is there only to store the data writer writes. The only thing the writer is doing, is just to write some short bursts of data quite often. The reader reads the written data from storage and then flushes the storage to free some space for the writer to write new data. To give some numbers and idea of the frequency of the accesses let's say that the writer is accessing the storage numerous times in minute and the reader is accessing it approximately once in an hour.



So the question is that do I need to use the singleton pattern in the storage class or is it enough to declare it as static class?



Also how I can ensure that when the reader class is using the storage, it releases the storage resource immediately after it has read and flushed the data from the storage? Most of the time the storage class should be available for the writer to write the data in it.



The singleton approach looks nice especially that I'm not from OOP background. I've heard that it's bad though.