Category Archives: IT Stuff

Dynamic for with Spring MVC using a HashMap

Sometime you need to dynamically generate a form without knowing how many fields it will be required (i.e. when your form is driven by a configuration or by some properties). The problem is to draw the form and, return the values to the Controller, and recognize the couples Field name / Field value after the submit..

You can easily get solve this problem just adding HashMap which will hold the key-value pair data to the DataModel.

Supposing your configuration says: you have to draw two fields and these are the name, you UI will be something like*:

<c:forEach items="${newRequest.fields}" var="field">
	<f:input type="text" path="rawFields['${field.field_id}']" class="form-control validate[groupRequired[mandatoryField]]" /> (R)
</c:forEach>

When you submit the form, the values and the key for the dynamic fields will be filled.

* newRequest is the DataModel you are passing and fields is the list of Fields that user will fill with data, like that:

public class Request {
 
	/** Request type */
	private int templateRequest;
 
	// ***** Input field ***** 
	List<RequestField> fields = new ArrayList<RequestField>();
 
	private HashMap<String, Object> rawFields = new HashMap<String, Object>();
 
	[Setters and getters]
 
}
Tagged , , ,

Apply jQuery datepicker to multiple instances

To add multiple instance of jQuery datepicker (or timepicker), it is possible to use class instead of the ID to initialize the widget in particular when the form is generated from the backend and the exact number of field is not known.

The html code will be:

<input type="text" class="datepicker" id="date1" />
<input type="text" class="datepicker" id="date2" />
<input type="text" class="datepicker" id="date3" />

The jQuery script will be:

$('.datepicker').each(function(){
    $(this).datepicker();
});

Remember to include jquery and jquery-ui scripts to the page.

Tagged , , ,

Leadership

The milestone of the leadership: Feb 5, 1995

Feb 5, 1995

Tagged

Disaster recovery

A disaster recovery plan (DRP) is a documented process or set of procedures to recover and protect a business IT infrastructure in the event of a disaster.

6844.strip_

Background

What do you actually mean with “Unfortunately, your background isn’t exactly what our client is looking for at this time”? It’s IT not old style industry!

dilbert2008073346229

On the Nature of Software

I never think about this:

Software is remarkable stuff. Sometimes, perhaps because we work with it all the time, we forget just how remarkable it is.
Very little else in human experience is as malleable, allowing us free rein to exercise our ingenuity and inventiveness almost without limits. Also, with a very few exceptions that we’ll cover later, software is deterministic—the next state is completely determined by the current state, and (crucially) we have com- plete access to all of that state whenever we want it.
Compared to traditional engineering, we are spoiled. What do you think a Formula One engineer would give to be able to instantaneously stop an engine when it’s rotating at 19,000 revolutions per minute and examine every aspect of it in minute detail? To see the precise state of each component while under pressure and stress, for example, or to dynamically record the shape and position of the flame front within the combustion chambers during ignition?
It is exactly this kind of trick that we are able to perform with our software, which is why the empirical approach is particularly powerful when debugging. [from book Debug It!]

Software archeology

l

Yeah, it happens… every single day of my life….

(Image owned by geek-and-poke.com)

Tagged

How can full-time telecommuters overcome feelings of isolation?

I’m working as full time telecommuters since 3 years and I periodically feel really uncomfortable with that (something that remembers a sine wave). Of course, isolation is the biggest issue that afflicts a telecommuter. I was reading some post on Internet about that a it seems to be an actual problem with many solutions, but anything that work for my small town.

Looking at this post on workplace.stackexchange.com and one of best solution was to search coworking places… in San Francisco :)

BTW, it seems to be really hard to explain to not IT people that telecommuting has some vexing cons…

Some fun pics about TC:



Tagged

Facebook: can we consider it a tailoring content platform?

I was looking at my Facebook home page and with complacency I seen I’m finally able to get my almost perfect online journal. Just hiding not important updates and liking the pages I am actually interested, I can be able to see all news that are tailored for me. Finally a gossip free web page without invasive advs.

Facebook can be used for this purpose btw the problem I see is be sure people near you understand you aren’t wasting your time with chat or stupid game :)

Tagged

Get auto generated keys with JdbcTemplate

Sometime you need to get the auto generated key of last record inserted into a table. It is possible to use Interface JdbcTemplate() and KeyHolder interface; this sample has been tested with mySql DB and Spring 3.2:

final String sqlCommand = new String("INSERT INTO Template_request(title, descriptio) VALUES (?, ?)");
KeyHolder holder = new GeneratedKeyHolder();
 
getJdbcTemplate().update(new PreparedStatementCreator() {           
 
    @Override
    public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
        PreparedStatement ps = connection.prepareStatement(sqlCommand, Statement.RETURN_GENERATED_KEYS);
        ps.setString(1, templateRequest.getTitle());
        ps.setString(2, templateRequest.getDecription());        
        return ps;
    }
}, holder);
 
Long request_id = holder.getKey().longValue();

This is the definition of Template_request table:

DROP TABLE IF EXISTS `Template_request`;
CREATE TABLE Template_request
(
    id_templaterequest	INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
    title				VARCHAR(256) NOT NULL,
    description			VARCHAR(2048),
    date_uprec 			TIMESTAMP DEFAULT now() ON UPDATE now(),
    CONSTRAINT PK_Template_request PRIMARY KEY (id_templaterequest)
);

interface KeyHolder
Interface for retrieving keys, typically used for auto-generated keys as potentially returned by JDBC insert statements.
Implementations of this interface can hold any number of keys. In the general case, the keys are returned as a List containing one Map for each row of keys.

Most applications only use on key per row and process only one row at a time in an insert statement. In these cases, just call getKey to retrieve the key. The returned value is a Number here, which is the usual type for auto-generated keys.

public class JdbcTemplate extends JdbcAccessor implements JdbcOperations
This is the central class in the JDBC core package. It simplifies the use of JDBC and helps to avoid common errors. It executes core JDBC workflow, leaving application code to provide SQL and extract results. This class executes SQL queries or updates, initiating iteration over ResultSets and catching JDBC exceptions and translating them to the generic, more informative exception hierarchy defined in the org.springframework.dao package.
Code using this class need only implement callback interfaces, giving them a clearly defined contract. The PreparedStatementCreator callback interface creates a prepared statement given a Connection, providing SQL and any necessary parameters. The ResultSetExtractor interface extracts values from a ResultSet. See also PreparedStatementSetter and RowMapper for two popular alternative callback interfaces.

Can be used within a service implementation via direct instantiation with a DataSource reference, or get prepared in an application context and given to services as bean reference. Note: The DataSource should always be configured as a bean in the application context, in the first case given to the service directly, in the second case to the prepared template.

Because this class is parameterizable by the callback interfaces and the SQLExceptionTranslator interface, there should be no need to subclass it.

All SQL operations performed by this class are logged at debug level, using “org.springframework.jdbc.core.JdbcTemplate” as log category.