Tuesday, August 7, 2012

something like Dot Net LINQ in Java : JOOOQ

jOOQ : A peace treaty between SQL and Java

SQL was never meant to be abstracted. To be confined in the narrow boundaries of heavy mappers, hiding the beauty and simplicity of relational data. SQL was never meant to be object-oriented. SQL was never meant to be anything other than... SQL!

It's simple. With the jOOQ DSL, SQL looks almost as if it were natively supported by Java. For instance, get all books published in 2011, ordered by title

create.selectFrom(BOOK)
      .where(PUBLISHED_IN.equal(2011))
      .orderBy(TITLE)
 
jOOQ also supports more complex SQL statements. get all authors' first and last names, and the number of books they've written in German, if they have written more than five books in German in the last three years (from 2011), and sort those authors by last names limiting results to the second and third row, then lock first and last names columns for update
 
http://www.jooq.org/ 

How to add custom unbound property to LINQ2SQL , EF

We encountered this error because we did not map entity property OrganizationName to certain table column in the database. You can check the table mapping by right click the entity User and select “Table Mapping”.
However, I don’t think we can solve the problem by only adding a custom property in the designer. Even we add the Organization table in the Mapping Details and map the OrganizationName property to Organization.Name column, EF still wants us to map the PK of the Organization table to one property in the User entity. But the FK property of User entity is not Entity Key, so the entire mapping still fails. Hope I made it clear to understand.
To workaround, I would recommend you to write some hard code to add a custom property into the User class. Since all the entity classes are partial classes, we can create a new partial User class and add the custom property outside the EDM designer:
==========================================================================
public partial class User
{
public string OrganizationName
{
get
{
if (this.Organization != null)
return this.Organization.Name;
else
return string.Empty;
}
set
{
if (this.Organization != null)
this.Organization.Name = value;
}
}
}
==========================================================================
BTW, I should mention the lazy loading feature of EF4 which is not available in EFv1. That mentions the related entities will not be loaded until we try to access them. If this case, if we turn off the lazy loading feature by (context.ContextOptions.LazyLoadingEnabled = false), we need to add such a filter in the getter/setter of property OrganizationName:
==========================================================================
if (!this.OrganizationReference.IsLoaded)
{
this.OrganizationReference.Load();
}
==========================================================================

Monday, August 6, 2012

C# Open WPF window in WindowsForm APP

using System;  
using System.Windows.Forms;  
using System.Windows.Forms.Integration;  
 
var wpfwindow = new WPFWindow.Window1();  
ElementHost.EnableModelessKeyboardInterop(wpfwindow); 
wpfwindow.Show();
 
you need to add project reference to WindowsFormsIntegration.dll 

Monday, July 30, 2012

REST



