/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package org.opentides.controller;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.opentides.bean.SystemCodes;
import org.opentides.bean.UserDefinable;
import org.opentides.bean.UserDefinedField;
import org.opentides.bean.UserDefinedRecord;
import org.opentides.editor.ClassEditor;
import org.opentides.editor.SystemCodeEditor;
import org.opentides.service.UserDefinedFieldService;
import org.opentides.util.CacheUtil;
import org.scannotation.AnnotationDB;
import org.scannotation.ClasspathUrlFinder;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;
/**
* This is the controller class for UserDefinedField. Auto generated by high
* tides.
*
* @author hightides
*/
public class UserDefinedFieldController extends
BaseCrudController<UserDefinedField> {
private static final List<String> userFields = new ArrayList<String>();
@SuppressWarnings("rawtypes")
private static final Map<Class, String> udfClasses = new HashMap<Class, String>();
private static String persistenceFile = "META-INF/persistence.xml";
/**
* Post construct method for initializing user fields and user classes.
*/
@PostConstruct
public void init() {
initializeUserFields();
initializeUdfClasses();
}
/**
* This method initialize the userFields by listing all available fields in
* UserDefinedRecord.
*/
@SuppressWarnings("rawtypes")
private static void initializeUserFields() {
Class udr = UserDefinedRecord.class;
Method[] methods = udr.getDeclaredMethods();
for (Method method : methods) {
// add all getter methods except for those starting with
// getEntityxxx
String name = method.getName();
String fieldName = name.substring(3, 4).toLowerCase()
+ name.substring(4);
try {
Field field = udr.getDeclaredField(fieldName);
if ((name.startsWith("getString")
|| name.startsWith("getBoolean")
|| name.startsWith("getDropdown") || name
.startsWith("getDate"))
&& !Modifier.isTransient(field.getModifiers()))
userFields.add(fieldName);
} catch (NoSuchFieldException e) {
}
}
}
/**
* This method initialize the udfClasses by checking all classes that
* implements UserDefinedRecord.
*
* @param persistenceFile
*/
@SuppressWarnings("rawtypes")
private static void initializeUdfClasses() {
try {
URL[] urls = ClasspathUrlFinder.findResourceBases(persistenceFile);
List<URL> toAdd = new ArrayList<URL>();
List<URL> finalUrls = new ArrayList<URL>();
for(URL url : urls) {
String file = url.getFile();
if(!file.startsWith("file:")) {
file = "file:" + file;
}
if("zip".equals(url.getProtocol())) {
toAdd.add(new URL("jar", url.getHost(), url.getPort(), file));
} else {
toAdd.add(url);
}
}
finalUrls.addAll(toAdd);
AnnotationDB db = new AnnotationDB();
db.scanArchives(finalUrls.toArray(new URL[finalUrls.size()]));
Set<String> entityClasses = db.getAnnotationIndex().get(
javax.persistence.Entity.class.getName());
for (String entityClass : entityClasses) {
Class clazz = Class.forName(entityClass);
if (UserDefinable.class.isAssignableFrom(clazz))
udfClasses.put(clazz, clazz.getSimpleName());
}
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Failed to load user defined fields." + ex);
}
}
/*
* (non-Javadoc)
*
* @see
* org.opentides.controller.BaseCrudController#referenceData(javax.servlet
* .http.HttpServletRequest, java.lang.Object,
* org.springframework.validation.Errors)
*/
@SuppressWarnings("rawtypes")
@Override
protected Map referenceData(HttpServletRequest request, Object command,
Errors errors) throws Exception {
Map<String, Object> model = new HashMap<String, Object>();
// get all classes that implement the interface UserDefinable
model.put("clazzList", udfClasses);
// get list of fields in UserDefinedRecord.
model.put("userFieldList", userFields);
return model;
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.web.servlet.mvc.BaseCommandController#initBinder(
* javax.servlet.http.HttpServletRequest,
* org.springframework.web.bind.ServletRequestDataBinder)
*/
@Override
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(SystemCodes.class, new SystemCodeEditor(
this.getSystemCodesService()));
binder.registerCustomEditor(Class.class, new ClassEditor());
}
/*
* (non-Javadoc)
*
* @see
* org.opentides.controller.BaseCrudController#postCreateAction(javax.servlet
* .http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
* org.opentides.bean.BaseEntity,
* org.springframework.validation.BindException)
*/
@Override
protected void postCreateAction(HttpServletRequest request,
HttpServletResponse response, UserDefinedField command,
BindException errors) {
refreshUdfFieldsList();
CacheUtil.clearSearchCache(command.getClazz());
}
/*
* (non-Javadoc)
*
* @see
* org.opentides.controller.BaseCrudController#postDeleteAction(javax.servlet
* .http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
* org.springframework.validation.BindException, java.lang.String)
*/
@Override
protected void postDeleteAction(HttpServletRequest request,
HttpServletResponse response, BindException errors, String id) {
refreshUdfFieldsList();
}
/*
* (non-Javadoc)
*
* @see
* org.opentides.controller.BaseCrudController#postUpdateAction(javax.servlet
* .http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
* org.opentides.bean.BaseEntity,
* org.springframework.validation.BindException)
*/
@Override
protected void postUpdateAction(HttpServletRequest request,
HttpServletResponse response, UserDefinedField command,
BindException errors) {
refreshUdfFieldsList();
CacheUtil.clearSearchCache(command.getClazz());
}
/**
* Refresh udfMap property of UserDefinedRecord
*/
@SuppressWarnings("rawtypes")
private void refreshUdfFieldsList() {
if (udfClasses.keySet() != null)
for (Class clazz : udfClasses.keySet()) {
List<UserDefinedField> udfList = ((UserDefinedFieldService) this
.getService()).findMetaByClass(clazz.getName());
UserDefinedRecord.setUdf(clazz, udfList);
}
}
/**
* Setter method for persistenceFile.
*
* @param persistenceFile
* the persistenceFile to set
*/
public void setPersistenceFile(String persistenceFile) {
UserDefinedFieldController.persistenceFile = persistenceFile;
}
// -- Start custom codes. Do not delete this comment line.
// -- End custom codes. Do not delete this comment line.
}