001    /**
002     * Copyright 2005-2015 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package edu.sampleu.bookstore.maintenance;
017    
018    import java.util.List;
019    import java.util.Map;
020    
021    import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
022    import org.kuali.rice.krad.maintenance.MaintenanceDocument;
023    import org.kuali.rice.krad.service.KRADServiceLocator;
024    import edu.sampleu.bookstore.bo.Account;
025    import edu.sampleu.bookstore.bo.Author;
026    import edu.sampleu.bookstore.bo.Book;
027    
028    /**
029     * maintainableClass for Book document. 
030     * Action to be taken before saving the BO
031     */
032    
033    public class BookMaintainable extends KualiMaintainableImpl {
034    
035            
036            private static final long serialVersionUID = 1L;
037    
038            @Override
039            public void saveBusinessObject() {
040                    // TODO Auto-generated method stub
041                    Book book = (Book) this.getBusinessObject();
042            
043    
044                    // /-----------------------------------------------------///
045                    // /-----------------------------------------------------///
046                    // /---IF author has to be edited from Book Document-----///
047                    // /---follow the below mentioned code else comment out--///
048                    // /-----------------------------------------------------///
049                    // /-----------------------------------------------------///
050                    List<Author> authors = book.getAuthors();
051                    for (Author author : authors) {
052                            Account account = (Account) author.getExtension();
053                            if (account != null && account.getAuthorId() == null) {
054                                    author.setExtension(null);
055                            }
056    
057                            KRADServiceLocator.getBusinessObjectService().save(author);
058    
059                            if (account != null && account.getAuthorId() == null) {
060                                    account.setAuthorId(author.getAuthorId());
061                                    KRADServiceLocator.getBusinessObjectService().save(account);
062                            }
063                    }
064    
065                    book.setAuthors(authors);
066                    KRADServiceLocator.getBusinessObjectService().save(book);
067    
068            }
069    
070            @Override
071            public void processAfterCopy(MaintenanceDocument document,
072                            Map<String, String[]> parameters) {
073                    super.processAfterCopy(document, parameters);
074                    Book book = ((Book) document.getNewMaintainableObject()
075                                    .getDataObject());
076                    book.setIsbn(null);
077            }
078    
079    }