星期日, 7月 10, 2011

MySQL JDBC

 

 

Driver:  com.mysql.jdbc.Driver

URL: jdbc:mysql://localhost:3306/test?useUnicode=true

星期四, 7月 07, 2011

A Vaadin resource for the blobstore

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class BlobstoreResource implements ApplicationResource {
// variables skipped
public BlobstoreResource(String keyStr, Application application) {
this.application = application;
this.blobKey = new BlobKey(keyStr);
this.blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey);
if (blobInfo == null)
; // handle missing blob here
application.addResource(this);
}
 
@Override
public String getMIMEType() {
return blobInfo.getContentType();
}
 
@Override
public DownloadStream getStream() {
InputStream inputStream;
try {
inputStream = new BlobstoreInputStream(blobKey);
} catch (IOException e) {
return null;
}
 
final DownloadStream ds = new DownloadStream(inputStream,
getMIMEType(), getFilename());
ds.setBufferSize(getBufferSize());
ds.setCacheTime(cacheTime);
return ds;
}
 
// other methods skipped ...
}

Vaadin refresh parameter handler

@Override
public void handleParameters(Map<String, String[]> parameters) {
if(parameters.containsKey("refresh")) {
// refresh the layout ...
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
final Map blobs = blobstoreService.getUploadedBlobs(req);
final BlobKey blobKey = blobs.get("myImage");
final String uploadKey = req.getParameter("uploadKey");
 
final Objectify ofy = ObjectifyService.begin();
final User user = ofy.query(User.class).filter("uploadKey", uploadKey).get();
user.setBlobKey(blobKey.getKeyString());
ofy.put(user);
 
resp.sendRedirect("/?refresh");
}

Get Http Request Object

Vaadin "transaction" equals "http request".

 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
     * Vaadin "transaction" equals "http request".  This method fires for all servlets
     * in the session.
     */
    private void attachHttpRequestListener() {
        getContext().addTransactionListener(new TransactionListener() {
            private static final long serialVersionUID = -2365336986843262181L;

            public void transactionStart(Application application,
                    Object transactionData) {
                ((LocalizedSystemMessages)getSystemMessages()).setThreadLocale(getLocale());
                current.set(CompetitionApplication.this);  // make the application available via ThreadLocal
                HttpServletRequest request = (HttpServletRequest)transactionData;
                request.getSession(true).setMaxInactiveInterval(3600);
            }
            
            public void transactionEnd(Application application,
                    Object transactionData) {
                // Transaction listener gets fired for all (Http) sessions
                // of Vaadin applications, checking to be this one.
                if (application == CompetitionApplication.this) {
                    closeHibernateSession();
                }
            }
        });
    }