View Javadoc
1   /**
2    * Copyright 2007 Björn Voß
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.oxclient.service.client.module;
17  
18  import java.sql.Timestamp;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import net.sf.oval.constraint.NotNull;
24  import net.sf.oval.guard.Guarded;
25  import net.sf.oxclient.beans.Folder;
26  import net.sf.oxclient.beans.OXBean;
27  import net.sf.oxclient.service.AbsGroupwareModule;
28  import net.sf.oxclient.service.client.IOXSession;
29  
30  import org.springframework.beans.BeanUtils;
31  
32  /**
33   * @author Björn Voß
34   *
35   */
36  @Guarded
37  public abstract class AbsOXModule<T extends OXBean>
38  		extends AbsGroupwareModule<T, Folder> {
39  
40  	private Folder defaultFolder = null;
41  
42  	/**
43  	 * @param moduleName
44  	 * @param entityClass
45  	 */
46  	protected AbsOXModule(final String moduleName, final Class<T> entityClass) {
47  		super(moduleName, entityClass);
48  	}
49  
50  	/* (non-Javadoc)
51  	 * @see net.sf.oxclient.service.AbsGroupwareModule#create(java.lang.Object)
52  	 */
53  	@SuppressWarnings("unchecked")
54  	@Override
55  	public T create(@NotNull final Folder parent) {
56  		final T result = (T) BeanUtils.instantiateClass(getEntityClass());
57  		result.setCreated(new Timestamp(System.currentTimeMillis()));
58  		result.setParent(parent);
59  		return result;
60  	}
61  
62  	/* (non-Javadoc)
63  	 * @see net.sf.oxclient.service.AbsGroupwareModule#list(java.lang.Object)
64  	 */
65  	@Override
66  	public List<T> list(final Folder folder) {
67  		final List<T> result = super.list(folder);
68  		for (final T entity : result) {
69  			entity.setParent(folder);
70  		}
71  		return result;
72  	}
73  
74  	@Override
75  	public Map<String, String> getQueryParameter(@NotNull final String objectId) {
76  		final Map<String, String> result = new HashMap<String, String>();
77  		result.put("action", "get");
78  		result.put("id", objectId);
79  		return result;
80  	}
81  
82  	@Override
83  	public IOXSession getSession() {
84  		return (IOXSession) super.getSession();
85  	}
86  
87  	public Folder getDefaultFolder() {
88  		if (this.defaultFolder == null) {
89  			this.defaultFolder = loadDefaultFolder();
90  		}
91  		return this.defaultFolder;
92  	}
93  
94  	protected Folder loadDefaultFolder() {
95  		final List<Folder> privateFolders =
96  				getSession().getFolderModule().getPrivateFolders();
97  		for (Folder folder : privateFolders) {
98  			if (folder.isDefaultFolder()
99  				&& getName().equals(folder.getModule())) {
100 				return folder;
101 			}
102 		}
103 		final StringBuilder sb = new StringBuilder();
104 		sb.append("No default folder for module \"");
105 		sb.append(getName());
106 		sb.append("\" found");
107 		throw new IllegalStateException(sb.toString());
108 	}
109 
110 }