a7aa1a3785d1568ec50504b57d24db05aed19460
[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 java.util.logging.Logger;
23 import javax.inject.Inject;
24
25 import org.apache.wicket.PageParameters;
26 import org.apache.wicket.markup.html.WebMarkupContainer;
27 import org.apache.wicket.markup.html.basic.Label;
28 import org.apache.wicket.markup.html.image.Image;
29 import org.apache.wicket.markup.html.link.Link;
30 import org.apache.wicket.markup.repeater.RepeatingView;
31 import org.apache.wicket.resource.ByteArrayResource;
32 import org.wamblee.photos.model.Album;
33 import org.wamblee.photos.model.Photo;
34 import org.wamblee.photos.model.PhotoEntry;
35 import org.wamblee.photos.model.plumbing.AllPhotos;
36 import org.wamblee.photos.model.plumbing.AuthorizedPhotos;
37 import org.wamblee.security.authentication.User;
38 import org.wamblee.security.authentication.UserAdministration;
39
40 /**
41  * Homepage
42  */
43 public class HomePage extends BasePage {
44
45     private static final Logger LOGGER = Logger.getLogger(HomePage.class.getName());
46
47     private static final long serialVersionUID = 1L;
48
49     @Inject
50     private transient User user;
51
52     @Inject
53     private transient UserAdministration userAdmin;
54
55     // TODO the state should be stored as a path.
56
57     @Inject
58     @AllPhotos
59     private transient Album album;
60
61     @Inject
62     @AuthorizedPhotos
63     private transient Album authorized;
64
65     private class SerializableEntryLink extends Link {
66
67         private String path;
68
69         public SerializableEntryLink(String aId, String aPath) {
70             super(aId);
71             path = aPath;
72         }
73
74         @Override
75         public void onClick() {
76             System.out.println("Entry " + path + " was clicked");
77             PageParameters pars = new PageParameters();
78             pars.put("path", path);
79             setResponsePage(HomePage.class, pars);
80         }
81     }
82
83     private String path;
84
85     /**
86      * Constructor that is invoked when page is invoked without a session.
87      *
88      * @param parameters Page parameters
89      */
90     public HomePage(final PageParameters parameters) throws Exception {
91         super();
92
93         path = parameters.getString("path", "/");
94         if (!path.startsWith("/")) {
95             info("Invalid album '" + path + "', showing root album instead");
96             path = "/";
97         }
98
99         add(new Label("message", "Hello world!"));
100
101         System.out.println("Currently logged in user: " + user);
102
103         List<String> usernames = userAdmin.getUsers();
104         System.out.println("All user names: " + usernames);
105
106         usernames = userAdmin.getUsers("public");
107         System.out.println("Users in group public: " + usernames);
108
109         System.out.println("Entries: " + album.size());
110         for (int i = 0; i < album.size(); i++) {
111             PhotoEntry entry = album.getEntry(i);
112             System.out.println("Entry " + i + " " + entry.getId() + " " + entry.getPath());
113         }
114
115         System.out.println("Authorized Entries: " + authorized.size());
116         for (int i = 0; i < authorized.size(); i++) {
117             PhotoEntry entry = authorized.getEntry(i);
118             System.out.println("Entry " + i + " " + entry.getId() + " " + entry.getPath());
119         }
120
121         PhotoEntry current = authorized.getEntry(path);
122
123         if (current instanceof Photo) {
124             throw new RuntimeException("Photo entry viewing not yet implemented");
125         }
126
127         Album album = (Album) current;
128
129         int ientry = 0;
130         int irow = 0;
131         RepeatingView row = new RepeatingView("row");
132         add(row);
133         while (irow < 5 && ientry < album.size()) {
134             int icolumn = 0;
135             WebMarkupContainer columns = new WebMarkupContainer(row.newChildId());
136             row.add(columns);
137             RepeatingView column = new RepeatingView("column");
138             columns.add(column);
139             while (icolumn < 5 && ientry < album.size()) {
140                 WebMarkupContainer thumbnail = new WebMarkupContainer(column.newChildId());
141                 column.add(thumbnail);
142
143                 final PhotoEntry entry = album.getEntry(ientry);
144                 Link link = new SerializableEntryLink("thumbnail", entry.getPath());
145                 thumbnail.add(link);
146                 ImageData data = getData(entry);
147
148                 // TODO very inefficient. all data is loaded when generating the page.
149                 link.add(new Image("image", new ByteArrayResource(data.getContentType(), data.getData())));
150
151                 link.add(new Label("name", album.getEntry(ientry).getId()));
152                 icolumn++;
153                 ientry++;
154             }
155             irow++;
156         }
157     }
158
159     public static final class ImageData {
160         private String contentType;
161         private byte[] data;
162
163         public ImageData(String aContentType, byte[] aData) {
164             contentType = aContentType;
165             data = aData;
166         }
167
168         public String getContentType() {
169             return contentType;
170         }
171
172         public byte[] getData() {
173             return data;
174         }
175     }
176
177     private ImageData getData(PhotoEntry aEntry) {
178         if (aEntry instanceof Photo) {
179             return getData((Photo) aEntry);
180         } else if (aEntry instanceof Album) {
181             return getData((Album) aEntry);
182         } else {
183             throw new RuntimeException("Unsupported type " + aEntry.getClass().getName());
184         }
185     }
186
187     private ImageData getData(Photo aPhoto) {
188         try (InputStream is = aPhoto.getThumbNail()) {
189             return new ImageData("image/jpeg", getBytes(is));
190         }
191         catch (IOException e) {
192             // to improve.
193             throw new RuntimeException("Cannot read photo", e);
194         }
195     }
196
197     private byte[] getBytes(InputStream is) throws IOException {
198         ByteArrayOutputStream bos = new ByteArrayOutputStream();
199         byte[] block = new byte[1024];
200         int n = is.read(block);
201         while (n > 0) {
202             bos.write(block, 0, n);
203             n = is.read(block);
204         }
205         return bos.toByteArray();
206     }
207
208     private ImageData getData(Album aAlbum) {
209         try (InputStream is = getClass().getResourceAsStream("folder.png")) {
210             return new ImageData("image/png", getBytes(is));
211         }
212         catch (IOException e) {
213             throw new RuntimeException("Cannot read album jpg", e);
214         }
215     }
216 }