REST stands for Representational State
Transfer and was looked at in the dissertation
by Roy Thomas Fielding.
Some of the most important aspects of the
REST environment are uniquely addressable
resources, their different characteristics
and formats, a uniform and easy - to - follow programming interface, and the facilitation of a highly
scalable environment.
Implemented today, REST mainly entails the use of technologies which are already well established
in practice on the Web and which have been used to outline the advantages of REST. HTTP or
HTTPS, for example, may be used as the transfer protocol. URLs including query strings are
used to address resources, and the representation formats supported range from HTML and XML
to JSON and ATOM, as well as to sound and video fi les. The simple and intuitive programming
interface previously mentioned is achieved by using HTTP verbs and status codes.
If, for example, you point your browser at the URL http://p2p.wrox.com/content/blogs ,
status code 200 is returned and your browser displays the returned HTML in the form of a
web site. In keeping with normal web practices, the following page also contains links to other
resources and topics:
In the URL above you can also see how intuitively these URLs are structured. Or use the following
URL to receive an RSS feed:
In contrast to SOA with SOAP, REST is not concerned with the defi nition of messages and the
design of methods; central components are resources and actions which can affect those resources.
The actions which affect resources are mainly CRUD (Create, Read, Update, and Delete) methods.
What should be done with a resource is determined by means of an HTTP verb, and the success of
the action is ascertained by querying the HTTP status code.
In the following example, a GET at http://localhost:1234/CarPool shows all the cars in the
fl eet in the form of an XML document. Status code 200 indicates success.
URL: http://localhost:1234/CarPool
Verb: GET
Status-Code: 200
Response-Body:
< Cars xmlns="http://schemas.datacontract.org/2004/07/Wrox"
xmlsn:i="http://www.w3.org/2001/XMLSchemainstance"
> < Car > < Make > Dodge < /Make > < Name > Dakota < /Name > < Seats > 8 < /Seats > < Type > Pickup
Truck < /Type > < /Car > < Car > < Make > Audi < /Make > < Name > TT < /Name > < Seats > 2 < /Seats > < Type > Sport
Car < /Type > < /Car > < Car > < Make > Seat < /Make > < Name > Leon < /Name > < Seats > 5 < /Seats > < Type > Sport
Car < /Type > < /Car > < /Cars >
In the following example, a GET at http://localhost:1234/CarPool/TT shows the Audi TT in the
form of an XML document. Status code 200 indicates success. Status code 404, for example, would
mean not found.
http://localhost:1234/CarPool/TT
Verb: GET
Status-Code: 200
Response-Body:
< Car xmlns="http://schemas.datacontract.org/2004/07/Wrox"
xmlsn:i="http://www.w3.org/2001/XMLSchemainstance"
> < Make > Audi < /Make > < Name > TT < /Name > < Seats > 2 < /Seats > < Type > Sport
Car < /Type > < /Car >
In the following example, a GET at http://localhost:1234/CarPool/TT?format=json shows the
Audi TT in the form of a JSON document. Status code 200 indicates success. Status code 404, for
example, would mean not found.
URL: http://localhost:1234/CarPool/TT?format=json
Verb: GET
Status-Code: 200 (OK)
Response-Body:
{"Make":"Audi","Name":"TT","Seats":2,"Type":"Sport Car"}
In the following example, a PUT at http://localhost:1234/CarPool/Leon with the requisite
XML data in the request body would mean that the car is to be added to the car pool. Status code201 means created. As a result of the POST action, the added object is normally returned, along with
any further details or links to other objects.
URL: http://localhost:1234/CarPool/Leon
Verb: PUT
Request-Body:
< Car xmlns="http://schemas.datacontract.org/2004/07/Wrox"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance" >
< Make > Seat < /Make > < Name > Leon < /Name > < Seats > 4 < /Seats > < Type > Sport
Car < /Type > < /Car >
Status-Code: 201 (Created)
Response-Body:
< Car xmlns="http://schemas.datacontract.org/2004/07/Wrox"
xmlsn:i="http://www.w3.org/2001/XMLSchemainstance"
> < Make > Seat < /Make > < Name > Leon < /Name > < Seats > 4 < /Seats > < Type > Sport
Car < /Type > < /Car >
Apart from GET and PUT , which have already been seen, the verbs most commonly used also include
DELETE and POST .
GET is used exclusively to retrieve data and, therefore, the result can also be buffered without any
concerns. It also offers a major advantage over SOAP messages in which the actual call cannot be
identifi ed straight away as a reading call. Therefore, they are also diffi cult to buffer.
DELETE is used for deleting resources. It should be called up as often as you wish without any
concerns about side effects. When http://localhost:1234/CarPool/Leon is called up for the fi rst
time, for example, the Leon is deleted. The next time the same URL is called up, the resource is no
longer available and can, therefore, not be deleted either.
PUT is used to add or change a resource if you can defi ne the URL yourself. For example, a PUT at
http://localhost:1234/CarPool/Leon along with corresponding details in the request body
means that this resource is added anew with the URL used. If you assign a PUT to this URL with
other values in the request body, it would lead to an update in the resource.
POST is an exception in certain regards. First of all, POST is frequently misused as DELETE and
PUT , because the use of DELETE and PUT is either not permitted or technically impossible from the
browser ’ s perspective. Secondly, POST is used to add new resources to a container without having
control of the URL created. Thus, for example, a POST to http://localhost:1234/CarPool
with corresponding details in the body would mean that a new car is being added to the carpool.
However, in this case the POST call would have to return the URL of the newly created resource.
In any event, the REST is stateless. Therefore, whenever a call is made, all the requisite values are
either transferred in the address of the resource or as additional parameters. This behavior also
means that REST applications are generally much easier to scale because there is no need to be
concerned about which server to send the query to in a load - balanced environment.
REST and WCF
WCF not only provides a platform for SOAP messages, it is also a means of offering RESTful
services.
The essential components that allow you to create a REST application can be found in System.
ServiceModel.Web.dll . The most important elements of a REST application include the [WebGet]
and [WebInvoke] attributes and the new binding webHttpBinding in connection with the webHttp
endpoint behavior.
If you use [WebGet] or [WebInvoke] , the [OperationContract] attribute is
optional.
Should you wish to create a RESTful solution from your SOAP - based solution, theoretically you
would simply have to change the binding to webHttpBinding and confi gure the endpoint with the
webHttp behavior.
This binding has the following effect: If you send a SOAP message to an endpoint in a classical SOA
environment, the content of the message will normally be checked fi rst. The correct operation will
be called up depending on the content of the message.
In REST, however, the method which is called up is dependent on the URL and on the verb used.
This altered dispatch behavior is made possible by the binding webHttpBinding and the webHttp
endpoint behavior. Furthermore, the WebHttpBehavior class or the endpoint behavior also enables
you to choose from two different serialization formats:
POX (Plain old XML) uses XML alone without the SOAP overhead.
JSON (JavaScript Object Notation) is a very compact and effi cient format, primarily in connection
with JavaScript.

