@@ -68,10 +68,6 @@ public void containerCreated(Container c, User user)
6868 {
6969 _log .warn ("Unable to populate default values for laboratory module" , e );
7070 }
71- catch (BatchValidationException e )
72- {
73- //ignore, since this may just indicate the table already has these values
74- }
7571 }
7672 }
7773
@@ -112,69 +108,11 @@ public void propertyChange(PropertyChangeEvent evt)
112108
113109 if (evt .getPropertyName ().equals (ContainerManager .Property .Name .name ()))
114110 {
115- if (evt instanceof ContainerManager .ContainerPropertyChangeEvent )
116- {
117- ContainerManager .ContainerPropertyChangeEvent ce = (ContainerManager .ContainerPropertyChangeEvent ) evt ;
118- if (ce .container .isWorkbook ())
119- {
120- TableInfo ti = LaboratorySchema .getInstance ().getTable (LaboratorySchema .TABLE_WORKBOOKS );
121- TableSelector ts = new TableSelector (ti , new SimpleFilter (FieldKey .fromString (LaboratoryWorkbooksTable .WORKBOOK_COL ), ce .container .getId ()), null );
122- if (ts .exists ())
123- {
124- try
125- {
126- Integer rowId = Integer .parseInt (ce .container .getName ());
127- WorkbookModel w = ts .getObject (ce .container .getId (), WorkbookModel .class );
128- w .setWorkbookId (rowId );
129- Table .update (ce .user , ti , w , ce .container .getId ());
130- }
131- catch (NumberFormatException e )
132- {
133-
134- }
135- }
136- }
137- }
111+ updateWorkbookTableOnNameChange (evt );
138112 }
139- else if (evt .getPropertyName ().equals (ContainerManager .Property .Policy .name ()))
113+ else if (evt .getPropertyName ().equals (ContainerManager .Property .Modules .name ()))
140114 {
141- if (evt instanceof ContainerManager .ContainerPropertyChangeEvent )
142- {
143- ContainerManager .ContainerPropertyChangeEvent ce = (ContainerManager .ContainerPropertyChangeEvent )evt ;
144-
145- User u = ce .user ;
146- if (u == null && HttpView .hasCurrentView ())
147- u = HttpView .currentView ().getViewContext ().getUser ();
148-
149- if (u == null || !ce .container .hasPermission (u , InsertPermission .class ))
150- return ;
151-
152- if (ce .container .getActiveModules ().contains (ModuleLoader .getInstance ().getModule (LaboratoryModule .class )))
153- {
154- try
155- {
156- LaboratoryManager .get ().initWorkbooksForContainer (u , ce .container );
157- }
158- catch (Exception e )
159- {
160- _log .error ("Unable to update laboratory workbooks table" , e );
161- }
162-
163- //attempt to populate default values on load
164- try
165- {
166- LaboratoryManager .get ().populateDefaultData (u , ce .container , null );
167- }
168- catch (IllegalArgumentException e )
169- {
170- _log .error ("Unable to populate defaults in laboratory module tables" , e );
171- }
172- catch (BatchValidationException e )
173- {
174- //ignore, since this may just indicate the table already has these values
175- }
176- }
177- }
115+ possiblyInitializeOnActiveModuleChange (evt );
178116 }
179117 }
180118
@@ -194,4 +132,91 @@ protected void purgeTable(UserSchema userSchema, TableInfo table, Container c)
194132 super .purgeTable (userSchema , table , c );
195133 }
196134 }
135+
136+ /**
137+ * The container name field stores the workbookId as a string. If that name changes (and this should no longer be permitted
138+ * for the most part in LK, we need to update this table
139+ */
140+ private void updateWorkbookTableOnNameChange (PropertyChangeEvent evt )
141+ {
142+ if (!(evt instanceof ContainerManager .ContainerPropertyChangeEvent ))
143+ {
144+ return ;
145+ }
146+
147+ ContainerManager .ContainerPropertyChangeEvent ce = (ContainerManager .ContainerPropertyChangeEvent ) evt ;
148+ if (!ce .container .isWorkbook ())
149+ {
150+ return ;
151+ }
152+
153+ TableInfo ti = LaboratorySchema .getInstance ().getTable (LaboratorySchema .TABLE_WORKBOOKS );
154+ TableSelector ts = new TableSelector (ti , new SimpleFilter (FieldKey .fromString (LaboratoryWorkbooksTable .WORKBOOK_COL ), ce .container .getId ()), null );
155+ if (ts .exists ())
156+ {
157+ try
158+ {
159+ Integer workbookId = Integer .parseInt (ce .container .getName ());
160+ WorkbookModel w = ts .getObject (ce .container .getId (), WorkbookModel .class );
161+ w .setWorkbookId (workbookId );
162+ Table .update (ce .user , ti , w , ce .container .getId ());
163+ }
164+ catch (NumberFormatException e )
165+ {
166+ _log .error ("Non-numeric workbook name: " + ce .container .getName () + " for: " + ce .container .getEntityId ());
167+ }
168+ }
169+ }
170+
171+ /**
172+ * The intent of this is to initialize the laboratory folder if the set of active modules
173+ * changes to include Laboratory. This should only occur on the parent folder, not individual workbooks.
174+ */
175+ private void possiblyInitializeOnActiveModuleChange (PropertyChangeEvent evt )
176+ {
177+ if (!(evt instanceof ContainerManager .ContainerPropertyChangeEvent ))
178+ {
179+ return ;
180+ }
181+
182+ ContainerManager .ContainerPropertyChangeEvent ce = (ContainerManager .ContainerPropertyChangeEvent )evt ;
183+
184+ //Only make these changes from the parent container for performance reasons
185+ if (ce .container .isWorkbook ())
186+ {
187+ return ;
188+ }
189+
190+ User u = ce .user ;
191+ if (u == null && HttpView .hasCurrentView ())
192+ {
193+ u = HttpView .currentView ().getViewContext ().getUser ();
194+ }
195+
196+ if (u == null || !ce .container .hasPermission (u , InsertPermission .class ))
197+ {
198+ return ;
199+ }
200+
201+ if (ce .container .getActiveModules ().contains (ModuleLoader .getInstance ().getModule (LaboratoryModule .class )))
202+ {
203+ try
204+ {
205+ LaboratoryManager .get ().recursivelyInitWorkbooksForContainer (u , ce .container );
206+ }
207+ catch (Exception e )
208+ {
209+ _log .error ("Unable to update laboratory workbooks table" , e );
210+ }
211+
212+ try
213+ {
214+ LaboratoryManager .get ().populateDefaultData (u , ce .container , null );
215+ }
216+ catch (Exception e )
217+ {
218+ _log .error ("Unable to populate defaults in laboratory module tables" , e );
219+ }
220+ }
221+ }
197222}
0 commit comments