Java 7 is now used.
[photos] / src / main / java / org / wamblee / photos / wicket / HomePage.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.photos.wicket;
17
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.List;
22 import javax.inject.Inject;
23
24 import org.apache.wicket.PageParameters;
25 import org.apache.wicket.markup.html.WebMarkupContainer;
26 import org.apache.wicket.markup.html.basic.Label;
27 import org.apache.wicket.markup.html.image.Image;
28 import org.apache.wicket.markup.html.link.Link;
29 import org.apache.wicket.markup.repeater.RepeatingView;
30 import org.apache.wicket.resource.ByteArrayResource;
31 import org.wamblee.photos.model.Album;
32 import org.wamblee.photos.model.Photo;
33 import org.wamblee.photos.model.PhotoEntry;
34 import org.wamblee.photos.model.plumbing.AllPhotos;
35 import org.wamblee.photos.model.plumbing.AuthorizedPhotos;
36 import org.wamblee.security.authentication.User;
37 import org.wamblee.security.authentication.UserAdministration;
38
39 /**
40  * Homepage
41  */
42 public class HomePage extends BasePage {
43
44     private static final long serialVersionUID = 1L;
45
46     @Inject
47     private transient User user;
48
49     @Inject
50     private transient UserAdministration userAdmin;
51
52     // TODO the state should be stored as a path.
53
54     @Inject
55     @AllPhotos
56     private transient Album album;
57
58     @Inject
59     @AuthorizedPhotos
60     private transient Album authorized;
61
62     private class SerializableEntryLink extends Link {
63
64         private String path;
65
66         public SerializableEntryLink(String aId, String aPath) {
67             super(aId);
68             path = aPath;
69         }
70
71         @Override
72         public void onClick() {
73             System.out.println("Entry " + path + " was clicked");
74         }
75     }
76
77     /**
78      * Constructor that is invoked when page is invoked without a session.
79      *
80      * @param parameters Page parameters
81      */
82     public HomePage(final PageParameters parameters) throws Exception {
83         super();
84         add(new Label("message", "Hello world!"));
85
86         System.out.println("Currently logged in user: " + user);
87
88         List<String> usernames = userAdmin.getUsers();
89         System.out.println("All user names: " + usernames);
90
91         usernames = userAdmin.getUsers("public");
92         System.out.println("Users in group public: " + usernames);
93
94         System.out.println("Entries: " + album.size());
95         for (int i = 0; i < album.size(); i++) {
96             PhotoEntry entry = album.getEntry(i);
97             System.out.println("Entry " + i + " " + entry.getId() + " " + entry.getPath());
98         }
99
100         System.out.println("Authorized Entries: " + authorized.size());
101         for (int i = 0; i < authorized.size(); i++) {
102             PhotoEntry entry = authorized.getEntry(i);
103             System.out.println("Entry " + i + " " + entry.getId() + " " + entry.getPath());
104         }
105
106         int ientry = 0;
107         int irow = 0;
108         RepeatingView row = new RepeatingView("row");
109         add(row);
110         while (irow < 5 && ientry < authorized.size()) {
111             int icolumn = 0;
112             WebMarkupContainer columns = new WebMarkupContainer(row.newChildId());
113             row.add(columns);
114             RepeatingView column = new RepeatingView("column");
115             columns.add(column);
116             while (icolumn < 5 && ientry < authorized.size()) {
117                 WebMarkupContainer thumbnail = new WebMarkupContainer(column.newChildId());
118                 column.add(thumbnail);
119
120                 final PhotoEntry entry = authorized.getEntry(ientry);
121                 Link link = new SerializableEntryLink("thumbnail", entry.getPath());
122                 thumbnail.add(link);
123                 ImageData data = getData(entry);
124
125                 // TODO very inefficient. all data is loaded when generating the page.
126                 link.add(new Image("image", new ByteArrayResource(data.getContentType(), data.getData())));
127
128                 link.add(new Label("name", authorized.getEntry(ientry).getId()));
129                 icolumn++;
130                 ientry++;
131             }
132             irow++;
133         }
134     }
135
136     public static final class ImageData {
137         private String contentType;
138         private byte[] data;
139
140         public ImageData(String aContentType, byte[] aData) {
141             contentType = aContentType;
142             data = aData;
143         }
144
145         public String getContentType() {
146             return contentType;
147         }
148
149         public byte[] getData() {
150             return data;
151         }
152     }
153
154     private ImageData getData(PhotoEntry aEntry) {
155         if (aEntry instanceof Photo) {
156             return getData((Photo) aEntry);
157         } else if (aEntry instanceof Album) {
158             return getData((Album) aEntry);
159         } else {
160             throw new RuntimeException("Unsupported type " + aEntry.getClass().getName());
161         }
162     }
163
164     private ImageData getData(Photo aPhoto) {
165         try (InputStream is = aPhoto.getThumbNail()) {
166             return new ImageData("image/jpeg", getBytes(is));
167         }
168         catch (IOException e) {
169             // to improve.
170             throw new RuntimeException("Cannot read photo", e);
171         }
172     }
173
174     private byte[] getBytes(InputStream is) throws IOException {
175         ByteArrayOutputStream bos = new ByteArrayOutputStream();
176         byte[] block = new byte[1024];
177         int n = is.read(block);
178         while (n > 0) {
179             bos.write(block, 0, n);
180             n = is.read(block);
181         }
182         return bos.toByteArray();
183     }
184
185     private ImageData getData(Album aAlbum) {
186         try (InputStream is = getClass().getResourceAsStream("folder.png")) {
187             return new ImageData("image/png", getBytes(is));
188         }
189         catch (IOException e) {
190             throw new RuntimeException("Cannot read album jpg", e);
191         }
192     }
193 }