Thursday, May 30, 2013

HTML5: Custom attributes

Use data- attribute. The format is "data-*", where "*" is replaced with the desired custom attribute name, then set to the desired string value. For example:

<div id="mydiv" data-brand="toyota" data-model="prius">
   John is very happy with his Toyota Prius, because he saves on gas.
</div>

Reference:
http://www.javascriptkit.com/dhtmltutors/customattributes.shtml

Tuesday, May 28, 2013

Java: convert from Long milliseconds to calendar object

Long myLong = entity.getPurchaseOrderDateMilliseconds();// returns 1369717200000
Timestamp timestamp = new Timestamp(myLong);
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTimeInMillis(timestamp.getTime());        

EXTJS 4.X: showing nested data in grid columns

Here is the response:

{"orderRequest":[
   {
      "detail":"dfvdfvdfv"
      ,"id":"1"
      ,"orderRequestStatus":{"id":"1","name":"registrada solicitud de pedido"}
      ,"orderRequestStatusId":"1"
      ,"purchaseOrderDate":"2013-05-28T00:00:00-05:00"
      ,"purchaseOrderNumber":"dfvdfv"
      ,"requestorName":"dfvd"
      ,"subject":"fv"
},{
   "detail":"wef"
   ,"id":"51"
   ,"orderRequestStatus":{"id":"1","name":"registrada solicitud de pedido"}
   ,"orderRequestStatusId":"1","purchaseOrderDate":"2013-05-28T00:00:00-05:00"
   ,"purchaseOrderNumber":"wef"
   ,"requestorName":"wef"
   ,"subject":"wef"
},{
  "detail":"fv"
   ,"id":"101"
   ,"orderRequestStatus":{"id":"1","name":"registrada solicitud de pedido"}
   ,"orderRequestStatusId":"1"
   ,"purchaseOrderDate":"2013-05-28T00:00:00-05:00"
   ,"purchaseOrderNumber":"dfvd"
   ,"requestorName":"dfvd"
   ,"subject":"fv"}
]}

The model:

Ext.define('WebMonitorClass.model.OrderRequestModel', {
    extend: 'Ext.data.Model',
    fields: [
        {
            name: 'id'
            ,type: 'int'
        },{
            name: 'subject'
            ,type: 'string'
        },{
            name: 'detail'
                ,type: 'string'
        },{
            name: 'requestorName'
                ,type: 'string'
        },{
            name: 'orderRequestStatusName'
            ,type: 'string'
            ,mapping: 'orderRequestStatus.name' // here
        }               
    ]
    ,idProperty: 'id'    
});

Grid panel columns:

columns:[
            {
                text     : 'ID',
                flex     : 1,
                sortable : false,
                width    : 300,
                dataIndex: 'id'
            },
            {
                text     : 'Nombre Solicitante',
                width    : 150,
                sortable : true,
                dataIndex: 'requestorName'
            },{
                text     : 'Resumen',
                width    : 150,
                sortable : true,
                dataIndex: 'subject'
            },{
                text     : 'Detalle',
                width    : 150,
                sortable : true,
                dataIndex: 'detail'
            },{
                text     : 'Estado',
                width    : 150,
                sortable : true,
                dataIndex: 'orderRequestStatusName' // here
            }
        ]

Reference:
http://stackoverflow.com/questions/10500367/extjs-model-fields-with-subfields

EclipseLink: disable caching

To disable caching you have to add the following linen to your persistence.xml file:

<property name="eclipselink.cache.shared.default" value="false"/>

Resource:
http://stackoverflow.com/questions/2809275/disable-caching-in-jpa-eclipselink

Netbeans, JPA, Rest: return attribute object in get response

My  orderRequest entity has the following attributes:

Long id (long)
Long orderRequestStatusId
String detail
Calendar purchaseOrderDate
String purchaseOrderNumber
String requestorName
String subject 
OrderRequestStatus orderRequestStatus

Note that orderRequest.orderRequestStatus is OrderRequestStatus type

Calling the entity get request response I got:

{"orderRequest":
 {"detail":"asdasdasd"
  ,"id":"1"
  ,"orderRequestStatusId":"1"
  ,"purchaseOrderDate":"2013-05-28T00:00:00-05:00"
  ,"purchaseOrderNumber":"sdasasd"
  ,"requestorName":"sdasasd"
  ,"subject":"asdasdasd"}
}

Adding the set and get method in the entity class:

public OrderRequestStatus getOrderRequestStatus() {
        return orderRequestStatus;
    }

    public void setOrderRequestStatus(OrderRequestStatus orderRequestStatus) {
        this.orderRequestStatus = orderRequestStatus;
    }

The get request response now has the orderRequestStatus attribute :

{"orderRequest":
  {"detail":"asdasdasd"
   ,"id":"1"
   ,"orderRequestStatus":
      {"id":"1"
      ,"name":"registrada solicitud de pedido"}
   ,"orderRequestStatusId":"1"
   ,"purchaseOrderDate":"2013-05-28T00:00:00-05:00"
   ,"purchaseOrderNumber":"sdasasd"
   ,"requestorName":"sdasasd"
   ,"subject":"asdasdasd"}
}

