Trace:
Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Next revision Both sides next revision | ||
jvx:code_snippets [2018/02/06 10:24] admin |
jvx:code_snippets [2018/02/06 10:30] admin |
||
---|---|---|---|
Line 1583: | Line 1583: | ||
,(select E.EDUCATION from EDUCATIONS E where E.ID = CE.EDUC_ID) EDUCATION | ,(select E.EDUCATION from EDUCATIONS E where E.ID = CE.EDUC_ID) EDUCATION | ||
from CONT_EDUC CE | from CONT_EDUC CE | ||
+ | </file> | ||
+ | |||
+ | ==== Encrypt passwords ==== | ||
+ | |||
+ | We don't encrypt passwords on client-side and not in the database. We use our middleware for that. It's super easy with server-side triggers/events: | ||
+ | |||
+ | Add the method: | ||
+ | |||
+ | <file java> | ||
+ | /** | ||
+ | * Encrypts a password, if password is changed. | ||
+ | * | ||
+ | * @param pEvent the storage event | ||
+ | * @throws Exception if encryption or data change fails | ||
+ | */ | ||
+ | public void doEncryptPwd(StorageEvent pEvent) throws Exception | ||
+ | { | ||
+ | IBean bn = pEvent.getNew(); | ||
+ | |||
+ | String sNew = (String)bn.get("PASSWORD"); | ||
+ | String sOld; | ||
+ | |||
+ | IBean bnOld = pEvent.getOld(); | ||
+ | |||
+ | if (bnOld != null) | ||
+ | { | ||
+ | sOld = (String)bnOld.get("PASSWORD"); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | sOld = null; | ||
+ | } | ||
+ | |||
+ | if (!CommonUtil.equals(sOld, sNew)) | ||
+ | { | ||
+ | //use the configuration of the selected application! | ||
+ | bn.put("PASSWORD", AbstractSecurityManager.getEncryptedPassword( | ||
+ | SessionContext.getCurrentSession().getConfig(), sNew)); | ||
+ | } | ||
+ | } | ||
+ | </file> | ||
+ | |||
+ | to your life-cycle object e.g. Session.java. | ||
+ | |||
+ | Add an event to your storage | ||
+ | |||
+ | <file java> | ||
+ | //example storage | ||
+ | dbsUser = new DBStorage(); | ||
+ | dbsUser.setDBAccess(getDBAccess()); | ||
+ | dbsUser.setFromClause("USER"); | ||
+ | dbsUser.open(); | ||
+ | |||
+ | dbsUser.eventBeforeInsert().addListener(this, "doEncryptPwd"); | ||
+ | dbsUser.eventBeforeUpdate().addListener(this, "doEncryptPwd"); | ||
+ | </file> | ||
+ | |||
+ | As last step, you need the password algorithm in your config.xml: | ||
+ | |||
+ | <file xml> | ||
+ | <?xml version="1.0" encoding="UTF-8"?> | ||
+ | |||
+ | <application> | ||
+ | <securitymanager> | ||
+ | <passwordalgorithm>SHA</passwordalgorithm> | ||
+ | </securitymanager> | ||
+ | ... | ||
+ | </application> | ||
+ | </file> | ||
+ | |||
+ | Choose one of the following algorithm: MD2, MD4, MD5, SHA, SHA-256, SHA-384, SHA-512. | ||
+ | |||
+ | ==== Fetch data with DBStorage ==== | ||
+ | |||
+ | Sometimes you want to use DBAccess and DBStorage without JVx server and life-cycle objects. This is very easy because JVx was designed as library. Simply use DBStorage and DBAccess in your Servlets, applications, ... | ||
+ | |||
+ | The following example uses DBAccess and DBStorage to check if a user exists in the database and if an email address doesn't exist. | ||
+ | |||
+ | <file java> | ||
+ | dba = DBAccess.getDBAccess("jdbc:oracle:thin:@localhost:1521:xe"); | ||
+ | dba.setUsername("user"); | ||
+ | dba.setPassword("password"); | ||
+ | //also possible, but needs rad/apps/myapp/config.xml | ||
+ | //DBAccess dba = DBAccess.getDBAccess(DBSecurityManager.getCredentials( | ||
+ | // Configuration.getApplicationZone("myapp").getConfig())); | ||
+ | dba.open(); | ||
+ | dba.getConnection().setAutoCommit(false); | ||
+ | |||
+ | DBStorage dbsUser = new DBStorage(); | ||
+ | dbsUser.setDBAccess(dba); | ||
+ | dbsUser.setWritebackTable("USERS"); | ||
+ | dbsUser.open(); | ||
+ | </file> | ||
+ | |||
+ | If you prefer POJOs: | ||
+ | |||
+ | <file java> | ||
+ | User user = dbsUser.createPOJO(User.class, bnUser); | ||
+ | </file> | ||
+ | |||
+ | ==== 50% width with FormLayout ==== | ||
+ | |||
+ | If you have two components, e.g. GroupPanels and if you want that each group is 50% of the parent width, you could use following code: | ||
+ | |||
+ | <file java> | ||
+ | UIFormLayout layout = new UIFormLayout(); | ||
+ | | ||
+ | IConstraints center = layout.getHCenterConstraints(0, 5, -1, 5); | ||
+ | |||
+ | UILabel label = new UILabel(); | ||
+ | label.setPreferredSize(5, 0); | ||
+ | |||
+ | panel.add(label, center); | ||
+ | panel.add(gpanLeft, layout.getConstraints(center.getTopAnchor(), | ||
+ | layout.createAnchor(layout.getLeftMarginAnchor(), 0), | ||
+ | center.getBottomAnchor(), | ||
+ | layout.createAnchor(center.getLeftAnchor(), 0))); | ||
+ | panel.add(gpanRight, layout.getConstraints(center.getTopAnchor(), | ||
+ | layout.createAnchor(center.getRightAnchor(), 0), | ||
+ | center.getBottomAnchor(), | ||
+ | layout.createAnchor(layout.getRightMarginAnchor(), 0))); | ||
+ | </file> | ||
+ | |||
+ | ==== Search column in condition ==== | ||
+ | |||
+ | If you create your own IStorage impelementation, it could be useful to know how to find the value for a specific column: | ||
+ | |||
+ | <file java> | ||
+ | public static Object getEqualsValue(ICondition pFilter, String pColumn) | ||
+ | { | ||
+ | if (pFilter instanceof OperatorCondition) | ||
+ | { | ||
+ | for (ICondition cond : ((OperatorCondition)pFilter).getConditions()) | ||
+ | { | ||
+ | if (cond instanceof Equals) | ||
+ | { | ||
+ | if (pColumn.equals(((Equals)cond).getColumnName())) | ||
+ | { | ||
+ | return ((Equals)cond).getValue(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | else if (pFilter instanceof Equals) | ||
+ | { | ||
+ | if (pColumn.equals(((Equals)pFilter).getColumnName())) | ||
+ | { | ||
+ | return ((Equals)pFilter).getValue(); | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | return null; | ||
+ | } | ||
+ | </file> | ||
+ | |||
+ | Above method tries to find the first Equals condition with the given column name. | ||
+ | |||
+ | The method doesn't work with recursion because usually this is not necessary. If you want to know the different condition types, simply check ''toString()'' of ''javax.rad.model.condition.BaseCondition''. | ||
+ | |||
+ | ==== Connection property changed listener ==== | ||
+ | |||
+ | It's easy to listen on connection property changed events: | ||
+ | |||
+ | <file java> | ||
+ | MasterConnection appcon = new MasterConnection(createConnection()); | ||
+ | appcon.addPropertyChangedListener("Application.mode", new IConnectionPropertyChangedListener() | ||
+ | { | ||
+ | public void propertyChanged(PropertyEvent pEvent) | ||
+ | { | ||
+ | liProps.add(pEvent.getPropertyName() + " = " + pEvent.getNewValue()); | ||
+ | } | ||
+ | }); | ||
+ | </file> | ||
+ | |||
+ | Above listener will be invoked whenever the connection property **Application.mode** will be changed. | ||
+ | |||
+ | It's also possible to listen on all properties, with following snippet: | ||
+ | |||
+ | <file java> | ||
+ | MasterConnection appcon = new MasterConnection(createConnection()); | ||
+ | appcon.addPropertyChangedListener(null, new IConnectionPropertyChangedListener() | ||
+ | { | ||
+ | public void propertyChanged(PropertyEvent pEvent) | ||
+ | { | ||
+ | liProps.add(pEvent.getPropertyName() + " = " + pEvent.getNewValue()); | ||
+ | } | ||
+ | }); | ||
</file> | </file> |