Saturday, July 28, 2012

POLLINGDUPLEXHTTPBINDING: HTTP POLLING

POLLINGDUPLEXHTTPBINDING: HTTP POLLING
The changing needs of users have led to the creation of web sites being increasingly interactive.
In this context, Silverlight offers a powerful development platform to create rich, interactive
applications. Silverlight runtime runs as a client in the user ’ s browser process. As with any clients, it
needs to access data remotely, often exposed as service. The more convenient and obvious platform
to develop this service is WCF.
The most common scenario enables a request - reply message exchange pattern where the Silverlight
client sends a request and receives a reply from the service. But sometimes, the service needs to
inform or notify the client when something happens on the other side. The service needs to push
data to the client. PollingDuplexHttpBinding , available since Silverlight v3 in the System.
ServiceModel.PollingDuplex.dll, is designed to accomplish this communication protocol.
PollingDuplexHttpBinding is the implementation of the WS - MakeConnection v1.1 OASIS
Specifi cation ( http://docs.oasis - open.org/ws - rx/wsmc/v1.1/wsmc.html ). In practice, when
a session is established, the client sends the request to the service and the service replies with an
Http response only when it needs to push data to the client — meaning only when there are data
for the client. This is done with a mechanism already known as long polling . If there aren ’ t data for
the client, the service holds on to the latest Http request until there are available data or a timeout
is reached. If the client wants to get more information after receiving one, it re - sends a new Http
request. The communication goes on until an active Http request is alive.
On client side you have to reference the client version of the PollingDuplexHttpBinding from the
System.ServiceModel.PollingDuplex.dll in the Silverlight v3 SDK Libraries path.

Sunday, July 8, 2012

Java Persian Calendar Class


package bixolonprint;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

 public class CalendarTool {


 public CalendarTool()
 {
 Calendar calendar = new GregorianCalendar();
 setGregorianDate(calendar.get(Calendar.YEAR),
 calendar.get(Calendar.MONTH)+1,
 calendar.get(Calendar.DAY_OF_MONTH));
 }

 public String getCurrentTimeStamp() {
    SimpleDateFormat sdfDate = new SimpleDateFormat("HH:mm:ss");//dd/MM/yyyy //yyyy-MM-dd
    Date now = new Date();
    String strDate = sdfDate.format(now);
    return strDate;
}
 public int getCurrentDay() {
    SimpleDateFormat sdfDate = new SimpleDateFormat("dd");//dd/MM/yyyy //yyyy-MM-dd HH:mm:ss
    Date now = new Date();
    String strDate = sdfDate.format(now);
    return Integer.parseInt( strDate );
}

 public CalendarTool(int year, int month, int day)
 {
 setGregorianDate(year,month,day);
 }

 public int getIranianYear() {
 return irYear;
 }

 public int getIranianMonth() {
 return irMonth;
 }

 public int getIranianDay() {
 return irDay;
 }

 public int getGregorianYear() {
 return gYear;
 }

 public int getGregorianMonth() {
 return gMonth;
 }

 public int getGregorianDay() {
 return gDay;
 }

 public int getJulianYear() {
 return juYear;
 }

 public int getJulianMonth() {
 return juMonth;
 }

 public int getJulianDay() {
 return juDay;
 }

 public String getIranianDate()
 {
 return (irYear+"/"+irMonth+"/"+irDay);
 }

 public String getGregorianDate()
 {
 return (gYear+"/"+gMonth+"/"+gDay);
 }

 public String getJulianDate()
 {
 return (juYear+"/"+juMonth+"/"+juDay);
 }

 public String getWeekDayStr()
 {
 String weekDayStr[]={
 "Monday",
 "Tuesday",
 "Wednesday",
 "Thursday",
 "Friday",
 "Saturday",
 "Sunday"};
 return (weekDayStr[getDayOfWeek()]);
 }

 public String toString()
 {
 return (getWeekDayStr()+
 ", Gregorian:["+getGregorianDate()+
 "], Julian:["+getJulianDate()+
 "], Iranian:["+getIranianDate()+"]");
 }

 public int getDayOfWeek()
 {
 return (JDN % 7);
 }

 public void nextDay()
 {
 JDN++;
 JDNToIranian();
 JDNToJulian();
 JDNToGregorian();
 }

 public void nextDay(int days)
 {
 JDN+=days;
 JDNToIranian();
 JDNToJulian();
 JDNToGregorian();
 }

 public void previousDay()
 {
 JDN--;
 JDNToIranian();
 JDNToJulian();
 JDNToGregorian();
 }

 public void previousDay(int days)
 {
 JDN-=days;
 JDNToIranian();
 JDNToJulian();
 JDNToGregorian();
 }

 public void setIranianDate(int year, int month, int day)
 {
 irYear =year;
 irMonth = month;
 irDay = day;
 JDN = IranianDateToJDN();
 JDNToIranian();
 JDNToJulian();
 JDNToGregorian();
 }

 public void setGregorianDate(int year, int month, int day)
 {
 gYear = year;
 gMonth = month;
 gDay = day;
 JDN = gregorianDateToJDN(year,month,day);
 JDNToIranian();
 JDNToJulian();
 JDNToGregorian();
 }

 public void setJulianDate(int year, int month, int day)
 {
 juYear = year;
 juMonth = month;
 juDay = day;
 JDN = julianDateToJDN(year,month,day);
 JDNToIranian();
 JDNToJulian();
 JDNToGregorian();
 }

 private void IranianCalendar()
 {
 // Iranian years starting the 33-year rule
 int Breaks[]=
 {-61, 9, 38, 199, 426, 686, 756, 818,1111,1181,
 1210,1635,2060,2097,2192,2262,2324,2394,2456,3178} ;
 int jm,N,leapJ,leapG,jp,j,jump;
 gYear = irYear + 621;
 leapJ = -14;
 jp = Breaks[0];
 // Find the limiting years for the Iranian year 'irYear'
 j=1;
 do{
 jm=Breaks[j];
 jump = jm-jp;
 if (irYear >= jm)
 {
 leapJ += (jump / 33 * 8 + (jump % 33) / 4);
 jp = jm;
 }
 j++;
 } while ((j<20) && (irYear >= jm));
 N = irYear - jp;
 // Find the number of leap years from AD 621 to the begining of the current
 // Iranian year in the Iranian (Jalali) calendar
 leapJ += (N/33 * 8 + ((N % 33) +3)/4);
 if ( ((jump % 33) == 4 ) && ((jump-N)==4))
 leapJ++;
 // And the same in the Gregorian date of Farvardin the first
 leapG = gYear/4 - ((gYear /100 + 1) * 3 / 4) - 150;
 march = 20 + leapJ - leapG;
 // Find how many years have passed since the last leap year
 if ( (jump - N) < 6 )
 N = N - jump + ((jump + 4)/33 * 33);
 leap = (((N+1) % 33)-1) % 4;
 if (leap == -1)
 leap = 4;
 }


 public boolean IsLeap(int irYear1)
 {
 // Iranian years starting the 33-year rule
 int Breaks[]=
 {-61, 9, 38, 199, 426, 686, 756, 818,1111,1181,
 1210,1635,2060,2097,2192,2262,2324,2394,2456,3178} ;
 int jm,N,leapJ,leapG,jp,j,jump;
 gYear = irYear1 + 621;
 leapJ = -14;
 jp = Breaks[0];
 // Find the limiting years for the Iranian year 'irYear'
 j=1;
 do{
 jm=Breaks[j];
 jump = jm-jp;
 if (irYear1 >= jm)
 {
 leapJ += (jump / 33 * 8 + (jump % 33) / 4);
 jp = jm;
 }
 j++;
 } while ((j<20) && (irYear1 >= jm));
 N = irYear1 - jp;
 // Find the number of leap years from AD 621 to the begining of the current
 // Iranian year in the Iranian (Jalali) calendar
 leapJ += (N/33 * 8 + ((N % 33) +3)/4);
 if ( ((jump % 33) == 4 ) && ((jump-N)==4))
 leapJ++;
 // And the same in the Gregorian date of Farvardin the first
 leapG = gYear/4 - ((gYear /100 + 1) * 3 / 4) - 150;
 march = 20 + leapJ - leapG;
 // Find how many years have passed since the last leap year
 if ( (jump - N) < 6 )
 N = N - jump + ((jump + 4)/33 * 33);
 leap = (((N+1) % 33)-1) % 4;
 if (leap == -1)
 leap = 4;
 if (leap==4 || leap==0)
 return true;
 else
 return false;

}

 private int IranianDateToJDN()
 {
 IranianCalendar();
 return (gregorianDateToJDN(gYear,3,march)+ (irMonth-1) * 31 - irMonth/7 * (irMonth-7) + irDay -1);
 }

 private void JDNToIranian()
 {
 JDNToGregorian();
 irYear = gYear - 621;
 IranianCalendar(); // This invocation will update 'leap' and 'march'
 int JDN1F = gregorianDateToJDN(gYear,3,march);
 int k = JDN - JDN1F;
 if (k >= 0)
 {
 if (k <= 185)
 {
 irMonth = 1 + k/31;
 irDay = (k % 31) + 1;
 return;
 }
 else
 k -= 186;
 }
 else
 {
 irYear--;
 k += 179;
 if (leap == 1)
 k++;
 }
 irMonth = 7 + k/30;
 irDay = (k % 30) + 1;
 }

 private int julianDateToJDN(int year, int month, int day)
 {
 return (year + (month - 8) / 6 + 100100) * 1461/4 + (153 * ((month+9) % 12) + 2)/5 + day - 34840408;
 }

 private void JDNToJulian()
 {
 int j= 4 * JDN + 139361631;
 int i= ((j % 1461)/4) * 5 + 308;
 juDay = (i % 153) / 5 + 1;
 juMonth = ((i/153) % 12) + 1;
 juYear = j/1461 - 100100 + (8-juMonth)/6;
 }

 private int gregorianDateToJDN(int year, int month, int day)
 {
 int jdn = (year + (month - 8) / 6 + 100100) * 1461/4 + (153 * ((month+9) % 12) + 2)/5 + day - 34840408;
 jdn = jdn - (year + 100100+(month-8)/6)/100*3/4+752;
 return (jdn);
 }

 private void JDNToGregorian()
 {
 int j= 4 * JDN + 139361631;
 j = j + (((((4* JDN +183187720)/146097)*3)/4)*4-3908);
 int i= ((j % 1461)/4) * 5 + 308;
 gDay = (i % 153) / 5 + 1;
 gMonth = ((i/153) % 12) + 1;
 gYear = j/1461 - 100100 + (8-gMonth)/6;
 }

private int irYear;
 private int irMonth;
 private int irDay;
 private int gYear;
 private int gMonth;
 private int gDay;
 private int juYear;
 private int juMonth;
 private int juDay;
 private int leap;
 private int JDN;
 private int march;
 }

Saturday, July 7, 2012

Host a JAVA Application as Windows Service with Apache Commons Daemon

Enjoy it , works correctly  :

http://commons.apache.org/daemon/procrun.html

Procrun is a set of applications that allow Windows users to wrap (mostly) Java applications (e.g. Tomcat) as a Windows service.

The service can be set to automatically start when the machine boots and will continue to run with no user logged onto the machine.

Prunmgr is a GUI application for monitoring and configuring procrun services.
Each command line directive is in the form of //XX[//ServiceName]
If the //ServiceName parameter is omitted, then the service name is assumed to be the name of the file.
The Prunsrv application behaves in the same way, so to allow both applications to reside in the same directory, the Prunmgr application will remove a trailing w (lower-case w) from the name.
For example if the Prunmgr application is renamed as TestService.exe - or as TestServicew.exe - then the default service name is TestService.
 ---
linux distributions
http://en.wikipedia.org/wiki/List_of_Linux_distributions