0

I have to write a different class to read a file of different kind. Now project is deployed on client side. And we have to give support to new files. so we have to create a new class and also modify in service class to create a new object of newly added class. Writing a new class for new type of class is fine. But I do not want to change service class each time. Is there any solution for this kind of problem? Thanks in advance.

Update 1: here is code of service class

 @Service("StockistServiceImpl")
 public class StockistServiceImpl implements StockistService {

@Override
@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
public JSONArray saveStockistOrder(Integer stockistId,
        MultipartFile[] orderFile, String orderNumber, String orderDate,
        String partyCode,String order,Integer userId) 
{
                            List<Pair<String, Integer>> charList = new ArrayList<Pair<String, Integer>>();
                            Properties code1 = new Properties();
                            try {
                                code.load(StockistServiceImpl.class.getClassLoader().getResourceAsStream("categoryOfFile.properties"));
                            }
                            catch (IOException e) {
                                //System.out.println("error in loading divisionNamePdfCode.properties");
                                e.printStackTrace();
                            }
                            String readDuelListedTxtFile = code.getProperty("readDuelListedTxtFile");
                            String readStartLineLengthForOrderTxtFile = code.getProperty("readStartLineLengthForOrderTxtFile");
                            String ReadFileWithNoStartLineTxtFile = code.getProperty("ReadFileWithNoStartLineTxtFile");
                            String ReadStartLineLengthForQtySingleListTxtFile = code.getProperty("ReadStartLineLengthForQtySingleListTxtFile");

                            if (readDuelListedTxtFile.contains(partyCode
                                    .trim())) {
                                charList.addAll(dualListText
                                            .readDuelListedTxtFile(
                                                    fileName, codeDetails));
                            }
                            else if (readStartLineLengthForOrderTxtFile.contains(partyCode
                                    .trim())) {
                                charList.addAll(lineLength
                                            .readStartLineLengthForOrderTxtFile(
                                                    fileName, codeDetails));
                            }
                            else if (ReadFileWithNoStartLineTxtFile.contains(partyCode
                                    .trim())) {
                                T_FileWithNoStartLine noStartLine = new T_FileWithNoStartLine();
                                charList.addAll(noStartLine
                                            .readFileWithNoStartLineTxtFile(
                                                    fileName, codeDetails));
                            }
                            else if (ReadStartLineLengthForQtySingleListTxtFile.contains(partyCode
                                    .trim())) {
                                T_StartLineLengthForQtySingleList noStartLine = new T_StartLineLengthForQtySingleList();
                                charList.addAll(noStartLine
                                            .readStartLineLengthForQtySingleListTxtFile(
                                                    fileName, codeDetails));
                            }
}

Update 2: here is property file from where we know that what is file type for a stockist.

#fileType,stockistCode
fileType1=ST001,ST009
fileType2=ST002,ST005,ST006
fileType3=ST003,ST007
fileType4=ST004,ST008

and i want to add a new property file like this to map a file type with class name so if a new class is added and then we will not have to edit service class.

#fileType,fullyqualifiedclassName
fileType1=FullyQualifiedClassName1
fileType2=FullyQualifiedclassName2
fileType3=FullyQualifiedClassName3
fileType4=FullyQualifiedclassName4
Gaurav Rajput
  • 873
  • 3
  • 11
  • 19

3 Answers3

1

Instance can be created at runtime using reflection in java, please have a look at below post:

Creating an instance using the class name and calling constructor

Community
  • 1
  • 1
1

Separate the creation of the file readers objects and the service class.

public class BuildFileReader() {
    FileReader getReader(String xyz) {
        FileReader reader;

        ...
        your logic
        reader = new WhatEverReaderYouWant();
        ...

        return reader;
    }
}

The service class simply asks the BuildFileReader which FileReader to use and doesn't need to change anymore.

public class StockistServiceImpl {
    ...
    BuildFileReader bfr = new BuildFileReader();

    FileReader fileReader = bfr.getReader(xyz); 

    fileReader.readFile(fileName, codeDetails);
    ...
}

If you need only one type of file reader per client, you could configure your BuildFileReader for each client. If you need more than one type of file reader per client, define an interface for each type an add a getReaderXYZ() function for each needed type in BuildFileReader.

jaysee
  • 456
  • 1
  • 10
  • 15
0

Finally after doing some code changes and adding property file for mapping class names with property of file here is the code and working fine.

@Service("StockistServiceImpl")
public class StockistServiceImpl implements StockistService {

 List<Pair<String, Integer>> charList = new ArrayList<Pair<String, Integer>>();


 Map<String,String> mapTxtFile = new HashMap<String, String>();
                            Properties fileTypeProperties = new Properties();
                            Properties matchClassNameProperties = new Properties();
                            try {
                                fileTypeProperties.load(StockistServiceImpl.class.getClassLoader().getResourceAsStream("fileTypeProperties.properties"));
                            }
                            catch (IOException e) {

                                //e.printStackTrace();
                            }
                            try {
                                matchClassNameProperties.load(StockistServiceImpl.class.getClassLoader().getResourceAsStream("matchClassNameProperties.properties"));
                            }
                            catch (IOException e) {

                                //e.printStackTrace();
                            }

                            for (String key : fileTypeProperties.stringPropertyNames()) {
                                String value = fileTypeProperties.getProperty(key);
                                mapTxtFile.put(key, value);

                                if(value.contains(partyCode.trim())){                                       
                                    String className = matchClassNameProperties.getProperty(key);
                                    try {
                                        Class clazz =   Class.forName(className);
                                        try {
                                            TxtFile objToReadTxtFile = (TxtFile) clazz.newInstance();
                                            charList= objToReadTxtFile.readTxtFile(fileName, codeDetails);

                                        } catch (InstantiationException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        } catch (IllegalAccessException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }

                                    } catch (ClassNotFoundException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }else{
                                    //read normally else block

                                }
                            }


}

Now it is working fine.But for that i created an interface for reading txt file which has readTxtFile method. and all other classes now implement this interface.

Gaurav Rajput
  • 873
  • 3
  • 11
  • 19