/** * Resolves bean's name from bean annotation or type name. May be used for resolving bean name * of base type during registration of bean subclass. */ public String resolveBeanName(final Class type, final boolean useLongTypeName) { PetiteBean petiteBean = ((Class<?>)type).getAnnotation(PetiteBean.class); String name = null; if (petiteBean != null) { name = petiteBean.value().trim(); } if ((name == null) || (name.length() == 0)) { if (useLongTypeName) { name = type.getName(); } else { name = StringUtil.uncapitalize(type.getSimpleName()); } } return name; }
/** * Reads action path from class. If the class is annotated with {@link MadvocAction} annotation, * class action path will be read from annotation value. Otherwise, action class path will be built from the * class name. This is done by removing the package name and the last contained word * (if there is more then one) from the class name. Such name is finally uncapitalized. */ protected String[] readClassActionPath(final Class actionClass) { // read class annotation MadvocAction madvocActionAnnotation = ((Class<?>)actionClass).getAnnotation(MadvocAction.class); String classActionPath = madvocActionAnnotation != null ? madvocActionAnnotation.value().trim() : null; if (StringUtil.isEmpty(classActionPath)) { classActionPath = null; } String actionClassName = actionClass.getSimpleName(); actionClassName = StringUtil.uncapitalize(actionClassName); actionClassName = MadvocUtil.stripLastCamelWord(actionClassName); // removes 'Action' from the class name if (classActionPath == null) { classActionPath = actionClassName; } return ArraysUtil.array(actionClassName, classActionPath); }
@Override public void process(final StringBuilder out) { DbEntityDescriptor ded = entityName != null ? lookupName(entityName) : lookupType(entityType); StringBuilder col = new StringBuilder(); StringBuilder val = new StringBuilder(); DbEntityColumnDescriptor[] decList = ded.getColumnDescriptors(); String typeName = StringUtil.uncapitalize(ded.getEntityName()); int size = 0; for (DbEntityColumnDescriptor dec : decList) { if (dec.isId() && !defaultIsUpdateablePrimaryKey) { continue; } String property = dec.getPropertyName(); Object value = BeanUtil.declared.getProperty(data, property); if (value == null) { continue; } if (size > 0) { col.append(',').append(' '); val.append(',').append(' '); } size++; col.append(dec.getColumnNameForQuery()); final String propertyName = typeName + '.' + property; defineParameter(val, propertyName, value, dec); } out.append("insert into ").append(ded.getTableNameForQuery()).append(" (") .append(col).append(") values (").append(val).append(')'); }
/** * Builds default field references. */ public BeanReferences buildDefaultReference(final PropertyDescriptor propertyDescriptor) { final PetiteReferenceType[] lookupReferences = petiteConfig.getLookupReferences(); final String[] references = new String[lookupReferences.length]; for (int i = 0; i < references.length; i++) { switch (lookupReferences[i]) { case NAME: references[i] = propertyDescriptor.getName(); break; case TYPE_SHORT_NAME: references[i] = StringUtil.uncapitalize(propertyDescriptor.getType().getSimpleName()); break; case TYPE_FULL_NAME: references[i] = propertyDescriptor.getType().getName(); break; } } return BeanReferences.of(references); }
break; case TYPE_SHORT_NAME: ref[i] = StringUtil.uncapitalize(paramTypes[j].getSimpleName()); break; case TYPE_FULL_NAME:
String typeName = StringUtil.uncapitalize(ded.getEntityName());
String typeName = StringUtil.uncapitalize(ded.getEntityName());
@Override public void inject(final ActionRequest actionRequest, final Targets targets) { final HttpServletRequest servletRequest = actionRequest.getHttpServletRequest(); targets.forEachTargetAndIn(this, (target, in) -> { Object value = null; if (in.type() == Cookie.class) { // get single cookie final String cookieName = StringUtil.uncapitalize(in.name()); value = ServletUtil.getCookie(servletRequest, cookieName); } else if (in.type().isArray()) { if (in.type().getComponentType().equals(Cookie.class)) { if (StringUtil.isEmpty(in.name())) { // get all cookies value = servletRequest.getCookies(); } else { // get all cookies by name value = ServletUtil.getAllCookies(servletRequest, in.name()); } } } if (value != null) { target.writeValue(in, value, true); } }); }
@Test void testCapitalize() { assertEquals("F", StringUtil.capitalize("f")); assertEquals("Foo", StringUtil.capitalize("foo")); assertEquals("Foo", StringUtil.capitalize("Foo")); assertEquals("", StringUtil.capitalize("")); assertEquals("f", StringUtil.uncapitalize("F")); assertEquals("foo", StringUtil.uncapitalize("foo")); assertEquals("foo", StringUtil.uncapitalize("Foo")); assertEquals("uRL", StringUtil.uncapitalize("URL")); assertEquals("", StringUtil.uncapitalize("")); assertEquals("f", StringUtil.decapitalize("F")); assertEquals("foo", StringUtil.decapitalize("foo")); assertEquals("foo", StringUtil.decapitalize("Foo")); assertEquals("URL", StringUtil.decapitalize("URL")); assertEquals("", StringUtil.decapitalize("")); }
/** * Creates SELECT criteria for the entity matched by foreign key. * Foreign key is created by concatenating foreign table name and column name. */ public DbSqlBuilder findForeign(final Class entity, final Object value) { final String tableRef = createTableRefName(entity); final DbEntityDescriptor dedFk = entityManager.lookupType(value.getClass()); final String tableName = dbOomConfig.getTableNames().convertTableNameToEntityName(dedFk.getTableName()); final String columnName = dbOomConfig.getColumnNames().convertColumnNameToPropertyName(dedFk.getIdColumnName()); final String fkColumn = uncapitalize(tableName) + capitalize(columnName); final Object idValue = BeanUtil.pojo.getProperty(value, dedFk.getIdPropertyName()); return sql().$(SELECT).column(tableRef).$(FROM).table(entity, tableRef).$(WHERE).ref(tableRef, fkColumn).$(EQUALS).columnValue(idValue); }
@Test void testInjection() { WebApp webapp = new WebApp(); webapp.start(); PetiteContainer madpc = webapp.madvocContainer().getPetiteContainer(); String baseName = StringUtil.uncapitalize(FooBean.class.getSimpleName()); madpc.defineParameter("foo", "1"); madpc.defineParameter(baseName + ".integer", "173"); madpc.defineParameter(baseName + ".string", "jodd"); madpc.defineParameter(baseName, "huh"); ParamsScope paramsScope = new ParamsScope(); BeanUtil.declared.setProperty(paramsScope, "madpc", madpc); FooBean fooBean = new FooBean(); paramsScope.inject(new Targets(fooBean, null)); assertEquals(173, fooBean.getInteger().intValue()); assertEquals("jodd", fooBean.getString()); } }
/** * Builds default field references. */ protected String[] fieldDefaultReferences(Field field) { PetiteReference[] lookupReferences = petiteConfig.getLookupReferences(); String[] references = new String[lookupReferences.length]; for (int i = 0; i < references.length; i++) { switch (lookupReferences[i]) { case NAME: references[i] = field.getName(); break; case TYPE_SHORT_NAME: references[i] = StringUtil.uncapitalize(field.getType().getSimpleName()); break; case TYPE_FULL_NAME: references[i] = field.getType().getName(); break; } } return references; }
/** * Resolves bean's name from bean annotation or type name. May be used for resolving bean name * of base type during registration of bean subclass. */ public String resolveBeanName(final Class type, final boolean useLongTypeName) { PetiteBean petiteBean = ((Class<?>)type).getAnnotation(PetiteBean.class); String name = null; if (petiteBean != null) { name = petiteBean.value().trim(); } if ((name == null) || (name.length() == 0)) { if (useLongTypeName) { name = type.getName(); } else { name = StringUtil.uncapitalize(type.getSimpleName()); } } return name; }
/** * Resolves bean's name from bean annotation or type name. May be used for resolving bean name * of base type during registration of bean subclass. */ public static String resolveBeanName(Class type, boolean useLongTypeName) { PetiteBean petiteBean = ((Class<?>)type).getAnnotation(PetiteBean.class); String name = null; if (petiteBean != null) { name = petiteBean.value().trim(); } if ((name == null) || (name.length() == 0)) { if (useLongTypeName) { name = type.getName(); } else { name = StringUtil.uncapitalize(type.getSimpleName()); } } return name; }
public void inject(Object target, ServletContext servletContext) { ScopeData.In[] injectData = lookupInData(target.getClass()); if (injectData == null) { return; } for (ScopeData.In in : injectData) { Class fieldType = in.type; Object value = null; if (fieldType.equals(ServletContext.class)) { // raw servlet type value = servletContext; } else if (in.name.equals(CONTEXT_MAP)) { // names value = new HttpServletContextMap(servletContext); } else if (in.name.startsWith(CONTEXT_NAME)) { value = BeanUtil.getDeclaredProperty(servletContext, StringUtil.uncapitalize(in.name.substring(CONTEXT_NAME.length()))); } if (value != null) { String property = in.target != null ? in.target : in.name; BeanUtil.setDeclaredProperty(target, property, value); } } }
@Override public void process(StringBuilder out) { DbEntityDescriptor ded = entityName != null ? lookupName(entityName) : lookupType(entityType); StringBuilder col = new StringBuilder(); StringBuilder val = new StringBuilder(); DbEntityColumnDescriptor[] decList = ded.getColumnDescriptors(); String typeName = StringUtil.uncapitalize(ded.getEntityName()); int size = 0; for (DbEntityColumnDescriptor dec : decList) { String property = dec.getPropertyName(); Object value = BeanUtil.getDeclaredProperty(data, property); if (value == null) { continue; } if (size > 0) { col.append(',').append(' '); val.append(',').append(' '); } size++; col.append(dec.getColumnName()); String propertyName = typeName + '.' + property; defineParameter(val, propertyName, value, dec); } out.append("insert into ").append(ded.getTableName()).append(" (") .append(col).append(") values (").append(val).append(')'); }
/** * Reads action path from class. If the class is annotated with {@link MadvocAction} annotation, * class action path will be read from annotation value. Otherwise, action class path will be built from the * class name. This is done by removing the package name and the last contained word * (if there is more then one) from the class name. Such name is finally uncapitalized. * <p> * If this method returns <code>null</code> class will be ignored. */ protected String readClassActionPath(Class actionClass) { // read annotation MadvocAction madvocActionAnnotation = ((Class<?>)actionClass).getAnnotation(MadvocAction.class); String classActionPath = madvocActionAnnotation != null ? madvocActionAnnotation.value().trim() : null; if (StringUtil.isEmpty(classActionPath)) { classActionPath = null; } String name = actionClass.getSimpleName(); name = StringUtil.uncapitalize(name); name = MadvocUtil.stripLastCamelWord(name); // decide if (classActionPath == null) { classActionPath = name; } else { classActionPath = StringUtil.replace(classActionPath, REPL_CLASS, name); } return classActionPath; }
/** * Builds default field references. */ public BeanReferences buildDefaultReference(final PropertyDescriptor propertyDescriptor) { final PetiteReferenceType[] lookupReferences = petiteConfig.getLookupReferences(); final String[] references = new String[lookupReferences.length]; for (int i = 0; i < references.length; i++) { switch (lookupReferences[i]) { case NAME: references[i] = propertyDescriptor.getName(); break; case TYPE_SHORT_NAME: references[i] = StringUtil.uncapitalize(propertyDescriptor.getType().getSimpleName()); break; case TYPE_FULL_NAME: references[i] = propertyDescriptor.getType().getName(); break; } } return BeanReferences.of(references); }
/** * Creates SELECT criteria for the entity matched by foreign key. * Foreign key is created by concatenating foreign table name and column name. */ public DbSqlBuilder findForeign(final Class entity, final Object value) { final String tableRef = createTableRefName(entity); final DbEntityDescriptor dedFk = entityManager.lookupType(value.getClass()); final String tableName = dbOomConfig.getTableNames().convertTableNameToEntityName(dedFk.getTableName()); final String columnName = dbOomConfig.getColumnNames().convertColumnNameToPropertyName(dedFk.getIdColumnName()); final String fkColumn = uncapitalize(tableName) + capitalize(columnName); final Object idValue = BeanUtil.pojo.getProperty(value, dedFk.getIdPropertyName()); return sql().$(SELECT).column(tableRef).$(FROM).table(entity, tableRef).$(WHERE).ref(tableRef, fkColumn).$(EQUALS).columnValue(idValue); }
/** * Creates SELECT criteria for the entity matched by foreign key. * Foreign key is created by concatenating foreign table name and column name. */ public static DbSqlBuilder findForeign(Class entity, Object value) { String tableRef = createTableRefName(entity); DbOomManager dbOomManager = DbOomManager.getInstance(); DbEntityDescriptor dedFk = dbOomManager.lookupType(value.getClass()); String tableName = dbOomManager.getTableNames().convertTableNameToEntityName(dedFk.getTableName()); String columnName = dbOomManager.getColumnNames().convertColumnNameToPropertyName(dedFk.getIdColumnName()); String fkColumn = uncapitalize(tableName) + capitalize(columnName); Object idValue = BeanUtil.getProperty(value, dedFk.getIdPropertyName()); return sql()._(SELECT).column(tableRef)._(FROM).table(entity, tableRef)._(WHERE).ref(tableRef, fkColumn)._(EQUALS).columnValue(idValue); }