Monday, May 27, 2013

Netbeans entity - EclipseLink: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails

I was getting this error:

Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`bismarksi/orders`, CONSTRAINT `FK_Orders_CLIENTID` FOREIGN KEY (`CLIENTID`) REFERENCES `clients` (`ID`))Error Code: 1452Call: INSERT INTO Orders (ID, CLIENTID, MODIFIEDDATE, ORDERSTATUSID, PROJECTID, RECEIPTDATE, SELLERID) VALUES (?, ?, ?, ?, ?, ?, ?)bind => [7 parameters bound]Query: InsertObjectQuery(com.expertix.bismark.si.entities.Order[ id=1 , projectId=1 , orderStatusId=1  ])

I solved the error adding to the entity the set and get method for the clientId Attribute:

   public int getClientId() {
        return clientId;
    }

    public void setClientId(int clientId) {
        this.clientId = clientId;
    }


Javascript: Converting .NET DateTime to Javascript Date Object

User this:

var jsonDate = jqueryCall();  // returns "/Date(1245398693390)/"; 
var re = /-?\d+/; 
var m = re.exec(jsonDate); 
var d = new Date(parseInt(m[0]));

Resource:
http://stackoverflow.com/questions/1016847/converting-net-datetime-to-json

MomentJS: convert from javasript date object to formated String

var myDateObject = new Date();
var myMomentJSFormat = "DD/MM/YYYY hh:mm";
        var myMomentObject = moment(myDateObject);
        // formated using moment.js
        var myMomentObjectFormated = myMomentObject.format(myMomentJSFormat);

Resources:
http://momentjs.com/docs/#/displaying/format/


JPA: handling date, time and timestamp

Here is a great article about handling date, time and timestamp

http://www.developerscrappad.com/228/java/java-ee/ejb3-jpa-dealing-with-date-time-and-timestamp/

Thursday, May 23, 2013

Entity Framework: ono to one relation


Example:

public class User
{
    public int UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }      
    public int BillingAddressId { get; set; }

    public Address BillingAddress { get; set; }

}

public class Address
{
    public int AddressId { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string PostalCode { get; set; }
}

// one user one billing adress
modelBuilder.Entity<User>()
                .HasRequired(a => a.BillingAddress)
                .WithMany()
                .HasForeignKey(u => u.BillingAddressId);

Note: this is not going to generate the unique constraint in the db on  BillingAddressId field

Reference:
http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx

NetBeans: RestFul Web Services Tutorial

JPA: Many to one relation

Here is a simple example of a Many to one relation using JPA:

http://ayushsuman.blogspot.com/2010/06/jpa-one-to-many-and-many-to-one.html

Tuesday, May 21, 2013

Monday, May 20, 2013

Jquery: add readonly to HMTL5 input tag

Use attr method:

$('#inputId').attr('readonly', true);

Reference:
http://stackoverflow.com/questions/1306708/add-readonly-to-input-jquery

Microsoft Visual Studio: Unable to start debugging on the web server

Using Microsoft Visual Studio, when you try to run the solution, ff you get the error:

Microsoft Visual Studio: Unable to start debugging on the web server. The web server did not respond in a timely manner.  This may be because another debugger is already attached to the web server.


Just reset IIS using iisreset in a console opened as administrator:






Thursday, May 16, 2013

IIS7.5: “405 method not allowed” for “PUT” method

If you get the “405 method not allowed” for “PUT” method You have to disable the ISS webdav option:

In Windows 7
1. Click on "start"
2. clieck on "Control panel"
3. click on "Programs"
4. click on "Turn windows features on or off" and wait some seconds
5. Disable the option : "Internet Information Services" > "world wide web services" > "Common HTTP Feature"s > "webDAV publishing"
6. Click "ok"

Reference:
http://social.msdn.microsoft.com/Forums/en-US/windowsazure/thread/a22b9e60-8353-40c7-af3e-69a8f18240c1

Jquery: get selected item value of a dropdown - combobox element in jQuery

Use $('#{Id} :selected')

var selectedItem = $('#dropDownId :selected') // selected item object
var selectedItemText = $('#dropDownId :selected').text(); // selection text
var selectedItemId = $('#dropDownId :selected').attr('id'); // selection id

Reference:
http://stackoverflow.com/questions/2780566/to-get-selected-value-of-a-dropdown-select-element-in-jquery

Tuesday, May 14, 2013

HTML5: select tag placeholder

Use the disabled selected property:


<select>
    <option value="" disabled selected>Select your option</option>
    <option value="hurr">Durr</option>
</select>

Reference:
http://stackoverflow.com/questions/5805059/select-placeholder

Monday, May 13, 2013

JQuery: remove content of div

To remove all the content of a div you can use .empty() function:

$('#mydiv').empty();

Reference:
http://stackoverflow.com/questions/652917/in-jquery-want-to-remove-all-html-inside-of-a-div